-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.rb
171 lines (135 loc) · 3.37 KB
/
parser.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
require_relative './parser_util'
class Parser < ParserUtil
rule :program do
match :eof do
[]
end
match :function_definition, :program do
@program.unshift @function_definition
end
end
rule :function_definition do
match :type, :iden, :formal_params, :block do
[:define_func, @type[1], @iden[1], @formal_params, @block]
end
end
rule :formal_params do
match '(', ')' do
[]
end
match ')' do
[]
end
match ',', :type, :iden, :formal_params do
@formal_params.unshift [@type[1], @iden[1]]
end
match '(', :type, :iden, :formal_params do
@formal_params.unshift [@type[1], @iden[1]]
end
end
rule :variable_definition do
match :type, :iden, :assignment_operator, :expression do
[:define_var, @type[1], @iden[1], @expression]
end
match :type, :iden do
[:define_var, @type[1], @iden[1]]
end
end
rule :type do
match :iden do
[:type, @iden[1]]
end
end
rule :block do
match '}' do
[]
end
match '{', :statement, :block do
@block.unshift @statement
end
match :statement, :block do
@block.unshift @statement
end
end
rule :statement do
match :return, :expression, ';' do
[:return, @expression]
end
match :if, '(', :expression, ')', :block, :else, :block do
[:if, @expression, @matched[4], @matched[6]]
end
match :if, '(', :expression, ')', :block do
[:if, @expression, @block]
end
match :for, '(', :expression, ';', :expression, ';', :expression, ')', :block do
[:for, @matched[2], @matched[4], @matched[6], @block]
end
match :while, '(', :expression, ')', :block do
[:while, @expression, @block]
end
match :variable_definition, ';' do
@variable_definition
end
match :expression, ';' do
@expression
end
end
rule :expression do
match :assignment_expression do
@assignment_expression
end
end
rule :factor do
match '(', :expression, ')' do
@expression
end
match :int do
[:int, @int[1].to_i]
end
match :function_call do
@function_call
end
match :iden, :postfix_operator do
case @postfix_operator[1]
when '++'
[:inc, @iden[1]]
when '--'
[:dec, @iden[1]]
else
raise "unknown postfix_operator #{@postfix_operator[1]}"
end
end
match :iden do
[:get, @iden[1]]
end
end
rule :function_call do
match :iden, :actual_params do
[:call, @iden[1], @actual_params]
end
end
rule :actual_params do
match '(', ')' do
[]
end
match ')' do
[]
end
match '(', :expression, :actual_params do
@actual_params.unshift @expression
end
match ',', :expression, :actual_params do
@actual_params.unshift @expression
end
end
binary_operation :assignment_expression, :assignment_operator, :relational_expression do |operator, left, right|
if match = operator.match(/(.)=/)
[:assign, left[1], [:call, match[1], [left, right]]]
else
[:assign, left[1], right]
end
end
binary_operation :relational_expression, :relational_operator, :additive_expression
binary_operation :additive_expression, :additive_operator, :multiplicative_expression
binary_operation :multiplicative_expression, :multiplicative_operator, :factor
end