sardonyx 0.1.82 → 0.1.83
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 +12 -0
- data/lib/sdx/compiler/parser.rb +1 -1
- data/lib/sdx/vm/datatypes.rb +117 -5
- data/lib/sdx/vm/vm.rb +77 -23
- 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: 4cc308366fad5b10c92dec85090d721e7e609d461aec60e7cf8f319a5a542a0e
|
|
4
|
+
data.tar.gz: 033f7e434ffae2192f194ef1a7933c52f400be53ef4bdeb9b7719afecd56dfbb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5ecf245af7c547d02cefe94b2d2bd0279051e14977c4f4b4e5ee2c156fce6cd3f3407fb2474dca295fdaf6d08704bd3065ff28624700dccd40c0b679b89bd05c
|
|
7
|
+
data.tar.gz: 2575dc2a542a141742645687f0c4bb0e93995760d621295be3b6fbd9907159221627c6ae622b33ec14db57f4335b1e0b8022578e8c71c241b7986919ffacbff3
|
data/bin/sdx
CHANGED
|
@@ -72,6 +72,18 @@ module Compiler
|
|
|
72
72
|
bc += "\x27"
|
|
73
73
|
when "^"
|
|
74
74
|
bc += "\x28"
|
|
75
|
+
when "=="
|
|
76
|
+
bc += "\x34"
|
|
77
|
+
when "!="
|
|
78
|
+
bc += "\x35"
|
|
79
|
+
when "<"
|
|
80
|
+
bc += "\x36"
|
|
81
|
+
when ">"
|
|
82
|
+
bc += "\x37"
|
|
83
|
+
when "<="
|
|
84
|
+
bc += "\x38"
|
|
85
|
+
when ">="
|
|
86
|
+
bc += "\x39"
|
|
75
87
|
end
|
|
76
88
|
when :if
|
|
77
89
|
bc += self.encode_node node.value
|
data/lib/sdx/compiler/parser.rb
CHANGED
data/lib/sdx/vm/datatypes.rb
CHANGED
|
@@ -58,6 +58,12 @@ class Bool < DataType
|
|
|
58
58
|
end)),
|
|
59
59
|
"__as_code_string" => (NativeFn.new 0, (Proc.new do
|
|
60
60
|
as_string
|
|
61
|
+
end)),
|
|
62
|
+
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
63
|
+
Bool.new @internal == other[0].internal
|
|
64
|
+
end)),
|
|
65
|
+
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
66
|
+
Bool.new @internal != other[0].internal
|
|
61
67
|
end))
|
|
62
68
|
}
|
|
63
69
|
end
|
|
@@ -99,6 +105,24 @@ class Int < DataType
|
|
|
99
105
|
end)),
|
|
100
106
|
"__pow" => (NativeFnInternal.new (Proc.new do |other|
|
|
101
107
|
pow other[0]
|
|
108
|
+
end)),
|
|
109
|
+
"__lt" => (NativeFnInternal.new (Proc.new do |other|
|
|
110
|
+
lt other[0]
|
|
111
|
+
end)),
|
|
112
|
+
"__gt" => (NativeFnInternal.new (Proc.new do |other|
|
|
113
|
+
gt other[0]
|
|
114
|
+
end)),
|
|
115
|
+
"__le" => (NativeFnInternal.new (Proc.new do |other|
|
|
116
|
+
le other[0]
|
|
117
|
+
end)),
|
|
118
|
+
"__ge" => (NativeFnInternal.new (Proc.new do |other|
|
|
119
|
+
ge other[0]
|
|
120
|
+
end)),
|
|
121
|
+
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
122
|
+
Bool.new @internal == other[0].internal
|
|
123
|
+
end)),
|
|
124
|
+
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
125
|
+
Bool.new @internal != other[0].internal
|
|
102
126
|
end))
|
|
103
127
|
}
|
|
104
128
|
end
|
|
@@ -134,6 +158,22 @@ class Int < DataType
|
|
|
134
158
|
def pow(other)
|
|
135
159
|
Int.new @internal ** other.internal
|
|
136
160
|
end
|
|
161
|
+
|
|
162
|
+
def lt(other)
|
|
163
|
+
Bool.new @internal < other.internal
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def gt(other)
|
|
167
|
+
Bool.new @internal > other.internal
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def le(other)
|
|
171
|
+
Bool.new @internal <= other.internal
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def ge(other)
|
|
175
|
+
Bool.new @internal >= other.internal
|
|
176
|
+
end
|
|
137
177
|
end
|
|
138
178
|
|
|
139
179
|
class Str < DataType
|
|
@@ -153,6 +193,12 @@ class Str < DataType
|
|
|
153
193
|
end)),
|
|
154
194
|
"__mul" => (NativeFnInternal.new (Proc.new do |other|
|
|
155
195
|
mul other[0]
|
|
196
|
+
end)),
|
|
197
|
+
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
198
|
+
Bool.new @internal == other[0].internal
|
|
199
|
+
end)),
|
|
200
|
+
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
201
|
+
Bool.new @internal != other[0].internal
|
|
156
202
|
end))
|
|
157
203
|
}
|
|
158
204
|
end
|
|
@@ -206,6 +252,24 @@ class Num < DataType
|
|
|
206
252
|
end)),
|
|
207
253
|
"__pow" => (NativeFnInternal.new (Proc.new do |other|
|
|
208
254
|
pow other[0]
|
|
255
|
+
end)),
|
|
256
|
+
"__lt" => (NativeFnInternal.new (Proc.new do |other|
|
|
257
|
+
lt other[0]
|
|
258
|
+
end)),
|
|
259
|
+
"__gt" => (NativeFnInternal.new (Proc.new do |other|
|
|
260
|
+
gt other[0]
|
|
261
|
+
end)),
|
|
262
|
+
"__le" => (NativeFnInternal.new (Proc.new do |other|
|
|
263
|
+
le other[0]
|
|
264
|
+
end)),
|
|
265
|
+
"__ge" => (NativeFnInternal.new (Proc.new do |other|
|
|
266
|
+
ge other[0]
|
|
267
|
+
end)),
|
|
268
|
+
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
269
|
+
Bool.new @internal == other[0].internal
|
|
270
|
+
end)),
|
|
271
|
+
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
272
|
+
Bool.new @internal != other[0].internal
|
|
209
273
|
end))
|
|
210
274
|
}
|
|
211
275
|
end
|
|
@@ -241,6 +305,22 @@ class Num < DataType
|
|
|
241
305
|
def pow(other)
|
|
242
306
|
Num.new @internal ** other.internal
|
|
243
307
|
end
|
|
308
|
+
|
|
309
|
+
def lt(other)
|
|
310
|
+
Bool.new @internal < other.internal
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
def gt(other)
|
|
314
|
+
Bool.new @internal > other.internal
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def le(other)
|
|
318
|
+
Bool.new @internal <= other.internal
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def ge(other)
|
|
322
|
+
Bool.new @internal >= other.internal
|
|
323
|
+
end
|
|
244
324
|
end
|
|
245
325
|
|
|
246
326
|
class Nil < DataType
|
|
@@ -249,6 +329,12 @@ class Nil < DataType
|
|
|
249
329
|
@fields = {
|
|
250
330
|
"__as_bool" => (NativeFnInternal.new (Proc.new do
|
|
251
331
|
Bool.new false
|
|
332
|
+
end)),
|
|
333
|
+
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
334
|
+
Bool.new @internal == other[0].internal
|
|
335
|
+
end)),
|
|
336
|
+
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
337
|
+
Bool.new @internal != other[0].internal
|
|
252
338
|
end))
|
|
253
339
|
}
|
|
254
340
|
end
|
|
@@ -280,11 +366,18 @@ class List < DataType
|
|
|
280
366
|
"__arity" => (Int.new 1),
|
|
281
367
|
"__call" => (NativeFnInternal.new (Proc.new do |args, scope|
|
|
282
368
|
@internal[args[0].value.internal]
|
|
369
|
+
end)),
|
|
370
|
+
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
371
|
+
Bool.new @internal == other[0].internal
|
|
372
|
+
end)),
|
|
373
|
+
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
374
|
+
Bool.new @internal != other[0].internal
|
|
283
375
|
end))
|
|
284
376
|
}
|
|
285
377
|
end
|
|
286
378
|
|
|
287
379
|
def as_string
|
|
380
|
+
s = ""
|
|
288
381
|
@internal.each do |item|
|
|
289
382
|
s += (stringify item) + ", "
|
|
290
383
|
end
|
|
@@ -294,6 +387,7 @@ class List < DataType
|
|
|
294
387
|
end
|
|
295
388
|
|
|
296
389
|
def as_code_string
|
|
390
|
+
s = ""
|
|
297
391
|
@internal.each do |item|
|
|
298
392
|
s += (codify item) + ", "
|
|
299
393
|
end
|
|
@@ -312,12 +406,12 @@ class List < DataType
|
|
|
312
406
|
if val
|
|
313
407
|
return val
|
|
314
408
|
else
|
|
315
|
-
return Variable.new Nil.new
|
|
409
|
+
return Variable.new Nil.new, :nil, @internal[0].scope
|
|
316
410
|
end
|
|
317
411
|
end
|
|
318
412
|
|
|
319
413
|
def add(other)
|
|
320
|
-
return List.new [*@internal, other]
|
|
414
|
+
return List.new [*@internal, (Variable.new other, (get_type other), @internal[0].scope)]
|
|
321
415
|
end
|
|
322
416
|
|
|
323
417
|
def mul(other)
|
|
@@ -359,7 +453,13 @@ class Function < DataType
|
|
|
359
453
|
"__call" => (NativeFnInternal.new (lambda do |args, scope|
|
|
360
454
|
call args, scope
|
|
361
455
|
end)),
|
|
362
|
-
"__arity" => (Int.new args.size)
|
|
456
|
+
"__arity" => (Int.new args.size),
|
|
457
|
+
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
458
|
+
Bool.new @internal == other[0].internal
|
|
459
|
+
end)),
|
|
460
|
+
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
461
|
+
Bool.new @internal != other[0].internal
|
|
462
|
+
end))
|
|
363
463
|
}
|
|
364
464
|
end
|
|
365
465
|
|
|
@@ -385,7 +485,13 @@ class Block < DataType
|
|
|
385
485
|
"__call" => (NativeFnInternal.new (Proc.new do |args, scope|
|
|
386
486
|
call args, scope
|
|
387
487
|
end)),
|
|
388
|
-
"__arity" => (Int.new 1)
|
|
488
|
+
"__arity" => (Int.new 1),
|
|
489
|
+
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
490
|
+
Bool.new @internal == other[0].internal
|
|
491
|
+
end)),
|
|
492
|
+
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
493
|
+
Bool.new @internal != other[0].internal
|
|
494
|
+
end))
|
|
389
495
|
}
|
|
390
496
|
end
|
|
391
497
|
|
|
@@ -412,7 +518,13 @@ class Obj < DataType
|
|
|
412
518
|
"__new" => (NativeFnInternal.new (Proc.new do |args, scope|
|
|
413
519
|
_new args, scope
|
|
414
520
|
end)),
|
|
415
|
-
"__arity" => (Int.new args.size)
|
|
521
|
+
"__arity" => (Int.new args.size),
|
|
522
|
+
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
523
|
+
Bool.new @internal == other[0].internal
|
|
524
|
+
end)),
|
|
525
|
+
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
526
|
+
Bool.new @internal != other[0].internal
|
|
527
|
+
end))
|
|
416
528
|
}
|
|
417
529
|
end
|
|
418
530
|
|
data/lib/sdx/vm/vm.rb
CHANGED
|
@@ -2,22 +2,6 @@ require 'sdx/vm/variables'
|
|
|
2
2
|
require 'sdx/vm/datatypes'
|
|
3
3
|
require 'sdx/vm/scope'
|
|
4
4
|
|
|
5
|
-
def stringify(val)
|
|
6
|
-
if val.value.fields["__as_string"]
|
|
7
|
-
(val.value.fields["__as_string"].call).internal
|
|
8
|
-
else
|
|
9
|
-
val.value.to_s
|
|
10
|
-
end
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def codify(val)
|
|
14
|
-
if val.value.fields["__as_code_string"]
|
|
15
|
-
(val.value.fields["__as_code_string"].call).internal
|
|
16
|
-
else
|
|
17
|
-
val.value.pretty_inspect
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
5
|
class VM
|
|
22
6
|
attr_accessor :bc_io
|
|
23
7
|
|
|
@@ -28,6 +12,22 @@ class VM
|
|
|
28
12
|
true
|
|
29
13
|
end
|
|
30
14
|
end
|
|
15
|
+
|
|
16
|
+
def stringify(val)
|
|
17
|
+
if val.value.fields["__as_string"]
|
|
18
|
+
(call val.value.fields["__as_string"], [], val.scope).internal
|
|
19
|
+
else
|
|
20
|
+
val.value.to_s
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def codify(val)
|
|
25
|
+
if val.value.fields["__as_code_string"]
|
|
26
|
+
(call val.value.fields["__as_code_string"], [], val.scope).internal
|
|
27
|
+
else
|
|
28
|
+
val.value.pretty_inspect
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
31
|
|
|
32
32
|
def call(val, *args)
|
|
33
33
|
if val.respond_to? :value and val.value.respond_to? :fields
|
|
@@ -150,7 +150,13 @@ class VM
|
|
|
150
150
|
0x30 => :object,
|
|
151
151
|
0x31 => :new,
|
|
152
152
|
0x32 => :block,
|
|
153
|
-
0x33 => :end
|
|
153
|
+
0x33 => :end,
|
|
154
|
+
0x34 => :eq,
|
|
155
|
+
0x35 => :ne,
|
|
156
|
+
0x36 => :lt,
|
|
157
|
+
0x37 => :gt,
|
|
158
|
+
0x38 => :le,
|
|
159
|
+
0x39 => :ge
|
|
154
160
|
}
|
|
155
161
|
bytes = []
|
|
156
162
|
begin
|
|
@@ -279,7 +285,7 @@ class VM
|
|
|
279
285
|
res = (call a.value.fields["__add"], b.value)
|
|
280
286
|
push_to_stack (to_var res)
|
|
281
287
|
else
|
|
282
|
-
error "Cannot
|
|
288
|
+
error "Cannot use + on #{a.type}"
|
|
283
289
|
end
|
|
284
290
|
when :sub
|
|
285
291
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -287,7 +293,7 @@ class VM
|
|
|
287
293
|
res = (call a.value.fields["__sub"], b.value)
|
|
288
294
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
289
295
|
else
|
|
290
|
-
error "Cannot
|
|
296
|
+
error "Cannot use - on #{a.type}"
|
|
291
297
|
end
|
|
292
298
|
when :mul
|
|
293
299
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -295,7 +301,7 @@ class VM
|
|
|
295
301
|
res = (call a.value.fields["__mul"], b.value)
|
|
296
302
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
297
303
|
else
|
|
298
|
-
error "Cannot
|
|
304
|
+
error "Cannot use * on #{a.type}"
|
|
299
305
|
end
|
|
300
306
|
when :div
|
|
301
307
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -303,7 +309,7 @@ class VM
|
|
|
303
309
|
res = (call a.value.fields["__div"], b.value)
|
|
304
310
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
305
311
|
else
|
|
306
|
-
error "Cannot
|
|
312
|
+
error "Cannot use / on #{a.type}"
|
|
307
313
|
end
|
|
308
314
|
when :mod
|
|
309
315
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -311,7 +317,7 @@ class VM
|
|
|
311
317
|
res = (call a.value.fields["__mod"], b.value)
|
|
312
318
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
313
319
|
else
|
|
314
|
-
error "Cannot
|
|
320
|
+
error "Cannot use % on #{a.type}"
|
|
315
321
|
end
|
|
316
322
|
when :pow
|
|
317
323
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -319,7 +325,55 @@ class VM
|
|
|
319
325
|
res = (call a.value.fields["__pow"], b.value)
|
|
320
326
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
321
327
|
else
|
|
322
|
-
error "Cannot
|
|
328
|
+
error "Cannot use ^ on #{a.type}"
|
|
329
|
+
end
|
|
330
|
+
when :eq
|
|
331
|
+
b, a = pop_from_stack, pop_from_stack
|
|
332
|
+
if a.value.fields["__eq"]
|
|
333
|
+
res = (call a.value.fields["__eq"], b.value)
|
|
334
|
+
push_to_stack (Variable.new res, (get_type res), @global)
|
|
335
|
+
else
|
|
336
|
+
error "Cannot use == on #{a.type}"
|
|
337
|
+
end
|
|
338
|
+
when :ne
|
|
339
|
+
b, a = pop_from_stack, pop_from_stack
|
|
340
|
+
if a.value.fields["__neq"]
|
|
341
|
+
res = (call a.value.fields["__neq"], b.value)
|
|
342
|
+
push_to_stack (Variable.new res, (get_type res), @global)
|
|
343
|
+
else
|
|
344
|
+
error "Cannot use != on #{a.type}"
|
|
345
|
+
end
|
|
346
|
+
when :lt
|
|
347
|
+
b, a = pop_from_stack, pop_from_stack
|
|
348
|
+
if a.value.fields["__lt"]
|
|
349
|
+
res = (call a.value.fields["__lt"], b.value)
|
|
350
|
+
push_to_stack (Variable.new res, (get_type res), @global)
|
|
351
|
+
else
|
|
352
|
+
error "Cannot use < on #{a.type}"
|
|
353
|
+
end
|
|
354
|
+
when :gt
|
|
355
|
+
b, a = pop_from_stack, pop_from_stack
|
|
356
|
+
if a.value.fields["__gt"]
|
|
357
|
+
res = (call a.value.fields["__gt"], b.value)
|
|
358
|
+
push_to_stack (Variable.new res, (get_type res), @global)
|
|
359
|
+
else
|
|
360
|
+
error "Cannot use > on #{a.type}"
|
|
361
|
+
end
|
|
362
|
+
when :le
|
|
363
|
+
b, a = pop_from_stack, pop_from_stack
|
|
364
|
+
if a.value.fields["__le"]
|
|
365
|
+
res = (call a.value.fields["__le"], b.value)
|
|
366
|
+
push_to_stack (Variable.new res, (get_type res), @global)
|
|
367
|
+
else
|
|
368
|
+
error "Cannot use <= on #{a.type}"
|
|
369
|
+
end
|
|
370
|
+
when :ge
|
|
371
|
+
b, a = pop_from_stack, pop_from_stack
|
|
372
|
+
if a.value.fields["__ge"]
|
|
373
|
+
res = (call a.value.fields["__ge"], b.value)
|
|
374
|
+
push_to_stack (Variable.new res, (get_type res), @global)
|
|
375
|
+
else
|
|
376
|
+
error "Cannot use >= on #{a.type}"
|
|
323
377
|
end
|
|
324
378
|
when :jmpi
|
|
325
379
|
val = pop_from_stack
|