forked from proglangclass/interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.rb
More file actions
52 lines (45 loc) · 1.29 KB
/
nodes.rb
File metadata and controls
52 lines (45 loc) · 1.29 KB
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
# Collection of nodes each one representing an expression.
class Nodes < Struct.new(:nodes)
def <<(node)
nodes << node
self
end
end
# Literals: static values that have a Ruby representation.
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
# 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
# Setting the value of a local variable.
class AssignNode < Struct.new(:name, :value); end
# Getting the value of a constant
class ConstantNode < Struct.new(:name); 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