-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.rb
63 lines (49 loc) · 1.49 KB
/
nodes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# collection of nodes each one representing an expression
class Nodes < Struct.new(:nodes)
def <<(node)
nodes << node
self
end
end
# literals are static values that have a Ruby representation, eg.: a string, a number,
# true, false, nil, etc
class LiteralNode < Struct.new(:value); end
class NumberNode < LiteralNode; end
class StringNode < LiteralNode; end
class TrueNode < LiteralNode
def initialize
super(true)
end
end
class FalseNode < LiteralNode
def initialize
super(false)
end
end
class NilNode < LiteralNode
def initialize
super(nil)
end
end
class WhileNode < Struct.new(:condition, :body); end
# node of a method call or local variable access, can take any of these forms:
#
# method # this form can also be a local variable
# method(argument1, argument2)
# receiver.method
# receiver.method(argument1, argument2)
#
class CallNode < Struct.new(:receiver, :method, :arguments); end
# retrieving the value of a constant
class GetConstantNode < Struct.new(:name); end
# setting the value of a constant
class SetConstantNode < Struct.new(:name, :value); end
# setting the value of a local variable
class SetLocalNode < Struct.new(:name, :value); end
# method definition
class DefNode < Struct.new(:name, :params, :body); end
# class definition
class ClassNode < Struct.new(:name, :body); end
# "if" control structure. look at this node if you want to implement other control
# structures like while, for, loop, etc
class IfNode < Struct.new(:condition, :body); end