Class: Mulang::Ruby::AstProcessor
- Inherits:
-
AST::Processor
- Object
- AST::Processor
- Mulang::Ruby::AstProcessor
show all
- Includes:
- AST::Sexp, Sexp
- Defined in:
- lib/mulang/ruby/ast_processor.rb
Instance Method Summary
collapse
-
#_ ⇒ Object
-
#handle_send_with_args(node, extra_args = []) ⇒ Object
-
#handler_missing(*args) ⇒ Object
-
#on_and(node) ⇒ Object
-
#on_and_asgn(node) ⇒ Object
-
#on_arg(node) ⇒ Object
(also: #on_restarg, #on_procarg0)
-
#on_array(node) ⇒ Object
-
#on_begin(node) ⇒ Object
-
#on_block(node) ⇒ Object
-
#on_casgn(node) ⇒ Object
-
#on_class(node) ⇒ Object
-
#on_const(node) ⇒ Object
-
#on_def(node) ⇒ Object
-
#on_defs(node) ⇒ Object
-
#on_dstr(node) ⇒ Object
-
#on_ensure(node) ⇒ Object
-
#on_false(_) ⇒ Object
-
#on_float(node) ⇒ Object
(also: #on_int)
-
#on_if(node) ⇒ Object
-
#on_irange(node) ⇒ Object
-
#on_kwbegin(node) ⇒ Object
-
#on_lvar(node) ⇒ Object
(also: #on_ivar)
-
#on_lvasgn(node) ⇒ Object
(also: #on_ivasgn)
-
#on_module(node) ⇒ Object
-
#on_nil(_) ⇒ Object
-
#on_op_asgn(node) ⇒ Object
-
#on_or(node) ⇒ Object
-
#on_or_asgn(node) ⇒ Object
-
#on_regexp(node) ⇒ Object
-
#on_resbody(node) ⇒ Object
-
#on_rescue(node) ⇒ Object
-
#on_return(node) ⇒ Object
-
#on_self(_) ⇒ Object
-
#on_send(node) ⇒ Object
-
#on_str(node) ⇒ Object
-
#on_sym(node) ⇒ Object
-
#on_true(_) ⇒ Object
-
#property_assignment(assignee, message, value) ⇒ Object
-
#reasign(accessor, args, id, message, value) ⇒ Object
-
#to_mulang_pattern(patterns, variable) ⇒ Object
-
#to_single_pattern(patterns) ⇒ Object
-
#to_type_pattern(node) ⇒ Object
-
#var_assignment(assignee, message, value) ⇒ Object
Methods included from Sexp
#ms, #mu_method, #sequence, #simple_method, #simple_send
Instance Method Details
#_ ⇒ Object
40
41
42
|
# File 'lib/mulang/ruby/ast_processor.rb', line 40
def _
Object.new.tap { |it| it.define_singleton_method(:==) { |_| true } }
end
|
#handle_send_with_args(node, extra_args = []) ⇒ Object
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
# File 'lib/mulang/ruby/ast_processor.rb', line 263
def handle_send_with_args(node, =[])
receptor, message, *args = *node
receptor ||= s(:self)
if message == :==
message = {tag: :Equal}
elsif message == :!=
message = {tag: :NotEqual}
else
message = {tag: :Reference, contents: message}
end
ms :Send, process(receptor), message, (process_all(args) + )
end
|
#handler_missing(*args) ⇒ Object
258
259
260
261
|
# File 'lib/mulang/ruby/ast_processor.rb', line 258
def handler_missing(*args)
puts args
ms :Other
end
|
#on_and(node) ⇒ Object
98
99
100
101
102
|
# File 'lib/mulang/ruby/ast_processor.rb', line 98
def on_and(node)
value, other = *node
simple_send process(value), '&&', [process(other)]
end
|
#on_and_asgn(node) ⇒ Object
232
233
234
235
|
# File 'lib/mulang/ruby/ast_processor.rb', line 232
def on_and_asgn(node)
assignee, value = *node
on_op_asgn s :op_asgn, assignee, '&&', value
end
|
#on_arg(node) ⇒ Object
Also known as:
on_restarg, on_procarg0
149
150
151
152
|
# File 'lib/mulang/ruby/ast_processor.rb', line 149
def on_arg(node)
name, _ = *node
ms :VariablePattern, name
end
|
#on_array(node) ⇒ Object
253
254
255
256
|
# File 'lib/mulang/ruby/ast_processor.rb', line 253
def on_array(node)
elements = *node
{tag: :MuList, contents: process_all(elements)}
end
|
#on_begin(node) ⇒ Object
25
26
27
|
# File 'lib/mulang/ruby/ast_processor.rb', line 25
def on_begin(node)
sequence(*process_all(node))
end
|
#on_block(node) ⇒ Object
131
132
133
134
135
|
# File 'lib/mulang/ruby/ast_processor.rb', line 131
def on_block(node)
send, parameters, body = *node
lambda = ms(:Lambda, process_all(parameters), process(body))
handle_send_with_args send, [lambda]
end
|
#on_casgn(node) ⇒ Object
192
193
194
195
|
# File 'lib/mulang/ruby/ast_processor.rb', line 192
def on_casgn(node)
_ns, id, value = *node
ms :Assignment, id, process(value)
end
|
#on_class(node) ⇒ Object
6
7
8
9
10
11
12
13
14
|
# File 'lib/mulang/ruby/ast_processor.rb', line 6
def on_class(node)
name, superclass, body = *node
body ||= s(:nil)
_, class_name = *name
_, superclass_name = *superclass
ms :Class, class_name, superclass_name, process(body)
end
|
#on_const(node) ⇒ Object
240
241
242
243
|
# File 'lib/mulang/ruby/ast_processor.rb', line 240
def on_const(node)
_ns, value = *node
ms :Reference, value
end
|
#on_def(node) ⇒ Object
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/mulang/ruby/ast_processor.rb', line 117
def on_def(node)
id, args, body = *node
body ||= s(:nil)
case id
when :equal?, :eql?, :==
mu_method :EqualMethod, process_all(args), process(body)
when :hash
mu_method :HashMethod, process_all(args), process(body)
else
simple_method id, process_all(args), process(body)
end
end
|
#on_defs(node) ⇒ Object
110
111
112
113
114
115
|
# File 'lib/mulang/ruby/ast_processor.rb', line 110
def on_defs(node)
_target, id, args, body = *node
body ||= s(:nil)
simple_method id, process_all(args), process(body)
end
|
#on_dstr(node) ⇒ Object
87
88
89
90
91
|
# File 'lib/mulang/ruby/ast_processor.rb', line 87
def on_dstr(node)
parts = *node
simple_send ms(:MuList, process_all(parts)), :join, []
end
|
#on_ensure(node) ⇒ Object
71
72
73
74
75
|
# File 'lib/mulang/ruby/ast_processor.rb', line 71
def on_ensure(node)
catch, finally = *node
try, catches = on_rescue(catch)[:contents]
ms :Try, try, catches, process(finally)
end
|
#on_false(_) ⇒ Object
249
250
251
|
# File 'lib/mulang/ruby/ast_processor.rb', line 249
def on_false(_)
ms :MuBool, false
end
|
#on_float(node) ⇒ Object
Also known as:
on_int
167
168
169
170
|
# File 'lib/mulang/ruby/ast_processor.rb', line 167
def on_float(node)
value, _ = *node
ms :MuNumber, value
end
|
#on_if(node) ⇒ Object
174
175
176
177
178
179
180
|
# File 'lib/mulang/ruby/ast_processor.rb', line 174
def on_if(node)
condition, if_true, if_false = *node
if_true ||= s(:nil)
if_false ||= s(:nil)
ms :If, process(condition), process(if_true), process(if_false)
end
|
#on_irange(node) ⇒ Object
77
78
79
|
# File 'lib/mulang/ruby/ast_processor.rb', line 77
def on_irange(node)
ms :Other
end
|
#on_kwbegin(node) ⇒ Object
67
68
69
|
# File 'lib/mulang/ruby/ast_processor.rb', line 67
def on_kwbegin(node)
process node.to_a.first
end
|
#on_lvar(node) ⇒ Object
Also known as:
on_ivar
182
183
184
185
|
# File 'lib/mulang/ruby/ast_processor.rb', line 182
def on_lvar(node)
value = *node
ms :Reference, value.first
end
|
#on_lvasgn(node) ⇒ Object
Also known as:
on_ivasgn
187
188
189
190
|
# File 'lib/mulang/ruby/ast_processor.rb', line 187
def on_lvasgn(node)
id, value = *node
ms :Assignment, id, process(value)
end
|
#on_module(node) ⇒ Object
16
17
18
19
20
21
22
23
|
# File 'lib/mulang/ruby/ast_processor.rb', line 16
def on_module(node)
name, body = *node
body ||= s(:nil)
_, module_name = *name
ms :Object, module_name, process(body)
end
|
#on_nil(_) ⇒ Object
141
142
143
|
# File 'lib/mulang/ruby/ast_processor.rb', line 141
def on_nil(_)
ms :MuNull
end
|
#on_op_asgn(node) ⇒ Object
197
198
199
200
201
202
203
204
205
|
# File 'lib/mulang/ruby/ast_processor.rb', line 197
def on_op_asgn(node)
assignee, message, value = *node
if assignee.type == :send
property_assignment assignee, message, value
else
var_assignment assignee, message, value
end
end
|
#on_or(node) ⇒ Object
93
94
95
96
|
# File 'lib/mulang/ruby/ast_processor.rb', line 93
def on_or(node)
value, other = *node
simple_send process(value), '||', [process(other)]
end
|
#on_or_asgn(node) ⇒ Object
227
228
229
230
|
# File 'lib/mulang/ruby/ast_processor.rb', line 227
def on_or_asgn(node)
assignee, value = *node
on_op_asgn s :op_asgn, assignee, '||', value
end
|
#on_regexp(node) ⇒ Object
81
82
83
84
85
|
# File 'lib/mulang/ruby/ast_processor.rb', line 81
def on_regexp(node)
value, _ops = *node
simple_send ms(:Reference, :Regexp), :new, [process(value)]
end
|
#on_resbody(node) ⇒ Object
34
35
36
37
38
|
# File 'lib/mulang/ruby/ast_processor.rb', line 34
def on_resbody(node)
patterns, variable, block = *node
[to_mulang_pattern(patterns, variable), process(block) || ms(:MuNull)]
end
|
#on_rescue(node) ⇒ Object
29
30
31
32
|
# File 'lib/mulang/ruby/ast_processor.rb', line 29
def on_rescue(node)
try, *catch, _ = *node
ms :Try, process(try), process_all(catch), ms(:MuNull)
end
|
#on_return(node) ⇒ Object
104
105
106
107
108
|
# File 'lib/mulang/ruby/ast_processor.rb', line 104
def on_return(node)
value = *node
ms(:Return, process(value.first))
end
|
#on_self(_) ⇒ Object
145
146
147
|
# File 'lib/mulang/ruby/ast_processor.rb', line 145
def on_self(_)
ms :Self
end
|
#on_send(node) ⇒ Object
137
138
139
|
# File 'lib/mulang/ruby/ast_processor.rb', line 137
def on_send(node)
handle_send_with_args(node)
end
|
#on_str(node) ⇒ Object
157
158
159
160
|
# File 'lib/mulang/ruby/ast_processor.rb', line 157
def on_str(node)
value, _ = *node
ms :MuString, value
end
|
#on_sym(node) ⇒ Object
162
163
164
165
|
# File 'lib/mulang/ruby/ast_processor.rb', line 162
def on_sym(node)
value, _ = *node
ms :MuSymbol, value.to_s
end
|
#on_true(_) ⇒ Object
245
246
247
|
# File 'lib/mulang/ruby/ast_processor.rb', line 245
def on_true(_)
ms :MuBool, true
end
|
#property_assignment(assignee, message, value) ⇒ Object
212
213
214
215
216
|
# File 'lib/mulang/ruby/ast_processor.rb', line 212
def property_assignment(assignee, message, value)
receiver, accessor, *accessor_args = *assignee
reasign accessor, process_all(accessor_args), process(receiver), message, process(value)
end
|
#reasign(accessor, args, id, message, value) ⇒ Object
218
219
220
221
222
223
224
225
|
# File 'lib/mulang/ruby/ast_processor.rb', line 218
def reasign(accessor, args, id, message, value)
simple_send id,
"#{accessor}=".to_sym,
args + [simple_send(
simple_send(id, accessor, args),
message,
[value])]
end
|
#to_mulang_pattern(patterns, variable) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/mulang/ruby/ast_processor.rb', line 44
def to_mulang_pattern(patterns, variable)
case [patterns, variable]
when [nil, nil]
ms :WildcardPattern
when [nil, _]
ms :VariablePattern, variable.to_a.first
when [_, nil]
to_single_pattern patterns
else
ms(:AsPattern, variable.to_a.first, to_single_pattern(patterns))
end
end
|
#to_single_pattern(patterns) ⇒ Object
57
58
59
60
|
# File 'lib/mulang/ruby/ast_processor.rb', line 57
def to_single_pattern(patterns)
mu_patterns = patterns.to_a.map { |it| to_type_pattern it }
mu_patterns.size == 1 ? mu_patterns.first : ms(:UnionPattern, mu_patterns)
end
|
#to_type_pattern(node) ⇒ Object
62
63
64
65
|
# File 'lib/mulang/ruby/ast_processor.rb', line 62
def to_type_pattern(node)
_, type = *node
ms :TypePattern, type
end
|
#var_assignment(assignee, message, value) ⇒ Object
207
208
209
210
|
# File 'lib/mulang/ruby/ast_processor.rb', line 207
def var_assignment(assignee, message, value)
id = assignee.to_a.first
ms :Assignment, id, simple_send(ms(:Reference, id), message, [process(value)])
end
|