Last active
October 14, 2020 01:22
-
-
Save genkuroki/e943b853e8f6163a04a9a7ac5da0e003 to your computer and use it in GitHub Desktop.
MetaUtils.jl example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Meta.@dump function f(x::T) where T<:Number\n sin(x)\nend", | |
"execution_count": 1, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Expr\n head: Symbol function\n args: Array{Any}((2,))\n 1: Expr\n head: Symbol where\n args: Array{Any}((2,))\n 1: Expr\n head: Symbol call\n args: Array{Any}((2,))\n 1: Symbol f\n 2: Expr\n head: Symbol ::\n args: Array{Any}((2,))\n 1: Symbol x\n 2: Symbol T\n 2: Expr\n head: Symbol <:\n args: Array{Any}((2,))\n 1: Symbol T\n 2: Symbol Number\n 2: Expr\n head: Symbol block\n args: Array{Any}((3,))\n 1: LineNumberNode\n line: Int64 1\n file: Symbol In[1]\n 2: LineNumberNode\n line: Int64 2\n file: Symbol In[1]\n 3: Expr\n head: Symbol call\n args: Array{Any}((2,))\n 1: Symbol sin\n 2: Symbol x\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "expr = :(\nfunction f(x::T) where T<:Number\n sin(x)\nend\n) |> Base.remove_linenums!", | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 2, | |
"data": { | |
"text/plain": ":(function f(x::T) where T <: Number\n sin(x)\n end)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "expr |> Meta.show_sexpr", | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "(:function, (:where, (:call, :f, (:(::), :x, :T)), (:<:, :T, :Number)), (:block,\n (:call, :sin, :x)\n ))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "expr |> dump", | |
"execution_count": 4, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Expr\n head: Symbol function\n args: Array{Any}((2,))\n 1: Expr\n head: Symbol where\n args: Array{Any}((2,))\n 1: Expr\n head: Symbol call\n args: Array{Any}((2,))\n 1: Symbol f\n 2: Expr\n head: Symbol ::\n args: Array{Any}((2,))\n 1: Symbol x\n 2: Symbol T\n 2: Expr\n head: Symbol <:\n args: Array{Any}((2,))\n 1: Symbol T\n 2: Symbol Number\n 2: Expr\n head: Symbol block\n args: Array{Any}((1,))\n 1: Expr\n head: Symbol call\n args: Array{Any}((2,))\n 1: Symbol sin\n 2: Symbol x\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "expr.head", | |
"execution_count": 5, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 5, | |
"data": { | |
"text/plain": ":function" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "expr.args[1]", | |
"execution_count": 6, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 6, | |
"data": { | |
"text/plain": ":(f(x::T) where T <: Number)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "expr.args[1] |> Meta.show_sexpr", | |
"execution_count": 7, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "(:where, (:call, :f, (:(::), :x, :T)), (:<:, :T, :Number))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "expr.args[1] |> dump", | |
"execution_count": 8, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Expr\n head: Symbol where\n args: Array{Any}((2,))\n 1: Expr\n head: Symbol call\n args: Array{Any}((2,))\n 1: Symbol f\n 2: Expr\n head: Symbol ::\n args: Array{Any}((2,))\n 1: Symbol x\n 2: Symbol T\n 2: Expr\n head: Symbol <:\n args: Array{Any}((2,))\n 1: Symbol T\n 2: Symbol Number\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "expr.args[2]", | |
"execution_count": 9, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 9, | |
"data": { | |
"text/plain": "quote\n sin(x)\nend" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "expr.args[2] |> Meta.show_sexpr", | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "(:block,\n (:call, :sin, :x)\n)", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "expr.args[2] |> dump", | |
"execution_count": 11, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Expr\n head: Symbol block\n args: Array{Any}((1,))\n 1: Expr\n head: Symbol call\n args: Array{Any}((2,))\n 1: Symbol sin\n 2: Symbol x\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "#]add https://github.com/genkuroki/MetaUtils.jl", | |
"execution_count": 12, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "#]dev https://github.com/genkuroki/MetaUtils.jl", | |
"execution_count": 13, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "using MetaUtils", | |
"execution_count": 14, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@show_expr function f(x::T) where T<:Number\n sin(x)\nend", | |
"execution_count": 15, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Expr(:function, \n Expr(:where, \n Expr(:call, :f, \n Expr(:(::), :x, :T)), \n Expr(:<:, :T, :Number)), \n Expr(:block, \n Expr(:call, :sin, :x)))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@show_texpr function f(x::T) where T<:Number\n sin(x)\nend", | |
"execution_count": 16, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "(:function, \n (:where, \n (:call, :f, \n (:(::), :x, :T)), \n (:<:, :T, :Number)), \n (:block, \n (:call, :sin, :x)))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@show_tree function f(x::T) where T<:Number\n sin(x)\nend", | |
"execution_count": 17, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": ":function\n├─ :where\n│ ├─ :call\n│ │ ├─ :f\n│ │ └─ :(::)\n│ │ ├─ :x\n│ │ └─ :T\n│ └─ :<:\n│ ├─ :T\n│ └─ :Number\n└─ :block\n └─ :call\n ├─ :sin\n └─ :x\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "(:block,\n (:function, \n (:where, \n (:call, :f, \n (:(::), :x, :T)), \n (:<:, :T, :Number)), \n (:block, \n (:call, :sin, :x))),\n (:call, :f, (:call, :/, π, 6))\n) |> texpr2expr", | |
"execution_count": 18, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 18, | |
"data": { | |
"text/plain": "quote\n function f(x::T) where T <: Number\n sin(x)\n end\n f(π / 6)\nend" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@teval (:block,\n (:function, \n (:where, \n (:call, :f, \n (:(::), :x, :T)), \n (:<:, :T, :Number)), \n (:block, \n (:call, :sin, :x))),\n (:call, :f, (:call, :/, π, 6))\n)", | |
"execution_count": 19, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 19, | |
"data": { | |
"text/plain": "0.49999999999999994" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "MetaUtils.@t (:block,\n (:function, \n (:where, \n (:call, :f, \n (:(::), :x, :T)), \n (:<:, :T, :Number)), \n (:block, \n (:call, :sin, :x))),\n (:call, :f, (:call, :/, π, 6))\n)", | |
"execution_count": 20, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "quote\n function f(x::T) where T <: Number\n sin(x)\n end\n f(π / 6)\nend\n→ 0.49999999999999994", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@show_tree @show float(π)", | |
"execution_count": 21, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": ":macrocall\n├─ Symbol(\"@show\")\n├─ :(#= In[21]:1 =#)\n└─ :call\n ├─ :float\n └─ :π\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@show_texpr @show float(π)", | |
"execution_count": 22, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "(:macrocall, Symbol(\"@show\"), LineNumberNode(1, Symbol(\"In[22]\")), \n (:call, :float, :π))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@show_expr @show float(π)", | |
"execution_count": 23, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Expr(:macrocall, Symbol(\"@show\"), LineNumberNode(1, Symbol(\"In[23]\")), \n Expr(:call, :float, :π))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Expr(:macrocall, Symbol(\"@show\"), LineNumberNode(1, Symbol(\"In[15]\")), \n Expr(:call, :float, :π)) |> eval", | |
"execution_count": 24, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "float(π) = 3.141592653589793\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"execution_count": 24, | |
"data": { | |
"text/plain": "3.141592653589793" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@show_sexpr LineNumberNode(@__LINE__, @__FILE__)", | |
"execution_count": 25, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "(:call, :LineNumberNode, (:macrocall, Symbol(\"@__LINE__\"), :(#= In[25]:1 =#)), (:macrocall, Symbol(\"@__FILE__\"), :(#= In[25]:1 =#)))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@show_texpr LineNumberNode(@__LINE__, @__FILE__)", | |
"execution_count": 26, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "(:call, :LineNumberNode, \n (:macrocall, Symbol(\"@__LINE__\"), LineNumberNode(1, Symbol(\"In[26]\"))), \n (:macrocall, Symbol(\"@__FILE__\"), LineNumberNode(1, Symbol(\"In[26]\"))))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@show_expr LineNumberNode(@__LINE__, @__FILE__)", | |
"execution_count": 27, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Expr(:call, :LineNumberNode, \n Expr(:macrocall, Symbol(\"@__LINE__\"), LineNumberNode(1, Symbol(\"In[27]\"))), \n Expr(:macrocall, Symbol(\"@__FILE__\"), LineNumberNode(1, Symbol(\"In[27]\"))))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Expr(:call, :LineNumberNode, \n Expr(:macrocall, Symbol(\"@__LINE__\"), Expr(:call, :LineNumberNode, 1, Symbol(\"In[44]\"))), \n Expr(:macrocall, Symbol(\"@__FILE__\"), Expr(:call, :LineNumberNode, 1, Symbol(\"In[44]\"))))", | |
"execution_count": 28, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 28, | |
"data": { | |
"text/plain": ":(LineNumberNode(@__LINE__(), @__FILE__()))" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "Expr(:call, :LineNumberNode, \n Expr(:macrocall, Symbol(\"@__LINE__\"), Expr(:call, :LineNumberNode, 1, Symbol(\"In[44]\"))), \n Expr(:macrocall, Symbol(\"@__FILE__\"), Expr(:call, :LineNumberNode, 1, Symbol(\"In[44]\")))) |> eval |> dump", | |
"execution_count": 29, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "LineNumberNode\n line: Int64 0\n file: Nothing nothing\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "LineNumberNode(@__LINE__, @__FILE__)", | |
"execution_count": 30, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 30, | |
"data": { | |
"text/plain": ":(#= In[30]:1 =#)" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "LineNumberNode(@__LINE__, @__FILE__) |> dump", | |
"execution_count": 31, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "LineNumberNode\n line: Int64 1\n file: Symbol In[31]\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "QuoteNode(:(sin(x)))", | |
"execution_count": 32, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 32, | |
"data": { | |
"text/plain": ":($(QuoteNode(:(sin(x)))))" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "QuoteNode(:(sin(x))) |> dump", | |
"execution_count": 33, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "QuoteNode\n value: Expr\n head: Symbol call\n args: Array{Any}((2,))\n 1: Symbol sin\n 2: Symbol x\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "QuoteNode(:(sin(x))) |> eval", | |
"execution_count": 34, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 34, | |
"data": { | |
"text/plain": ":(sin(x))" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "QuoteNode(:(sin(x))) |> Meta.show_sexpr", | |
"execution_count": 35, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "(:quote, #QuoteNode\n (:call, :sin, :x)\n)", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "QuoteNode(:(sin(x))) |> show_texpr", | |
"execution_count": 36, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "QuoteNode(\n (:call, :sin, :x))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "QuoteNode(:(sin(x))) |> show_expr", | |
"execution_count": 37, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "QuoteNode(\n Expr(:call, :sin, :x))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "QuoteNode(\n Expr(:call, :sin, :x)) |> eval", | |
"execution_count": 38, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 38, | |
"data": { | |
"text/plain": ":(sin(x))" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@teval (:quote, (:call, :sin, π/6)) ", | |
"execution_count": 39, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 39, | |
"data": { | |
"text/plain": ":(sin(0.5235987755982988))" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "QuoteNode(Expr(:call, :sin, π/6)) ", | |
"execution_count": 40, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"execution_count": 40, | |
"data": { | |
"text/plain": ":($(QuoteNode(:(sin(0.5235987755982988)))))" | |
}, | |
"metadata": {} | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "@show_expr f(x...)", | |
"execution_count": 41, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Expr(:call, :f, \n Expr(:..., :x))", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true | |
}, | |
"cell_type": "code", | |
"source": "", | |
"execution_count": null, | |
"outputs": [] | |
} | |
], | |
"metadata": { | |
"@webio": { | |
"lastKernelId": null, | |
"lastCommId": null | |
}, | |
"_draft": { | |
"nbviewer_url": "https://gist.github.com/e943b853e8f6163a04a9a7ac5da0e003" | |
}, | |
"gist": { | |
"id": "e943b853e8f6163a04a9a7ac5da0e003", | |
"data": { | |
"description": "MetaUtils.jl example", | |
"public": true | |
} | |
}, | |
"kernelspec": { | |
"name": "julia-depwarn--o3-1.6", | |
"display_name": "Julia depwarn -O3 1.6.0-DEV", | |
"language": "julia" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"name": "julia", | |
"mimetype": "application/julia", | |
"version": "1.6.0" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"base_numbering": 1, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment