Create a gist now

Instantly share code, notes, and snippets.

@benolee /01-ast-s-exp Secret
Created Nov 22, 2012

[jruby] playing with ast
$ ~/.rbenv/versions/jruby-1.7.0/bin/ast -h
Usage: /Users/bholley/.rbenv/versions/jruby-1.7.0/bin/ast [options]
-d, --dot Display as dot data
-h, --help Display this help
-i, --ir Dump all IR passes without executing
-s, --sexp Display the S-Expression for the AST
--source Display the source
--no-ast Do not print out the AST for this (only useful with -s)
-e, --expression exp
$ cat << 'WAT' > wat.rb
> class Foo
> def bar
> puts "hola mundo"
> end
> end
>
> f = Foo.new
> f.bar
> WAT
$ ast -s --no-ast wat.rb
SEXP:
(root (block (newline (class (colon2implicit Foo) (newline (defn (argument bar) (argsnoarg) (newline (fcallonearg puts (array (str 'hola mundo')))))))) (newline (localasgn f (callnoarg new (const Foo)))) (newline (callnoarg bar (localvar f)))))
class Foo
def bar
puts "hola mundo"
end
end
f = Foo.new
f.bar
ast -d wat.rb | dot -T png -o wat.png
AST:
RootNode 0
BlockNode 0
NewlineNode 0
ClassNode 0
Colon2ImplicitNode:Foo 0
NewlineNode 1
DefnNode:bar 1
ArgumentNode:bar 1
ArgsNoArgNode 2
NewlineNode 2
FCallOneArgNode:puts 2
ArrayNode 2
StrNode 2
NewlineNode 6
LocalAsgnNode:f 6
CallNoArgNode:new 6
ConstNode:Foo 6
NewlineNode 7
CallNoArgNode:bar 7
LocalVarNode:f 7
2012-11-22T11:45:03.580-06:00: BasicCompilerPassListener: Starting Temporary Variable Reduction on scope Script: file: -ScriptBody -[-:0]
2012-11-22T11:45:03.582-06:00: BasicCompilerPassListener:
instrs:
0 %self = recv_self
1 line_num(0)
2 %v_0 = def_class(Foo, module<->, nil, -)
3 [DEAD-RESULT]%v_1 = process_module_body(%v_0)
4 line_num(6)
5 %v_0 = search_const(Foo, scope<->, no-private-consts=false)
6 f(0:0) = call(NORMAL, 'new', %v_0, [])
7 line_num(7)
8 %v_0 = call(NORMAL, 'bar', f(0:0), [])
9 return(%v_0)
2012-11-22T11:45:03.582-06:00: BasicCompilerPassListener:
live variables:
%v_0: 2-9
2012-11-22T11:45:03.582-06:00: BasicCompilerPassListener: Finished Temporary Variable Reduction on scope in 2ms.
2012-11-22T11:45:03.583-06:00: BasicCompilerPassListener: Starting CFG Builder on scope Script: file: -ScriptBody -[-:0]
2012-11-22T11:45:03.585-06:00: BasicCompilerPassListener:
Graph:
BB [1:LBL_0]:>[3,2]
BB [2:LBL_1]:>[3], <[1]
BB [3:LBL_2]:<[1,2]
2012-11-22T11:45:03.585-06:00: BasicCompilerPassListener:
Instructions:
BB [1:LBL_0]
BB [2:LBL_1]
%self = recv_self
line_num(0)
%v_0 = def_class(Foo, module<->, nil, -)
[DEAD-RESULT]%v_1 = process_module_body(%v_0)
line_num(6)
%v_0 = search_const(Foo, scope<->, no-private-consts=false)
f(0:0) = call(NORMAL, 'new', %v_0, [])
line_num(7)
%v_0 = call(NORMAL, 'bar', f(0:0), [])
return(%v_0)
BB [3:LBL_2]
------ Rescue block map ------
------ Ensure block map ------
2012-11-22T11:45:03.585-06:00: BasicCompilerPassListener: Finished CFG Builder on scope in 2ms.
2012-11-22T11:45:03.585-06:00: BasicCompilerPassListener: Starting Local Optimizations on scope Script: file: -ScriptBody -[-:0]
2012-11-22T11:45:03.586-06:00: BasicCompilerPassListener:
Graph:
BB [1:LBL_0]:>[3,2]
BB [2:LBL_1]:>[3], <[1]
BB [3:LBL_2]:<[1,2]
2012-11-22T11:45:03.586-06:00: BasicCompilerPassListener:
Instructions:
BB [1:LBL_0]
BB [2:LBL_1]
%self = recv_self
line_num(0)
%v_0 = def_class(Foo, module<->, nil, -)
[DEAD-RESULT]%v_1 = process_module_body(%v_0)
line_num(6)
%v_0 = search_const(Foo, scope<->, no-private-consts=false)
f(0:0) = call(NORMAL, 'new', %v_0, [])
line_num(7)
%v_0 = call(NORMAL, 'bar', f(0:0), [])
return(%v_0)
BB [3:LBL_2]
------ Rescue block map ------
------ Ensure block map ------
2012-11-22T11:45:03.586-06:00: BasicCompilerPassListener: Finished Local Optimizations on scope in 0ms.
2012-11-22T11:45:03.586-06:00: BasicCompilerPassListener: Starting Linearize CFG on scope Script: file: -ScriptBody -[-:0]
2012-11-22T11:45:03.587-06:00: BasicCompilerPassListener:
Graph:
BB [1:LBL_0]:>[3,2]
BB [2:LBL_1]:>[3], <[1]
BB [3:LBL_2]:<[1,2]
2012-11-22T11:45:03.587-06:00: BasicCompilerPassListener:
Instructions:
BB [1:LBL_0]
BB [2:LBL_1]
%self = recv_self
line_num(0)
%v_0 = def_class(Foo, module<->, nil, -)
[DEAD-RESULT]%v_1 = process_module_body(%v_0)
line_num(6)
%v_0 = search_const(Foo, scope<->, no-private-consts=false)
f(0:0) = call(NORMAL, 'new', %v_0, [])
line_num(7)
%v_0 = call(NORMAL, 'bar', f(0:0), [])
return(%v_0)
BB [3:LBL_2]
------ Rescue block map ------
------ Ensure block map ------
2012-11-22T11:45:03.587-06:00: BasicCompilerPassListener: Finished Linearize CFG on scope in 1ms.
2012-11-22T11:45:03.590-06:00: BasicCompilerPassListener: Starting Temporary Variable Reduction on scope ClassBody Foo[-:0]
2012-11-22T11:45:03.591-06:00: BasicCompilerPassListener:
instrs:
0 %self = recv_self
1 line_num(1)
2 def_inst_meth("--unused--", bar, -)
3 return(nil)
2012-11-22T11:45:03.591-06:00: BasicCompilerPassListener:
live variables:
2012-11-22T11:45:03.591-06:00: BasicCompilerPassListener: Finished Temporary Variable Reduction on scope in 1ms.
2012-11-22T11:45:03.591-06:00: BasicCompilerPassListener: Starting CFG Builder on scope ClassBody Foo[-:0]
2012-11-22T11:45:03.591-06:00: BasicCompilerPassListener:
Graph:
BB [1:LBL_0]:>[2,3]
BB [2:LBL_1]:>[3], <[1]
BB [3:LBL_2]:<[2,1]
2012-11-22T11:45:03.591-06:00: BasicCompilerPassListener:
Instructions:
BB [1:LBL_0]
BB [2:LBL_1]
%self = recv_self
line_num(1)
def_inst_meth("--unused--", bar, -)
return(nil)
BB [3:LBL_2]
------ Rescue block map ------
------ Ensure block map ------
2012-11-22T11:45:03.591-06:00: BasicCompilerPassListener: Finished CFG Builder on scope in 0ms.
2012-11-22T11:45:03.591-06:00: BasicCompilerPassListener: Starting Local Optimizations on scope ClassBody Foo[-:0]
2012-11-22T11:45:03.591-06:00: BasicCompilerPassListener:
Graph:
BB [1:LBL_0]:>[2,3]
BB [2:LBL_1]:>[3], <[1]
BB [3:LBL_2]:<[2,1]
2012-11-22T11:45:03.591-06:00: BasicCompilerPassListener:
Instructions:
BB [1:LBL_0]
BB [2:LBL_1]
%self = recv_self
line_num(1)
def_inst_meth("--unused--", bar, -)
return(nil)
BB [3:LBL_2]
------ Rescue block map ------
------ Ensure block map ------
2012-11-22T11:45:03.591-06:00: BasicCompilerPassListener: Finished Local Optimizations on scope in 0ms.
2012-11-22T11:45:03.592-06:00: BasicCompilerPassListener: Starting Linearize CFG on scope ClassBody Foo[-:0]
2012-11-22T11:45:03.592-06:00: BasicCompilerPassListener:
Graph:
BB [1:LBL_0]:>[2,3]
BB [2:LBL_1]:>[3], <[1]
BB [3:LBL_2]:<[2,1]
2012-11-22T11:45:03.592-06:00: BasicCompilerPassListener:
Instructions:
BB [1:LBL_0]
BB [2:LBL_1]
%self = recv_self
line_num(1)
def_inst_meth("--unused--", bar, -)
return(nil)
BB [3:LBL_2]
------ Rescue block map ------
------ Ensure block map ------
2012-11-22T11:45:03.592-06:00: BasicCompilerPassListener: Finished Linearize CFG on scope in 1ms.
2012-11-22T11:45:03.593-06:00: BasicCompilerPassListener: Starting Temporary Variable Reduction on scope Method bar[-:1]
2012-11-22T11:45:03.593-06:00: BasicCompilerPassListener:
instrs:
0 %self = recv_self
1 check_arity(0, 0, -1)
2 %block(0:0) = recv_closure
3 thread_poll
4 line_num(2)
5 %v_0 = call(FUNCTIONAL, 'puts', %self, ["hola mundo"])
6 return(%v_0)
2012-11-22T11:45:03.593-06:00: BasicCompilerPassListener:
live variables:
%v_0: 5-6
2012-11-22T11:45:03.593-06:00: BasicCompilerPassListener: Finished Temporary Variable Reduction on scope in 0ms.
2012-11-22T11:45:03.594-06:00: BasicCompilerPassListener: Starting CFG Builder on scope Method bar[-:1]
2012-11-22T11:45:03.594-06:00: BasicCompilerPassListener:
Graph:
BB [1:LBL_0]:>[2,3]
BB [2:LBL_1]:>[3], <[1]
BB [3:LBL_2]:<[2,1]
2012-11-22T11:45:03.594-06:00: BasicCompilerPassListener:
Instructions:
BB [1:LBL_0]
BB [2:LBL_1]
%self = recv_self
check_arity(0, 0, -1)
%block(0:0) = recv_closure
thread_poll
line_num(2)
%v_0 = call(FUNCTIONAL, 'puts', %self, ["hola mundo"])
return(%v_0)
BB [3:LBL_2]
------ Rescue block map ------
------ Ensure block map ------
2012-11-22T11:45:03.594-06:00: BasicCompilerPassListener: Finished CFG Builder on scope in 0ms.
2012-11-22T11:45:03.594-06:00: BasicCompilerPassListener: Starting Local Optimizations on scope Method bar[-:1]
2012-11-22T11:45:03.594-06:00: BasicCompilerPassListener:
Graph:
BB [1:LBL_0]:>[2,3]
BB [2:LBL_1]:>[3], <[1]
BB [3:LBL_2]:<[2,1]
2012-11-22T11:45:03.594-06:00: BasicCompilerPassListener:
Instructions:
BB [1:LBL_0]
BB [2:LBL_1]
%self = recv_self
check_arity(0, 0, -1)
%block(0:0) = recv_closure
thread_poll
line_num(2)
%v_0 = call(FUNCTIONAL, 'puts', %self, ["hola mundo"])
return(%v_0)
BB [3:LBL_2]
------ Rescue block map ------
------ Ensure block map ------
2012-11-22T11:45:03.594-06:00: BasicCompilerPassListener: Finished Local Optimizations on scope in 0ms.
2012-11-22T11:45:03.594-06:00: BasicCompilerPassListener: Starting Linearize CFG on scope Method bar[-:1]
2012-11-22T11:45:03.594-06:00: BasicCompilerPassListener:
Graph:
BB [1:LBL_0]:>[2,3]
BB [2:LBL_1]:>[3], <[1]
BB [3:LBL_2]:<[2,1]
2012-11-22T11:45:03.595-06:00: BasicCompilerPassListener:
Instructions:
BB [1:LBL_0]
BB [2:LBL_1]
%self = recv_self
check_arity(0, 0, -1)
%block(0:0) = recv_closure
thread_poll
line_num(2)
%v_0 = call(FUNCTIONAL, 'puts', %self, ["hola mundo"])
return(%v_0)
BB [3:LBL_2]
------ Rescue block map ------
------ Ensure block map ------
2012-11-22T11:45:03.595-06:00: BasicCompilerPassListener: Finished Linearize CFG on scope in 0ms.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment