-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.rb
76 lines (63 loc) · 1.2 KB
/
compiler.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
require "parser"
class Compiler
def initialize(generator_class)
@parser = Parser.new
@generator_class = generator_class
end
def compile(code)
generator = @generator_class.new
@parser.parse(code).compile(generator)
generator.assemble
end
end
class Nodes
def compile(generator)
generator.compile_all(nodes)
end
end
class NumberNode
def compile(generator)
generator.number_literal(value)
end
end
class StringNode
def compile(generator)
generator.string_literal(value)
end
end
class TrueNode
def compile(generator)
generator.true_literal
end
end
class FalseNode
def compile(generator)
generator.false_literal
end
end
class NilNode
def compile(generator)
generator.nil_literal
end
end
class CallNode
def compile(generator)
if receiver.nil? && generator.has_local?(method) && arguments.empty?
# Local variable load
generator.get_local(method)
else
# Method call
generator.call(receiver, method, arguments)
end
end
end
class AssignNode
def compile(generator)
generator.set_local(name, value)
end
end
class IfNode
def compile(generator)
generator.if condition, body
end
end