/
interpreter.rb
98 lines (71 loc) · 1.38 KB
/
interpreter.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
require "parser"
require "runtime"
class Interpreter
def initialize
@parser = Parser.new
end
def eval(code)
@parser.parse(code).eval(RootContext)
end
end
class Nodes
# This method is the "interpreter" part of our language. All nodes know how to eval
# itself and returns the result of its evaluation by implementing the "eval" method.
# The "context" variable is the environment in which the node is evaluated (local
# variables, current class, etc.).
def eval(context)
end
end
class NumberNode
def eval(context)
end
end
class StringNode
def eval(context)
end
end
class TrueNode
def eval(context)
end
end
class FalseNode
def eval(context)
end
end
class NilNode
def eval(context)
end
end
class SetLocalNode
def eval(context)
end
end
class GetLocalNode
def eval(context)
end
end
class GetConstantNode
def eval(context)
end
end
class CallNode
def eval(context)
end
end
class DefNode
def eval(context)
end
end
class ClassNode
def eval(context)
end
end
class IfNode
def eval(context)
### Exercise
# Here you have access to:
# condition_node: condition node that will determine if the body should be executed
# body_node: node to be executed if the condition is true
# else_body_node: node to be executed if the condition is false
end
end