-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnodes.rb
209 lines (183 loc) · 4.64 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
class Awesome
end
# Collection of nodes each one representing an expression.
class Nodes < Awesome
attr_accessor :nodes
def initialize(nodes)
@nodes = nodes
end
def <<(node)
@nodes << node
self
end
# This method is the "interpreter" part of our language.
# All nodes know how to eval itself and returns the result
# of its evaluation.
# The "context" variable is the environment in which the node
# is evaluated (local variables, current class, etc.).
def eval(context)
# The last value evaluated in a method is the return value.
@nodes.map { |node| node.eval(context) }.last
end
end
# Literals are static values that have a Ruby representation,
# eg.: a string, a number, true, false, nil, etc.
class LiteralNode < Awesome
attr_accessor :value
def initialize(value)
@value = value
end
def eval(context)
case @value
when Numeric
Runtime["Number"].new_value(@value)
when String
Runtime["String"].new_value(@value)
when TrueClass
Runtime["true"]
when FalseClass
Runtime["false"]
when NilClass
Runtime["nil"]
else
raise "Unknown literal type: " + @value.class.name
end
end
end
class VarNode < Awesome
attr_accessor :name
def initialize(name)
@name = name
end
def eval(context)
context[@name]
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 < Awesome
attr_accessor :receiver, :method, :arguments, :is_end
def initialize(receiver, method, arguments=[], is_end=false)
@receiver = receiver
@method = method
@arguments = arguments
@is_end = is_end
end
def eval(context)
# If there's no receiver and the method name is
# the name of a local variable, then it's a local
# variable access.
# This trick allows us to skip the () when calling
# a method.
if @receiver.nil? && context.locals[@method]
context.locals[@method]
# Method call
else
# In case there's no receiver we default to self
# So that calling "print" is like "self.print".
if @receiver
receiver = @receiver.eval(context)
else
receiver = context.current_self
end
arguments = @arguments.map { |arg| arg.eval(context) }
receiver.call(@method, arguments)
end
end
end
class ArrayNode < Awesome
attr_accessor :values
def initialize(arguments=[])
@values = arguments
end
def end(context)
context[@value]
end
end
# Retreiving the value of a constant.
class GetConstantNode < Awesome
attr_accessor :name
def initialize(name)
@name = name
end
def eval(context)
context[@name]
end
end
# Setting the value of a constant.
class SetConstantNode < Awesome
attr_accessor :name, :value
def initialize(name, value)
@name = name
@value = value
end
def eval(context)
context[@name] = @value.eval(context)
end
end
# Setting the value of a local variable.
class SetLocalNode < Awesome
attr_accessor :name, :value, :is_end
def initialize(name, value, is_end)
@name = name
@value = value
@is_end = is_end
end
def eval(context)
context.locals[@name] = @value.eval(context)
end
end
# Method definition.
class DefNode < Awesome
attr_accessor :name, :params, :body
def initialize(name, params, body)
@name = name
@params = params
@body = body
end
def eval(context)
context.current_class.awesome_methods[@name] = AwesomeMethod.new(@params, @body)
end
end
# Class definition.
class ClassNode < Awesome
attr_accessor :name, :body
def initialize(name, body)
@name = name
@body = body
end
def eval(context)
# Create the class and put it's value in a constant.
awesome_class = AwesomeClass.new
context[@name] = awesome_class
# Evaluate the body of the class in its context.
@body.eval(Context.new(awesome_class, awesome_class))
awesome_class
end
end
# if-else control structure.
# Look at this node if you want to implement other
# control structures like while, for, loop, etc.
class IfNode < Awesome
attr_accessor :condition, :body, :else_body
def initialize(condition, body, else_body=nil)
@condition = condition
@body = body
@else_body = else_body
end
def eval(context)
# We turn the condition node into a Ruby value
# to use Ruby's "if" control structure.
if @condition.eval(context).ruby_value
@body.eval(context)
elsif @else_body
@else_body.eval(context)
end
end
end