-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshlParse.rb
executable file
·403 lines (346 loc) · 11.5 KB
/
shlParse.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#! /usr/bin/ruby
# coding: utf-8
require_relative './rdparse'
require_relative './nodes'
require_relative './helpers'
class SHLParse
def initialize
@shlp = Parser.new('shorthand language') do
# LEXER
token(/\s+/)
token(/#>[^<]*<#/) # start-stop comment
token(/#.*$/) # single line comment
token(/\d+\.\d+/) { |m| m.to_f } # float
token(/\d+/) { |m| m.to_i } # int
token(/"[^"]*"/) { |m| m } # strings
token(/[\wÅÄÖåäö][\w\d_åäöÅÄÖ]*/) { |m| m } # identifiers
token(/:[ifsahb]/) { |m| m } # type assignments
token(/~ei|~[iewf]/) { |m| m } # if / loops
token(/\!->|->\!/) { |m| m } # interrupt keywords
token(/==|<=|>=|!=|\*\*|\/\/|<-|->|\+\+|--|&&|\|\|/) { |m| m }
token(/./) { |m| m } # symbol
# PARSER
start :begin do
match(:stmt_list) { |sl| SHLProgramNode.new(sl) }
end
rule :stmt_list do
match(:stmt, :stmt_list) { |s, sl| [s].concat(sl) }
match(:stmt) { |s| [s] }
end
rule :stmt do
match(:expr, ';') { |s, _| s }
match(:if_stmt)
match(:for_stmt)
match(:while_stmt)
match(:class_def)
match(:function_def)
match(:return, ';')
match('!->', ';') { InterruptNode.new(:continue) } # continue
match('->!', ';') { InterruptNode.new(:break) } # break
end
rule :expr do
match(:assignment)
match(:conversion)
match(:bool_expr)
match(:comparison)
match(:expr_call)
match(:arith_expr)
match(:identifier)
match(:type)
match('!', :expr) { |_, b| NegationNode.new(b) }
match('(', :expr, ')')
end
rule :unary_expr do
match(:unary_op, :identifier) do |op, expr|
UnaryExprNode.new(expr, op, false)
end
match(:unary_op, '^', :identifier) do |op, _, expr|
UnaryExprNode.new(expr, op, false, true)
end
match(:identifier, :unary_op) do |expr, op|
UnaryExprNode.new(expr, op, true)
end
match('^', :identifier, :unary_op) do |_, expr, op|
UnaryExprNode.new(expr, op, true, true)
end
end
rule :conversion do
match(:identifier, '->', :type_dec) do |i, _, t|
ConversionNode.new(i, t)
end
end
rule :bool_expr do
match(:bool_expr, '&&', :expr) { |a, _, b| BoolExprNode.new(a, b, '&&') }
match(:bool_expr, '||', :expr) { |a, _, b| BoolExprNode.new(a, b, '||') }
match(:bool)
end
rule :expr_call do
match(:identifier, '(', :arg_list, ')') { |i, _, al| CallNode.new(i, al)}
match(:identifier, '(', ')') { |i| CallNode.new(i, []) }
end
rule :if_stmt do
match('~i', :expr, :cond_body, :elseif_list, '~e', :cond_body) \
do |_, i_cond, i_body, elseifs, _, e_body|
if_statement_handler(i_body, i_cond, elseifs, e_body)
end
match('~i', :expr, :cond_body, :elseif_list) \
do |_, i_cond, i_body, elseifs|
if_statement_handler(i_body, i_cond, elseifs)
end
match('~i', :expr, :cond_body, '~e', :cond_body) \
do |_, i_cond, i_body, _, e_body|
if_statement_handler(i_body, i_cond, [], e_body)
end
match('~i', :expr, :cond_body) { |_, c, b| if_statement_handler(b, c) }
end
rule :elseif_list do
match(:elseif_list, :elseif) { |list, ei| [ei].concat(list) }
match(:elseif) { |a| [a] }
end
rule :elseif do
match('~ei', :expr, :cond_body) do |_, c, b|
IfNode.new(BlockNode.new(b), c)
end
end
rule :for_stmt do
# No version with only assignment exists, as it is impossible
# to tell whether an assignment is meant to be just an assigment
# or the condition
# All specified
match('~f', :assignment, ';', :expr, ';', :expr, :cond_body) \
do |_, a, _, c, _, i, body|
for_statement_handler(body, assignment: a, cond: c, inc: i)
end
# Assignment and Condition specified
match('~f', :assignment, ';', :expr, :cond_body) do
|_, a, _, c, body|
for_statement_handler(body, assignment: a, cond: c)
end
# Condition and Incement specified
match('~f', :expr, ';', :expr, :cond_body) do |_, c, _, i, b|
for_statement_handler(b, cond: c, inc: i)
end
# Assignment and Incement specified
match('~f', :assignment, ';', ';', :expr, :cond_body) do
|_, a, _, _, i, body|
for_statement_handler(body, assignment: a, inc: i)
end
# Condition specified
match('~f', :expr, :cond_body) do |_, c, body|
for_statement_handler(body, cond: c)
end
# None specified
match('~f', :cond_body) do |_, body|
for_statement_handler(body)
end
end
rule :while_stmt do
match('~w', :expr, :cond_body) do |_, e, c|
WhileNode.new(e, BlockNode.new(c))
end
match('~w', :cond_body) do |_, c|
WhileNode.new(ConstantNode.new(true), BlockNode.new(c))
end
end
rule :cond_body do
match('{', :stmt_list, '}') { |_, s, _| s }
match(:stmt) { |x| [x] }
end
rule :arg_list do
match(:expr, ',', :arg_list) { |e, _, al| [e].concat(al) }
match(:expr) { |e| [e] }
end
rule :param_list do
match(:identifier, ',', :param_def_list) do |i, _, pdl|
[[i.name, :nv]].concat(pdl)
end
match(:identifier, ',', :param_list) do |i, _, pl|
[[i.name, :nv]].concat(pl)
end
match(:param_def_list)
match(:identifier) { |i| [[i.name, :nv]] }
end
rule :param_def_list do
match(:identifier, '=', :expr, ',', :param_def_list) { |i,_,e,_,pdl| [[i.name,e]].concat(pdl) }
match(:identifier, '=', :expr) { |i, _, e| [[i.name,e]] }
end
rule :class_def do
match('!', :identifier,'(', :param_list, ')', '{', :stmt_list, '}') do |_,i,_,pl,_,_,sl|
CallableDefNode.new(i.name, :class, pl, BlockNode.new(sl))
end
match('!', :identifier, '{', :stmt_list, '}') do |_,i,_,sl|
CallableDefNode.new(i.name, :class, [], BlockNode.new(sl))
end
match('!', :identifier, '(', ')', '{', :stmt_list, '}') do |_, i, _, _, _, sl|
CallableDefNode.new(i.name, :class, [], BlockNode.new(sl))
end
end
rule :function_def do
match('@', :identifier, '(', :param_list, ')', '{', :stmt_list, '}') do |_,i,_,pl,_,_,sl|
CallableDefNode.new(i.name, :func, pl, BlockNode.new(sl))
end
match('@', :identifier, '(', ')', '{', :stmt_list, '}') do |_,i,_,_,_,sl|
CallableDefNode.new(i.name, :func, [], BlockNode.new(sl))
end
match('@', :identifier, '{', :stmt_list, '}') do |_, i, _, sl, _|
CallableDefNode.new(i.name, :func, [], BlockNode.new(sl))
end
end
rule :value do
match(:type)
match(:identifier)
end
rule :identifier do
match(:identifier, '.', :identifier) { |i1, _, i2| MemberNode.new(i1, i2) }
match(:type, '.', :identifier) { |i1, _, i2| MemberNode.new(i1, i2) }
match(:identifier, '[', :value, ']') { |n, _, t, _| BracketCallNode.new(n, t) }
match(:name) { |n| VariableNode.new(n) }
end
rule :name do
match(/[\wÅÄÖåäö][\w\d_åäöÅÄÖ]*/) { |a| a }
end
rule :unary_op do
match('++')
match(/--/)
end
rule :comp_op do
match('==')
match('<=')
match('>=')
match('!=')
match('<')
match('>')
end
rule :comparison do
match(:arith_expr, :comp_op, :arith_expr) do |a, op, b|
ComparisonNode.new(a, b, op)
end
end
rule :type_dec do
match(':i') { ConstantNode.new(0) }
match(':f') { ConstantNode.new(0.0) }
match(':s') { ConstantNode.new('') }
match(':a') { ConstantNode.new([]) }
match(':h') { ConstantNode.new({}) }
match(':b') { ConstantNode.new(false) }
end
rule :arith_op do
match('+')
match('-')
end
rule :arith_expr do
match(:arith_expr, :arith_op, :term) do |a, op, b|
ArithmeticNode.new(a, b, op)
end
match(:term, :arith_op, :term) do |a, op, b|
ArithmeticNode.new(a, b, op)
end
match(:term)
end
rule :term_op do
match('//')
match('*')
match('/')
end
rule :term do
match(:term, :term_op, :pow) do |a, op, b|
ArithmeticNode.new(a, b, op)
end
match(:pow, :term_op, :term) do |a, op, b|
ArithmeticNode.new(a, b, op)
end
match(:pow)
end
rule :pow do
match(:pow, '**', :factor) { |a, _, b| ArithmeticNode.new(a, b, '**') }
match(:factor)
end
rule :factor do
match('(', :arith_expr, ')') { |_, b, _| b }
match('-', :term) { |_, v| UnaryExprNode.new(v, '-', false) }
match(:unary_expr)
match(:type)
match(:expr_call)
match(:identifier)
end
rule :assignment do
match(:expr_assignment)
match(:type_assignment)
end
rule :expr_assignment do
match(:identifier, '=', :expr) { |i, _, e| AssignmentNode.new(i, e) }
match('^', :identifier, '=', :expr) do |_, i, _, e|
AssignmentNode.new(i, e, true)
end
end
rule :type_assignment do
match(:identifier, :type_dec) { |i, td| AssignmentNode.new(i, td) }
end
rule :return do
match('<-', :expr) { |_, e| InterruptNode.new(:return, e) }
match('<-') { InterruptNode.new(:return) }
end
rule :type do
match(:bool)
match(:int)
match(:float)
match(:string)
match(:array)
match(:hash)
match(:nil)
end
rule :hash_arg_list do
match(:hash_arg, ',', :hash_arg_list) { |s, _, l| [s].concat(l) }
match(:hash_arg) { |x| [x] }
end
rule :hash_arg do
match(:value, ':', :value) { |lhs, _, rhs| [lhs, rhs] }
end
rule :hash do
match('{', :hash_arg_list, '}') { |_, h, _| HashNode.new(h) }
match('{', '}') { HashNode.new }
end
rule :array do
match('[', :arg_list, ']') { |_, al| ArrayNode.new(al) }
match('[', ']') { ArrayNode.new([]) }
end
rule :string do
# Remove quotes around string
match(/"[^"]*"/) do |s|
ConstantNode.new(s.length <= 2 ? '' : s[1...-1])
end
end
rule :float do
match(Float) { |f| ConstantNode.new(f) }
end
rule :int do
match(Integer) { |i| ConstantNode.new(i) }
end
rule :bool do
match('true') { ConstantNode.new(true) }
match('false') { ConstantNode.new(false) }
end
rule :nil do
match('nil') { ConstantNode.new(nil) }
end
end
end
def log(state = true)
if state
@shlp.logger.level = Logger::DEBUG
else
@shlp.logger.level = Logger::WARN
end
end
def parse(str)
@shlp.parse str
end
end
if __FILE__ == $PROGRAM_NAME
sp = SHLParse.new
fail 'Usage: shlParse.rb <file>' if ARGV[0].nil?
f = File.read ARGV[0]
sp.log(false)
program = sp.parse f
program.evaluate
end