sardonyx 0.1.84 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/sdx +1 -1
- data/lib/sdx/compiler/compiler.rb +3 -1
- data/lib/sdx/compiler/parser.rb +216 -99
- data/lib/sdx/vm/datatypes.rb +3 -4
- data/lib/sdx/vm/vm.rb +25 -9
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b28cfaca2d8790be84bad3e1f6bffd5ead67aeaa6be749643a68828530b68bbe
|
|
4
|
+
data.tar.gz: 92a7ea6bccf98cf8099fbe8665e20c1a6548c6c7fe4a7c92289ce8dda7ca6393
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 89cde9393ea7e0f22af24ebe35c18a0b414c9dd115df9d378bcc98b4686f246cd550210ef3ed7f417f00e71b1bc866ccdc3611230c2d63379f8bdae93e651ba4
|
|
7
|
+
data.tar.gz: 19dc7e4ccf4700d2158d29812fedfc673ad68fb893c99d9ff98f5bc3d8044b3f0cf10efa9738fbe41931f683e2a276d5f5cf8823509428f9b75540b68f371dcc
|
data/bin/sdx
CHANGED
|
@@ -103,10 +103,12 @@ module Compiler
|
|
|
103
103
|
end
|
|
104
104
|
i += "\x2a#{e.size}\x18"
|
|
105
105
|
end
|
|
106
|
-
bc += "\
|
|
106
|
+
bc += "\x2b#{i.size}\x18" + i
|
|
107
107
|
if e
|
|
108
108
|
bc += e
|
|
109
109
|
end
|
|
110
|
+
when :bool
|
|
111
|
+
bc += "\x21\x12#{node.value}\x18"
|
|
110
112
|
when :name
|
|
111
113
|
bc += "\x20#{node.value}\x18"
|
|
112
114
|
when :nil
|
data/lib/sdx/compiler/parser.rb
CHANGED
|
@@ -10,11 +10,13 @@ module Parser
|
|
|
10
10
|
/\Aobject/ => :object,
|
|
11
11
|
/\Anew/ => :new,
|
|
12
12
|
/\Arequire/ => :require,
|
|
13
|
-
/\A(
|
|
14
|
-
/\A(\+|-|\*|\/|%)?=/ => :eq,
|
|
15
|
-
/\A(\+|-|\*|\/|%)/ => :op,
|
|
16
|
-
/\A-?[0-9]+/ => :number,
|
|
13
|
+
/\A(true|false)/ => :bool,
|
|
17
14
|
/\A-?[0-9]+\.[0-9]+/ => :float,
|
|
15
|
+
/\A-?[0-9]+/ => :number,
|
|
16
|
+
/\A(\+|-)/ => :l1op,
|
|
17
|
+
/\A(\/|\*|%|\^)/ => :l2op,
|
|
18
|
+
/\A(<|>|<=|>=|==|!=)/ => :l1op,
|
|
19
|
+
/\A(\+|-|\*|\/|%)?=/ => :eq,
|
|
18
20
|
/\A"([^"]|\\")*"/ => :string,
|
|
19
21
|
/\Anil/ => :nil,
|
|
20
22
|
/\A\(/ => :lpar,
|
|
@@ -97,6 +99,14 @@ module Parser
|
|
|
97
99
|
end
|
|
98
100
|
end
|
|
99
101
|
|
|
102
|
+
def self.parse_bool(tokens)
|
|
103
|
+
if self.expect tokens, :bool
|
|
104
|
+
[ (Node.new :bool, tokens[0][0], []), 1 ]
|
|
105
|
+
else
|
|
106
|
+
nil
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
100
110
|
def self.parse_float(tokens)
|
|
101
111
|
if self.expect tokens, :float
|
|
102
112
|
[ (Node.new :float, tokens[0][0], []), 1 ]
|
|
@@ -115,13 +125,15 @@ module Parser
|
|
|
115
125
|
|
|
116
126
|
def self.parse_parens(tokens)
|
|
117
127
|
if self.expect tokens, :lpar
|
|
118
|
-
tokens = tokens[1
|
|
119
|
-
|
|
128
|
+
tokens = tokens[1..-1]
|
|
129
|
+
res = self.parse_expr tokens
|
|
130
|
+
unless res
|
|
120
131
|
return nil
|
|
121
132
|
end
|
|
122
133
|
e, part = self.parse_expr tokens
|
|
123
|
-
tokens = tokens[part
|
|
124
|
-
|
|
134
|
+
tokens = tokens[part..-1]
|
|
135
|
+
res = self.expect tokens, :rpar
|
|
136
|
+
unless res
|
|
125
137
|
return nil
|
|
126
138
|
end
|
|
127
139
|
return [e, part + 2]
|
|
@@ -134,7 +146,7 @@ module Parser
|
|
|
134
146
|
unless (self.expect tokens, :lbrack)
|
|
135
147
|
return nil
|
|
136
148
|
end
|
|
137
|
-
tokens = tokens[1
|
|
149
|
+
tokens = tokens[1..-1]
|
|
138
150
|
children = []
|
|
139
151
|
total = 1
|
|
140
152
|
while true
|
|
@@ -142,13 +154,14 @@ module Parser
|
|
|
142
154
|
total += 1
|
|
143
155
|
break
|
|
144
156
|
end
|
|
145
|
-
|
|
157
|
+
res = self.parse_expr tokens
|
|
158
|
+
unless res
|
|
146
159
|
return nil
|
|
147
160
|
end
|
|
148
|
-
e, part =
|
|
161
|
+
e, part = res
|
|
149
162
|
children << e
|
|
150
163
|
total += part
|
|
151
|
-
tokens = tokens[part
|
|
164
|
+
tokens = tokens[part..-1]
|
|
152
165
|
if self.expect tokens, :rbrack
|
|
153
166
|
total += 1
|
|
154
167
|
break
|
|
@@ -157,7 +170,7 @@ module Parser
|
|
|
157
170
|
return nil
|
|
158
171
|
end
|
|
159
172
|
total += 1
|
|
160
|
-
tokens = tokens[1
|
|
173
|
+
tokens = tokens[1..-1]
|
|
161
174
|
end
|
|
162
175
|
[ (Node.new :list, "", children), total ]
|
|
163
176
|
end
|
|
@@ -166,7 +179,7 @@ module Parser
|
|
|
166
179
|
unless (self.expect tokens, :lbrace)
|
|
167
180
|
return nil
|
|
168
181
|
end
|
|
169
|
-
tokens = tokens[1
|
|
182
|
+
tokens = tokens[1..-1]
|
|
170
183
|
children = []
|
|
171
184
|
total = 1
|
|
172
185
|
while true
|
|
@@ -178,7 +191,7 @@ module Parser
|
|
|
178
191
|
if e
|
|
179
192
|
children << e[0]
|
|
180
193
|
total += e[1]
|
|
181
|
-
tokens = tokens[e[1]
|
|
194
|
+
tokens = tokens[e[1]..-1]
|
|
182
195
|
else
|
|
183
196
|
puts "Syntax error at token ", tokens[0]
|
|
184
197
|
Kernel.exit 1
|
|
@@ -189,41 +202,52 @@ module Parser
|
|
|
189
202
|
end
|
|
190
203
|
|
|
191
204
|
def self.parse_literal(tokens)
|
|
192
|
-
(self.parse_block tokens) ||
|
|
205
|
+
(self.parse_block tokens) ||
|
|
206
|
+
(self.parse_bool tokens) ||
|
|
207
|
+
(self.parse_float tokens) ||
|
|
208
|
+
(self.parse_name tokens) ||
|
|
209
|
+
(self.parse_number tokens) ||
|
|
210
|
+
(self.parse_list tokens) ||
|
|
211
|
+
(self.parse_string tokens) ||
|
|
212
|
+
(self.parse_nil tokens) ||
|
|
213
|
+
(self.parse_parens tokens)
|
|
193
214
|
end
|
|
194
215
|
|
|
195
216
|
def self.parse_call(tokens)
|
|
196
|
-
|
|
217
|
+
res = (self.parse_literal tokens)
|
|
218
|
+
unless res
|
|
197
219
|
return nil
|
|
198
220
|
end
|
|
199
|
-
callee =
|
|
221
|
+
callee = res
|
|
200
222
|
total = callee[1]
|
|
201
|
-
tokens = tokens[total
|
|
223
|
+
tokens = tokens[total..-1]
|
|
202
224
|
callee = callee[0]
|
|
203
225
|
if self.expect tokens, :lpar
|
|
204
226
|
args = []
|
|
205
|
-
tokens = tokens[1
|
|
227
|
+
tokens = tokens[1..-1]
|
|
206
228
|
total += 1
|
|
207
229
|
if self.expect tokens, :rpar
|
|
208
230
|
return [ (Node.new :call, callee, args), total + 1 ]
|
|
209
231
|
end
|
|
210
232
|
while true
|
|
211
|
-
|
|
233
|
+
res = (self.parse_expr tokens)
|
|
234
|
+
unless res
|
|
212
235
|
return nil
|
|
213
236
|
end
|
|
214
|
-
arg, part =
|
|
237
|
+
arg, part = res
|
|
215
238
|
total += part
|
|
216
|
-
tokens = tokens[part
|
|
239
|
+
tokens = tokens[part..-1]
|
|
217
240
|
args << arg
|
|
218
|
-
total += 1
|
|
219
241
|
if self.expect tokens, :rpar
|
|
220
|
-
tokens = tokens[1
|
|
242
|
+
tokens = tokens[1..-1]
|
|
243
|
+
total += 1
|
|
221
244
|
break
|
|
222
245
|
end
|
|
223
246
|
unless (self.expect tokens, :comma)
|
|
224
247
|
return nil
|
|
225
248
|
end
|
|
226
|
-
|
|
249
|
+
total += 1
|
|
250
|
+
tokens = tokens[1..-1]
|
|
227
251
|
end
|
|
228
252
|
return [ (Node.new :call, callee, args), total ]
|
|
229
253
|
else
|
|
@@ -236,36 +260,38 @@ module Parser
|
|
|
236
260
|
return nil
|
|
237
261
|
end
|
|
238
262
|
total = 1
|
|
239
|
-
tokens = tokens[1
|
|
263
|
+
tokens = tokens[1..-1]
|
|
240
264
|
if self.lookahead tokens, :lpar, 1
|
|
241
|
-
|
|
265
|
+
res = (self.parse_literal tokens)
|
|
266
|
+
unless res
|
|
242
267
|
return nil
|
|
243
268
|
end
|
|
244
|
-
callee =
|
|
269
|
+
callee = res
|
|
245
270
|
callee = callee[0]
|
|
246
271
|
args = []
|
|
247
|
-
tokens = tokens[2
|
|
272
|
+
tokens = tokens[2..-1]
|
|
248
273
|
total += 2
|
|
249
274
|
if self.expect tokens, :rpar
|
|
250
275
|
return [ (Node.new :call, callee, args), total + 1 ]
|
|
251
276
|
end
|
|
252
277
|
while true
|
|
253
|
-
|
|
278
|
+
res = (self.parse_expr tokens)
|
|
279
|
+
unless res
|
|
254
280
|
return nil
|
|
255
281
|
end
|
|
256
|
-
arg, part =
|
|
282
|
+
arg, part = res
|
|
257
283
|
total += part
|
|
258
|
-
tokens = tokens[part
|
|
284
|
+
tokens = tokens[part..-1]
|
|
259
285
|
args << arg
|
|
260
286
|
total += 1
|
|
261
287
|
if self.expect tokens, :rpar
|
|
262
|
-
tokens = tokens[1
|
|
288
|
+
tokens = tokens[1..-1]
|
|
263
289
|
break
|
|
264
290
|
end
|
|
265
291
|
unless (self.expect tokens, :comma)
|
|
266
292
|
return nil
|
|
267
293
|
end
|
|
268
|
-
tokens = tokens[1
|
|
294
|
+
tokens = tokens[1..-1]
|
|
269
295
|
end
|
|
270
296
|
return [ (Node.new :new, callee, args), total ]
|
|
271
297
|
else
|
|
@@ -278,30 +304,33 @@ module Parser
|
|
|
278
304
|
return nil
|
|
279
305
|
end
|
|
280
306
|
total = 1
|
|
281
|
-
tokens = tokens[1
|
|
282
|
-
|
|
307
|
+
tokens = tokens[1..-1]
|
|
308
|
+
res = self.parse_expr tokens
|
|
309
|
+
unless res
|
|
283
310
|
return nil
|
|
284
311
|
end
|
|
285
|
-
e, part =
|
|
312
|
+
e, part = res
|
|
286
313
|
total += part
|
|
287
|
-
tokens = tokens[part
|
|
288
|
-
|
|
314
|
+
tokens = tokens[part..-1]
|
|
315
|
+
res = self.parse_expr tokens
|
|
316
|
+
unless res
|
|
289
317
|
return nil
|
|
290
318
|
end
|
|
291
|
-
block, part =
|
|
319
|
+
block, part = res
|
|
292
320
|
total += part
|
|
293
|
-
tokens = tokens[part
|
|
321
|
+
tokens = tokens[part..-1]
|
|
294
322
|
el = nil
|
|
295
323
|
if self.expect tokens, :else
|
|
296
324
|
total += 1
|
|
297
|
-
tokens = tokens[1
|
|
298
|
-
|
|
325
|
+
tokens = tokens[1..-1]
|
|
326
|
+
res = self.parse_expr tokens
|
|
327
|
+
unless res
|
|
299
328
|
return nil
|
|
300
329
|
end
|
|
301
|
-
el, part =
|
|
330
|
+
el, part = res
|
|
302
331
|
total += part
|
|
303
332
|
end
|
|
304
|
-
|
|
333
|
+
[ (Node.new :if, e, [block, el]), total ]
|
|
305
334
|
end
|
|
306
335
|
|
|
307
336
|
def self.parse_while(tokens)
|
|
@@ -309,19 +338,21 @@ module Parser
|
|
|
309
338
|
return nil
|
|
310
339
|
end
|
|
311
340
|
total = 1
|
|
312
|
-
tokens = tokens[1
|
|
313
|
-
|
|
341
|
+
tokens = tokens[1..-1]
|
|
342
|
+
res = self.parse_expr tokens
|
|
343
|
+
unless res
|
|
314
344
|
return nil
|
|
315
345
|
end
|
|
316
|
-
e, part =
|
|
346
|
+
e, part = res
|
|
317
347
|
total += part
|
|
318
|
-
tokens = tokens[part
|
|
319
|
-
|
|
348
|
+
tokens = tokens[part..-1]
|
|
349
|
+
res = self.parse_expr tokens
|
|
350
|
+
unless res
|
|
320
351
|
return nil
|
|
321
352
|
end
|
|
322
|
-
block, part =
|
|
353
|
+
block, part = res
|
|
323
354
|
total += part
|
|
324
|
-
|
|
355
|
+
[ (Node.new :while, e, [block]), total ]
|
|
325
356
|
end
|
|
326
357
|
|
|
327
358
|
def self.parse_for(tokens)
|
|
@@ -329,47 +360,120 @@ module Parser
|
|
|
329
360
|
return nil
|
|
330
361
|
end
|
|
331
362
|
total = 1
|
|
332
|
-
tokens = tokens[1
|
|
363
|
+
tokens = tokens[1..-1]
|
|
333
364
|
name = nil
|
|
334
365
|
if self.expect tokens, :name and self.lookahead tokens, :in, 1
|
|
335
366
|
name = tokens[0][0]
|
|
336
367
|
total += 2
|
|
337
|
-
tokens = tokens[2
|
|
368
|
+
tokens = tokens[2..-1]
|
|
338
369
|
end
|
|
339
|
-
|
|
370
|
+
res = self.parse_expr tokens
|
|
371
|
+
unless res
|
|
340
372
|
return nil
|
|
341
373
|
end
|
|
342
|
-
e, part =
|
|
374
|
+
e, part = res
|
|
343
375
|
total += part
|
|
344
|
-
tokens = tokens[part
|
|
345
|
-
|
|
376
|
+
tokens = tokens[part..-1]
|
|
377
|
+
res = self.parse_expr tokens
|
|
378
|
+
unless res
|
|
346
379
|
return nil
|
|
347
380
|
end
|
|
348
|
-
block, part =
|
|
381
|
+
block, part = res
|
|
349
382
|
total += part
|
|
350
|
-
|
|
383
|
+
[ (Node.new :for, e, [name, block]), total ]
|
|
351
384
|
end
|
|
352
385
|
|
|
353
|
-
def self.
|
|
386
|
+
def self.parse_factor(tokens)
|
|
387
|
+
(self.parse_call tokens) ||
|
|
388
|
+
(self.parse_require tokens) ||
|
|
389
|
+
(self.parse_new tokens) ||
|
|
390
|
+
(self.parse_object tokens) ||
|
|
391
|
+
(self.parse_fn tokens) ||
|
|
392
|
+
(self.parse_assign tokens) ||
|
|
393
|
+
(self.parse_literal tokens) ||
|
|
394
|
+
(self.parse_if tokens) ||
|
|
395
|
+
(self.parse_while tokens) ||
|
|
396
|
+
(self.parse_for tokens)
|
|
397
|
+
end
|
|
398
|
+
|
|
399
|
+
def self.parse_term(tokens)
|
|
354
400
|
total = 0
|
|
355
|
-
|
|
401
|
+
res = self.parse_factor tokens
|
|
402
|
+
unless res
|
|
403
|
+
return nil
|
|
404
|
+
end
|
|
405
|
+
lhs, part = res
|
|
406
|
+
total += part
|
|
407
|
+
tokens = tokens[part..-1]
|
|
408
|
+
unless self.expect tokens, :l2op
|
|
409
|
+
return [lhs, part]
|
|
410
|
+
end
|
|
411
|
+
op = tokens[0][0]
|
|
412
|
+
total += 1
|
|
413
|
+
tokens = tokens[1..-1]
|
|
414
|
+
res = self.parse_factor tokens
|
|
415
|
+
unless res
|
|
356
416
|
return nil
|
|
357
417
|
end
|
|
358
|
-
|
|
418
|
+
rhs, part = res
|
|
359
419
|
total += part
|
|
360
|
-
tokens = tokens[part
|
|
361
|
-
|
|
420
|
+
tokens = tokens[part..-1]
|
|
421
|
+
out = (Node.new :op, op, [lhs])
|
|
422
|
+
while self.expect tokens, :l2op
|
|
423
|
+
op = tokens[0][0]
|
|
424
|
+
total += 1
|
|
425
|
+
tokens = tokens[1..-1]
|
|
426
|
+
res = self.parse_term tokens
|
|
427
|
+
unless res
|
|
428
|
+
return nil
|
|
429
|
+
end
|
|
430
|
+
rhs2, part = res
|
|
431
|
+
total += part
|
|
432
|
+
tokens = tokens[part..-1]
|
|
433
|
+
rhs = Node.new :op, op, [rhs, rhs2]
|
|
434
|
+
end
|
|
435
|
+
out.children << rhs
|
|
436
|
+
[out, total]
|
|
437
|
+
end
|
|
438
|
+
|
|
439
|
+
def self.parse_op(tokens)
|
|
440
|
+
total = 0
|
|
441
|
+
res = self.parse_term tokens
|
|
442
|
+
unless res
|
|
362
443
|
return nil
|
|
363
444
|
end
|
|
445
|
+
lhs, part = res
|
|
446
|
+
total += part
|
|
447
|
+
tokens = tokens[part..-1]
|
|
448
|
+
unless self.expect tokens, :l1op
|
|
449
|
+
return [lhs, part]
|
|
450
|
+
end
|
|
364
451
|
op = tokens[0][0]
|
|
365
452
|
total += 1
|
|
366
|
-
tokens = tokens[1
|
|
367
|
-
|
|
453
|
+
tokens = tokens[1..-1]
|
|
454
|
+
res = self.parse_term tokens
|
|
455
|
+
unless res
|
|
368
456
|
return nil
|
|
369
457
|
end
|
|
370
|
-
rhs, part =
|
|
458
|
+
rhs, part = res
|
|
371
459
|
total += part
|
|
372
|
-
|
|
460
|
+
tokens = tokens[part..-1]
|
|
461
|
+
out = (Node.new :op, op, [lhs])
|
|
462
|
+
while self.expect tokens, :l1op
|
|
463
|
+
op = tokens[0][0]
|
|
464
|
+
total += 1
|
|
465
|
+
tokens = tokens[1..-1]
|
|
466
|
+
res = self.parse_term tokens
|
|
467
|
+
unless res
|
|
468
|
+
return nil
|
|
469
|
+
end
|
|
470
|
+
rhs2, part = res
|
|
471
|
+
total += part
|
|
472
|
+
tokens = tokens[part..-1]
|
|
473
|
+
rhs = Node.new :op, op, [rhs, rhs2]
|
|
474
|
+
end
|
|
475
|
+
out.children << rhs
|
|
476
|
+
[out, total]
|
|
373
477
|
end
|
|
374
478
|
|
|
375
479
|
def self.parse_assign(tokens)
|
|
@@ -379,19 +483,20 @@ module Parser
|
|
|
379
483
|
end
|
|
380
484
|
name = tokens[0][0]
|
|
381
485
|
total += 1
|
|
382
|
-
tokens = tokens[1
|
|
486
|
+
tokens = tokens[1..-1]
|
|
383
487
|
unless self.expect tokens, :eq
|
|
384
488
|
return nil
|
|
385
489
|
end
|
|
386
490
|
eq = tokens[0][0]
|
|
387
491
|
total += 1
|
|
388
|
-
tokens = tokens[1
|
|
389
|
-
|
|
492
|
+
tokens = tokens[1..-1]
|
|
493
|
+
res = self.parse_expr tokens
|
|
494
|
+
unless res
|
|
390
495
|
return nil
|
|
391
496
|
end
|
|
392
|
-
rhs, part =
|
|
497
|
+
rhs, part = res
|
|
393
498
|
total += part
|
|
394
|
-
|
|
499
|
+
[ (Node.new :assign, eq, [name, rhs]), total]
|
|
395
500
|
end
|
|
396
501
|
|
|
397
502
|
def self.parse_fn(tokens)
|
|
@@ -399,23 +504,23 @@ module Parser
|
|
|
399
504
|
return nil
|
|
400
505
|
end
|
|
401
506
|
total = 1
|
|
402
|
-
tokens = tokens[1
|
|
507
|
+
tokens = tokens[1..-1]
|
|
403
508
|
unless self.expect tokens, :name
|
|
404
509
|
return nil
|
|
405
510
|
end
|
|
406
511
|
name = tokens[0][0]
|
|
407
512
|
total += 1
|
|
408
|
-
tokens = tokens[1
|
|
513
|
+
tokens = tokens[1..-1]
|
|
409
514
|
unless self.expect tokens, :lpar
|
|
410
515
|
return nil
|
|
411
516
|
end
|
|
412
517
|
total += 1
|
|
413
|
-
tokens = tokens[1
|
|
518
|
+
tokens = tokens[1..-1]
|
|
414
519
|
args = []
|
|
415
520
|
while true
|
|
416
521
|
if self.expect tokens, :rpar
|
|
417
522
|
total += 1
|
|
418
|
-
tokens = tokens[1
|
|
523
|
+
tokens = tokens[1..-1]
|
|
419
524
|
break
|
|
420
525
|
end
|
|
421
526
|
unless self.expect tokens, :name
|
|
@@ -423,24 +528,25 @@ module Parser
|
|
|
423
528
|
end
|
|
424
529
|
args << tokens[0][0]
|
|
425
530
|
total += 1
|
|
426
|
-
tokens = tokens[1
|
|
531
|
+
tokens = tokens[1..-1]
|
|
427
532
|
if self.expect tokens, :rpar
|
|
428
533
|
total += 1
|
|
429
|
-
tokens = tokens[1
|
|
534
|
+
tokens = tokens[1..-1]
|
|
430
535
|
break
|
|
431
536
|
end
|
|
432
537
|
unless self.expect tokens, :comma
|
|
433
538
|
return nil
|
|
434
539
|
end
|
|
435
540
|
total += 1
|
|
436
|
-
tokens = tokens[1
|
|
541
|
+
tokens = tokens[1..-1]
|
|
437
542
|
end
|
|
438
|
-
|
|
543
|
+
res = self.parse_expr tokens
|
|
544
|
+
unless res
|
|
439
545
|
return nil
|
|
440
546
|
end
|
|
441
|
-
body, part =
|
|
547
|
+
body, part = res
|
|
442
548
|
total += part
|
|
443
|
-
|
|
549
|
+
[ (Node.new :fn, name, [args, body]), total ]
|
|
444
550
|
end
|
|
445
551
|
|
|
446
552
|
def self.parse_object(tokens)
|
|
@@ -448,21 +554,21 @@ module Parser
|
|
|
448
554
|
return nil
|
|
449
555
|
end
|
|
450
556
|
total = 1
|
|
451
|
-
tokens = tokens[1
|
|
557
|
+
tokens = tokens[1..-1]
|
|
452
558
|
unless self.expect tokens, :name
|
|
453
559
|
return nil
|
|
454
560
|
end
|
|
455
561
|
name = tokens[0][0]
|
|
456
562
|
total += 1
|
|
457
|
-
tokens = tokens[1
|
|
563
|
+
tokens = tokens[1..-1]
|
|
458
564
|
args = []
|
|
459
565
|
if self.expect tokens, :lpar
|
|
460
566
|
total += 1
|
|
461
|
-
tokens = tokens[1
|
|
567
|
+
tokens = tokens[1..-1]
|
|
462
568
|
while true
|
|
463
569
|
if self.expect tokens, :rpar
|
|
464
570
|
total += 1
|
|
465
|
-
tokens = tokens[1
|
|
571
|
+
tokens = tokens[1..-1]
|
|
466
572
|
break
|
|
467
573
|
end
|
|
468
574
|
unless self.expect tokens, :name
|
|
@@ -470,40 +576,51 @@ module Parser
|
|
|
470
576
|
end
|
|
471
577
|
args << tokens[0][0]
|
|
472
578
|
total += 1
|
|
473
|
-
tokens = tokens[1
|
|
579
|
+
tokens = tokens[1..-1]
|
|
474
580
|
if self.expect tokens, :rpar
|
|
475
581
|
total += 1
|
|
476
|
-
tokens = tokens[1
|
|
582
|
+
tokens = tokens[1..-1]
|
|
477
583
|
break
|
|
478
584
|
end
|
|
479
585
|
unless self.expect tokens, :comma
|
|
480
586
|
return nil
|
|
481
587
|
end
|
|
482
588
|
total += 1
|
|
483
|
-
tokens = tokens[1
|
|
589
|
+
tokens = tokens[1..-1]
|
|
484
590
|
end
|
|
485
591
|
end
|
|
486
|
-
|
|
592
|
+
res = self.parse_expr tokens
|
|
593
|
+
unless res
|
|
487
594
|
return nil
|
|
488
595
|
end
|
|
489
|
-
body, part =
|
|
596
|
+
body, part = res
|
|
490
597
|
total += part
|
|
491
|
-
|
|
598
|
+
[ (Node.new :object, name, [args, body]), total ]
|
|
492
599
|
end
|
|
493
600
|
|
|
494
601
|
def self.parse_require(tokens)
|
|
495
602
|
unless self.expect tokens, :require
|
|
496
603
|
return nil
|
|
497
604
|
end
|
|
498
|
-
tokens = tokens[1
|
|
605
|
+
tokens = tokens[1..-1]
|
|
499
606
|
unless self.expect tokens, :string
|
|
500
607
|
return nil
|
|
501
608
|
end
|
|
502
|
-
|
|
609
|
+
[ (Node.new :require, tokens[0][0][1..-2], []), 2 ]
|
|
503
610
|
end
|
|
504
611
|
|
|
505
612
|
def self.parse_expr(tokens)
|
|
506
|
-
(self.
|
|
613
|
+
(self.parse_op tokens) ||
|
|
614
|
+
(self.parse_call tokens) ||
|
|
615
|
+
(self.parse_require tokens) ||
|
|
616
|
+
(self.parse_new tokens) ||
|
|
617
|
+
(self.parse_object tokens) ||
|
|
618
|
+
(self.parse_fn tokens) ||
|
|
619
|
+
(self.parse_assign tokens) ||
|
|
620
|
+
(self.parse_literal tokens) ||
|
|
621
|
+
(self.parse_if tokens) ||
|
|
622
|
+
(self.parse_while tokens) ||
|
|
623
|
+
(self.parse_for tokens)
|
|
507
624
|
end
|
|
508
625
|
|
|
509
626
|
def self.parse(tokens, path)
|
|
@@ -530,7 +647,7 @@ module Parser
|
|
|
530
647
|
else
|
|
531
648
|
parsed << e[0]
|
|
532
649
|
end
|
|
533
|
-
tokens = tokens[e[1]
|
|
650
|
+
tokens = tokens[e[1]..-1]
|
|
534
651
|
else
|
|
535
652
|
puts "Syntax error at token ", tokens[0][1]
|
|
536
653
|
Kernel.exit 1
|
data/lib/sdx/vm/datatypes.rb
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
require "sdx/vm/vm"
|
|
2
|
-
require "sdx/vm/variables"
|
|
3
1
|
require "stringio"
|
|
4
2
|
|
|
5
3
|
class DataType
|
|
@@ -341,8 +339,9 @@ class Nil < DataType
|
|
|
341
339
|
end
|
|
342
340
|
|
|
343
341
|
class List < DataType
|
|
344
|
-
def initialize(val)
|
|
342
|
+
def initialize(val, scope=nil)
|
|
345
343
|
@internal = val
|
|
344
|
+
@scope = scope
|
|
346
345
|
@pos = 0
|
|
347
346
|
@fields = {
|
|
348
347
|
"__as_string" => (NativeFnInternal.new (Proc.new do
|
|
@@ -411,7 +410,7 @@ class List < DataType
|
|
|
411
410
|
end
|
|
412
411
|
|
|
413
412
|
def add(other)
|
|
414
|
-
return List.new [*@internal, (Variable.new other, (get_type other), @internal[0].scope)]
|
|
413
|
+
return List.new [*@internal, (Variable.new other, (get_type other), @scope || @internal[0].scope)]
|
|
415
414
|
end
|
|
416
415
|
|
|
417
416
|
def mul(other)
|
data/lib/sdx/vm/vm.rb
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
require 'sdx/vm/variables'
|
|
2
|
-
require 'sdx/vm/datatypes'
|
|
3
|
-
require 'sdx/vm/scope'
|
|
1
|
+
require './lib/sdx/vm/variables'
|
|
2
|
+
require './lib/sdx/vm/datatypes'
|
|
3
|
+
require './lib/sdx/vm/scope'
|
|
4
4
|
|
|
5
5
|
def codify(val)
|
|
6
6
|
if val.value.fields["__as_code_string"]
|
|
7
|
-
|
|
7
|
+
if val.value.fields["__as_code_string"].respond_to? :call
|
|
8
|
+
(val.value.fields["__as_code_string"].call).internal
|
|
9
|
+
else
|
|
10
|
+
(val.value.fields["__as_code_string"].fields["__call"].call [], val.scope).internal
|
|
11
|
+
end
|
|
8
12
|
else
|
|
9
13
|
val.value.pretty_inspect
|
|
10
14
|
end
|
|
@@ -14,10 +18,14 @@ class VM
|
|
|
14
18
|
attr_accessor :bc_io
|
|
15
19
|
|
|
16
20
|
def truthy(val)
|
|
21
|
+
case val.value
|
|
22
|
+
when Bool
|
|
23
|
+
return val.value.internal
|
|
24
|
+
end
|
|
17
25
|
if val.value.fields["__as_bool"]
|
|
18
|
-
(val.value.fields["__as_bool"].call).internal
|
|
26
|
+
return (val.value.fields["__as_bool"].call).internal
|
|
19
27
|
else
|
|
20
|
-
true
|
|
28
|
+
return true
|
|
21
29
|
end
|
|
22
30
|
end
|
|
23
31
|
|
|
@@ -51,7 +59,7 @@ class VM
|
|
|
51
59
|
when String
|
|
52
60
|
to_var (Str.new val)
|
|
53
61
|
when Float
|
|
54
|
-
to_var (
|
|
62
|
+
to_var (Num.new val)
|
|
55
63
|
when Array
|
|
56
64
|
to_var (List.new val.map { |v| from_rb v })
|
|
57
65
|
when Nil
|
|
@@ -157,7 +165,8 @@ class VM
|
|
|
157
165
|
0x36 => :lt,
|
|
158
166
|
0x37 => :gt,
|
|
159
167
|
0x38 => :le,
|
|
160
|
-
0x39 => :ge
|
|
168
|
+
0x39 => :ge,
|
|
169
|
+
0x28 => :pow,
|
|
161
170
|
}
|
|
162
171
|
bytes = []
|
|
163
172
|
begin
|
|
@@ -271,12 +280,19 @@ class VM
|
|
|
271
280
|
vals << pop_from_stack
|
|
272
281
|
end
|
|
273
282
|
vals.reverse!
|
|
274
|
-
push_to_stack Variable.new (List.new vals), :list, @global
|
|
283
|
+
push_to_stack Variable.new (List.new vals, @global), :list, @global
|
|
275
284
|
when :block
|
|
276
285
|
size = get_string.to_i
|
|
277
286
|
body =
|
|
278
287
|
((load_bytes size, false).map { |e| e.chr }).join ""
|
|
279
288
|
push_to_stack Variable.new (Block.new body), :block, @global
|
|
289
|
+
when :bool
|
|
290
|
+
val = get_string
|
|
291
|
+
t = {
|
|
292
|
+
"true" => true,
|
|
293
|
+
"false" => false,
|
|
294
|
+
}
|
|
295
|
+
push_to_stack Variable.new (Bool.new t[val]), :bool, @global
|
|
280
296
|
when :nil
|
|
281
297
|
push_to_stack Variable.new (Nil.new), :nil, @global
|
|
282
298
|
end
|