sardonyx 0.1.9 → 0.1.81
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 +2 -2
- data/lib/sdx/compiler/compiler.rb +1 -15
- data/lib/sdx/compiler/parser.rb +23 -130
- data/lib/sdx/vm/datatypes.rb +26 -137
- data/lib/sdx/vm/vm.rb +20 -91
- 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: ae3902bcee8c7322c0ead62c5c9e4f76fed8ff73c0bd326921360ff03f49f252
|
|
4
|
+
data.tar.gz: 587f6306aa1042d1531e5ffb6b9498185252e2db2fb2855449041b557ee34ec3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c42b9e8cc520d5e360dd7d07314030246164034035715019fc975aa00b36e9b1be5d653d1e97380f87c0a1cce46a84a5b2d3d73a66b526a0cac689736fc7f0c4
|
|
7
|
+
data.tar.gz: 6754b493b645962e86f18af568310f6291b3ef00ad8f6d01ef154202c29ef856187f284b85dc1bb0771d601f7e3a5258fde30ecd109a2f68e439ff2a1548ab76
|
data/bin/sdx
CHANGED
|
@@ -15,7 +15,7 @@ if ARGV.size == 1
|
|
|
15
15
|
else
|
|
16
16
|
path = [(File.expand_path Dir.pwd), *(ENV.fetch("SDX_PATH", "").split ":")]
|
|
17
17
|
vm = VM.new StringIO.new ""
|
|
18
|
-
puts "Sardonyx v0.1.
|
|
18
|
+
puts "Sardonyx v0.1.8"
|
|
19
19
|
def exit(_) end
|
|
20
20
|
loop do
|
|
21
21
|
print "> "
|
|
@@ -29,7 +29,7 @@ else
|
|
|
29
29
|
val = vm.stack[-1]
|
|
30
30
|
vm.clear
|
|
31
31
|
if val
|
|
32
|
-
puts
|
|
32
|
+
puts stringify val
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
end
|
|
@@ -72,18 +72,6 @@ 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"
|
|
87
75
|
end
|
|
88
76
|
when :if
|
|
89
77
|
bc += self.encode_node node.value
|
|
@@ -103,12 +91,10 @@ module Compiler
|
|
|
103
91
|
end
|
|
104
92
|
i += "\x2a#{e.size}\x18"
|
|
105
93
|
end
|
|
106
|
-
bc += "\
|
|
94
|
+
bc += "\x29#{i.size}\x18" + i
|
|
107
95
|
if e
|
|
108
96
|
bc += e
|
|
109
97
|
end
|
|
110
|
-
when :bool
|
|
111
|
-
bc += "\x21\x12#{node.value}\x18"
|
|
112
98
|
when :name
|
|
113
99
|
bc += "\x20#{node.value}\x18"
|
|
114
100
|
when :nil
|
data/lib/sdx/compiler/parser.rb
CHANGED
|
@@ -10,13 +10,11 @@ module Parser
|
|
|
10
10
|
/\Aobject/ => :object,
|
|
11
11
|
/\Anew/ => :new,
|
|
12
12
|
/\Arequire/ => :require,
|
|
13
|
-
/\A(
|
|
14
|
-
/\A-?[0-9]+\.[0-9]+/ => :float,
|
|
15
|
-
/\A-?[0-9]+/ => :number,
|
|
16
|
-
/\A(\+|-)/ => :l1op,
|
|
17
|
-
/\A(\/|\*|%|\^)/ => :l2op,
|
|
18
|
-
/\A(<|>|<=|>=|==|!=)/ => :l1op,
|
|
13
|
+
/\A(<|>|<=|>=|==)/ => :op,
|
|
19
14
|
/\A(\+|-|\*|\/|%)?=/ => :eq,
|
|
15
|
+
/\A(\+|-|\*|\/|%)/ => :op,
|
|
16
|
+
/\A-?[0-9]+/ => :number,
|
|
17
|
+
/\A-?[0-9]+\.[0-9]+/ => :float,
|
|
20
18
|
/\A"([^"]|\\")*"/ => :string,
|
|
21
19
|
/\Anil/ => :nil,
|
|
22
20
|
/\A\(/ => :lpar,
|
|
@@ -99,14 +97,6 @@ module Parser
|
|
|
99
97
|
end
|
|
100
98
|
end
|
|
101
99
|
|
|
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
|
-
|
|
110
100
|
def self.parse_float(tokens)
|
|
111
101
|
if self.expect tokens, :float
|
|
112
102
|
[ (Node.new :float, tokens[0][0], []), 1 ]
|
|
@@ -123,23 +113,6 @@ module Parser
|
|
|
123
113
|
end
|
|
124
114
|
end
|
|
125
115
|
|
|
126
|
-
def self.parse_parens(tokens)
|
|
127
|
-
if self.expect tokens, :lpar
|
|
128
|
-
tokens = tokens[1..tokens.size]
|
|
129
|
-
unless self.parse_expr tokens
|
|
130
|
-
return nil
|
|
131
|
-
end
|
|
132
|
-
e, part = self.parse_expr tokens
|
|
133
|
-
tokens = tokens[part..tokens.size]
|
|
134
|
-
unless self.expect tokens, :rpar
|
|
135
|
-
return nil
|
|
136
|
-
end
|
|
137
|
-
return [e, part + 2]
|
|
138
|
-
else
|
|
139
|
-
return nil
|
|
140
|
-
end
|
|
141
|
-
end
|
|
142
|
-
|
|
143
116
|
def self.parse_list(tokens)
|
|
144
117
|
unless (self.expect tokens, :lbrack)
|
|
145
118
|
return nil
|
|
@@ -182,7 +155,7 @@ module Parser
|
|
|
182
155
|
while true
|
|
183
156
|
if self.expect tokens, :rbrace
|
|
184
157
|
total += 1
|
|
185
|
-
|
|
158
|
+
break
|
|
186
159
|
end
|
|
187
160
|
e = self.parse_expr tokens
|
|
188
161
|
if e
|
|
@@ -194,26 +167,23 @@ module Parser
|
|
|
194
167
|
Kernel.exit 1
|
|
195
168
|
end
|
|
196
169
|
end
|
|
197
|
-
total += 1
|
|
198
170
|
[ (Node.new :block, "", children), total ]
|
|
199
171
|
end
|
|
200
172
|
|
|
201
173
|
def self.parse_literal(tokens)
|
|
202
|
-
(self.parse_block tokens) ||
|
|
174
|
+
(self.parse_block tokens) || self.parse_float(tokens) || (self.parse_name tokens) || (self.parse_number tokens) || (self.parse_list tokens) || (self.parse_string tokens) || (self.parse_nil tokens)
|
|
203
175
|
end
|
|
204
176
|
|
|
205
177
|
def self.parse_call(tokens)
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
callee = callee[0]
|
|
213
|
-
if self.expect tokens, :lpar
|
|
178
|
+
if self.lookahead tokens, :lpar, 1
|
|
179
|
+
unless (self.parse_literal tokens)
|
|
180
|
+
return nil
|
|
181
|
+
end
|
|
182
|
+
callee = (self.parse_literal tokens)
|
|
183
|
+
callee = callee[0]
|
|
214
184
|
args = []
|
|
215
|
-
tokens = tokens[
|
|
216
|
-
total
|
|
185
|
+
tokens = tokens[2..tokens.size]
|
|
186
|
+
total = 2
|
|
217
187
|
if self.expect tokens, :rpar
|
|
218
188
|
return [ (Node.new :call, callee, args), total + 1 ]
|
|
219
189
|
end
|
|
@@ -225,15 +195,14 @@ module Parser
|
|
|
225
195
|
total += part
|
|
226
196
|
tokens = tokens[part..tokens.size]
|
|
227
197
|
args << arg
|
|
198
|
+
total += 1
|
|
228
199
|
if self.expect tokens, :rpar
|
|
229
200
|
tokens = tokens[1..tokens.size]
|
|
230
|
-
total += 1
|
|
231
201
|
break
|
|
232
202
|
end
|
|
233
203
|
unless (self.expect tokens, :comma)
|
|
234
204
|
return nil
|
|
235
205
|
end
|
|
236
|
-
total += 1
|
|
237
206
|
tokens = tokens[1..tokens.size]
|
|
238
207
|
end
|
|
239
208
|
return [ (Node.new :call, callee, args), total ]
|
|
@@ -361,92 +330,26 @@ module Parser
|
|
|
361
330
|
return [ (Node.new :for, e, [name, block]), total ]
|
|
362
331
|
end
|
|
363
332
|
|
|
364
|
-
|
|
365
|
-
def self.parse_factor(tokens)
|
|
366
|
-
(self.parse_call tokens) ||
|
|
367
|
-
(self.parse_require tokens) ||
|
|
368
|
-
(self.parse_new tokens) ||
|
|
369
|
-
(self.parse_object tokens) ||
|
|
370
|
-
(self.parse_fn tokens) ||
|
|
371
|
-
(self.parse_assign tokens) ||
|
|
372
|
-
(self.parse_literal tokens) ||
|
|
373
|
-
(self.parse_if tokens) ||
|
|
374
|
-
(self.parse_while tokens) ||
|
|
375
|
-
(self.parse_for tokens)
|
|
376
|
-
end
|
|
377
|
-
|
|
378
|
-
def self.parse_term(tokens)
|
|
379
|
-
total = 0
|
|
380
|
-
unless self.parse_factor tokens
|
|
381
|
-
return nil
|
|
382
|
-
end
|
|
383
|
-
lhs, part = self.parse_factor tokens
|
|
384
|
-
total += part
|
|
385
|
-
tokens = tokens[part..tokens.size]
|
|
386
|
-
unless self.expect tokens, :l2op
|
|
387
|
-
return [lhs, part]
|
|
388
|
-
end
|
|
389
|
-
op = tokens[0][0]
|
|
390
|
-
total += 1
|
|
391
|
-
tokens = tokens[1..tokens.size]
|
|
392
|
-
unless self.parse_factor tokens
|
|
393
|
-
return nil
|
|
394
|
-
end
|
|
395
|
-
rhs, part = self.parse_factor tokens
|
|
396
|
-
total += part
|
|
397
|
-
tokens = tokens[part..tokens.size]
|
|
398
|
-
out = (Node.new :op, op, [lhs])
|
|
399
|
-
while self.expect tokens, :l2op
|
|
400
|
-
op = tokens[0][0]
|
|
401
|
-
total += 1
|
|
402
|
-
tokens = tokens[1..tokens.size]
|
|
403
|
-
unless self.parse_term tokens
|
|
404
|
-
return nil
|
|
405
|
-
end
|
|
406
|
-
rhs2, part = self.parse_term tokens
|
|
407
|
-
total += part
|
|
408
|
-
tokens = tokens[part..tokens.size]
|
|
409
|
-
rhs = Node.new :op, op, [rhs, rhs2]
|
|
410
|
-
end
|
|
411
|
-
out.children << rhs
|
|
412
|
-
return [out, total]
|
|
413
|
-
end
|
|
414
|
-
|
|
415
333
|
def self.parse_op(tokens)
|
|
416
334
|
total = 0
|
|
417
|
-
unless self.
|
|
335
|
+
unless self.parse_literal tokens
|
|
418
336
|
return nil
|
|
419
337
|
end
|
|
420
|
-
lhs, part = self.
|
|
338
|
+
lhs, part = self.parse_literal tokens
|
|
421
339
|
total += part
|
|
422
340
|
tokens = tokens[part..tokens.size]
|
|
423
|
-
unless self.expect tokens, :
|
|
424
|
-
return
|
|
341
|
+
unless self.expect tokens, :op
|
|
342
|
+
return nil
|
|
425
343
|
end
|
|
426
344
|
op = tokens[0][0]
|
|
427
345
|
total += 1
|
|
428
346
|
tokens = tokens[1..tokens.size]
|
|
429
|
-
unless self.
|
|
347
|
+
unless self.parse_expr tokens
|
|
430
348
|
return nil
|
|
431
349
|
end
|
|
432
|
-
rhs, part = self.
|
|
350
|
+
rhs, part = self.parse_expr tokens
|
|
433
351
|
total += part
|
|
434
|
-
|
|
435
|
-
out = (Node.new :op, op, [lhs])
|
|
436
|
-
while self.expect tokens, :l1op
|
|
437
|
-
op = tokens[0][0]
|
|
438
|
-
total += 1
|
|
439
|
-
tokens = tokens[1..tokens.size]
|
|
440
|
-
unless self.parse_term tokens
|
|
441
|
-
return nil
|
|
442
|
-
end
|
|
443
|
-
rhs2, part = self.parse_term tokens
|
|
444
|
-
total += part
|
|
445
|
-
tokens = tokens[part..tokens.size]
|
|
446
|
-
rhs = Node.new :op, op, [rhs, rhs2]
|
|
447
|
-
end
|
|
448
|
-
out.children << rhs
|
|
449
|
-
return [out, total]
|
|
352
|
+
return [ (Node.new :op, op, [lhs, rhs]), total]
|
|
450
353
|
end
|
|
451
354
|
|
|
452
355
|
def self.parse_assign(tokens)
|
|
@@ -580,17 +483,7 @@ module Parser
|
|
|
580
483
|
end
|
|
581
484
|
|
|
582
485
|
def self.parse_expr(tokens)
|
|
583
|
-
(self.parse_op tokens) ||
|
|
584
|
-
(self.parse_call tokens) ||
|
|
585
|
-
(self.parse_require tokens) ||
|
|
586
|
-
(self.parse_new tokens) ||
|
|
587
|
-
(self.parse_object tokens) ||
|
|
588
|
-
(self.parse_fn tokens) ||
|
|
589
|
-
(self.parse_assign tokens) ||
|
|
590
|
-
(self.parse_literal tokens) ||
|
|
591
|
-
(self.parse_if tokens) ||
|
|
592
|
-
(self.parse_while tokens) ||
|
|
593
|
-
(self.parse_for tokens)
|
|
486
|
+
(self.parse_require tokens) || (self.parse_new tokens) || (self.parse_object tokens) || (self.parse_fn tokens) || (self.parse_assign tokens) || (self.parse_op tokens) || (self.parse_call tokens) || (self.parse_literal tokens) || (self.parse_if tokens) || (self.parse_while tokens) || (self.parse_for tokens)
|
|
594
487
|
end
|
|
595
488
|
|
|
596
489
|
def self.parse(tokens, path)
|
data/lib/sdx/vm/datatypes.rb
CHANGED
|
@@ -58,12 +58,6 @@ 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
|
|
67
61
|
end))
|
|
68
62
|
}
|
|
69
63
|
end
|
|
@@ -89,40 +83,22 @@ class Int < DataType
|
|
|
89
83
|
as_bool
|
|
90
84
|
end)),
|
|
91
85
|
"__add" => (NativeFnInternal.new (Proc.new do |other|
|
|
92
|
-
add other
|
|
86
|
+
add other
|
|
93
87
|
end)),
|
|
94
88
|
"__sub" => (NativeFnInternal.new (Proc.new do |other|
|
|
95
|
-
sub other
|
|
89
|
+
sub other
|
|
96
90
|
end)),
|
|
97
91
|
"__mul" => (NativeFnInternal.new (Proc.new do |other|
|
|
98
|
-
mul other
|
|
92
|
+
mul other
|
|
99
93
|
end)),
|
|
100
94
|
"__div" => (NativeFnInternal.new (Proc.new do |other|
|
|
101
|
-
div other
|
|
95
|
+
div other
|
|
102
96
|
end)),
|
|
103
97
|
"__mod" => (NativeFnInternal.new (Proc.new do |other|
|
|
104
|
-
mod other
|
|
98
|
+
mod other
|
|
105
99
|
end)),
|
|
106
100
|
"__pow" => (NativeFnInternal.new (Proc.new do |other|
|
|
107
|
-
pow other
|
|
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
|
|
101
|
+
pow other
|
|
126
102
|
end))
|
|
127
103
|
}
|
|
128
104
|
end
|
|
@@ -158,22 +134,6 @@ class Int < DataType
|
|
|
158
134
|
def pow(other)
|
|
159
135
|
Int.new @internal ** other.internal
|
|
160
136
|
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
|
|
177
137
|
end
|
|
178
138
|
|
|
179
139
|
class Str < DataType
|
|
@@ -189,16 +149,10 @@ class Str < DataType
|
|
|
189
149
|
as_code_string
|
|
190
150
|
end)),
|
|
191
151
|
"__add" => (NativeFnInternal.new (Proc.new do |other|
|
|
192
|
-
add other
|
|
152
|
+
add other
|
|
193
153
|
end)),
|
|
194
154
|
"__mul" => (NativeFnInternal.new (Proc.new do |other|
|
|
195
|
-
mul other
|
|
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
|
|
155
|
+
mul other
|
|
202
156
|
end))
|
|
203
157
|
}
|
|
204
158
|
end
|
|
@@ -236,40 +190,22 @@ class Num < DataType
|
|
|
236
190
|
as_bool
|
|
237
191
|
end)),
|
|
238
192
|
"__add" => (NativeFnInternal.new (Proc.new do |other|
|
|
239
|
-
add other
|
|
193
|
+
add other
|
|
240
194
|
end)),
|
|
241
195
|
"__sub" => (NativeFnInternal.new (Proc.new do |other|
|
|
242
|
-
sub other
|
|
196
|
+
sub other
|
|
243
197
|
end)),
|
|
244
198
|
"__mul" => (NativeFnInternal.new (Proc.new do |other|
|
|
245
|
-
mul other
|
|
199
|
+
mul other
|
|
246
200
|
end)),
|
|
247
201
|
"__div" => (NativeFnInternal.new (Proc.new do |other|
|
|
248
|
-
div other
|
|
202
|
+
div other
|
|
249
203
|
end)),
|
|
250
204
|
"__mod" => (NativeFnInternal.new (Proc.new do |other|
|
|
251
|
-
mod other
|
|
205
|
+
mod other
|
|
252
206
|
end)),
|
|
253
207
|
"__pow" => (NativeFnInternal.new (Proc.new do |other|
|
|
254
|
-
pow other
|
|
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
|
|
208
|
+
pow other
|
|
273
209
|
end))
|
|
274
210
|
}
|
|
275
211
|
end
|
|
@@ -305,22 +241,6 @@ class Num < DataType
|
|
|
305
241
|
def pow(other)
|
|
306
242
|
Num.new @internal ** other.internal
|
|
307
243
|
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
|
|
324
244
|
end
|
|
325
245
|
|
|
326
246
|
class Nil < DataType
|
|
@@ -329,21 +249,14 @@ class Nil < DataType
|
|
|
329
249
|
@fields = {
|
|
330
250
|
"__as_bool" => (NativeFnInternal.new (Proc.new do
|
|
331
251
|
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
|
|
338
252
|
end))
|
|
339
253
|
}
|
|
340
254
|
end
|
|
341
255
|
end
|
|
342
256
|
|
|
343
257
|
class List < DataType
|
|
344
|
-
def initialize(val
|
|
258
|
+
def initialize(val)
|
|
345
259
|
@internal = val
|
|
346
|
-
@scope = scope
|
|
347
260
|
@pos = 0
|
|
348
261
|
@fields = {
|
|
349
262
|
"__as_string" => (NativeFnInternal.new (Proc.new do
|
|
@@ -359,40 +272,34 @@ class List < DataType
|
|
|
359
272
|
iter
|
|
360
273
|
end)),
|
|
361
274
|
"__add" => (NativeFnInternal.new (Proc.new do |other|
|
|
362
|
-
add other
|
|
275
|
+
add other
|
|
363
276
|
end)),
|
|
364
277
|
"__mul" => (NativeFnInternal.new (Proc.new do |other|
|
|
365
|
-
mul other
|
|
278
|
+
mul other
|
|
366
279
|
end)),
|
|
367
280
|
"__arity" => (Int.new 1),
|
|
368
281
|
"__call" => (NativeFnInternal.new (Proc.new do |args, scope|
|
|
369
282
|
@internal[args[0].value.internal]
|
|
370
|
-
end)),
|
|
371
|
-
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
372
|
-
Bool.new @internal == other[0].internal
|
|
373
|
-
end)),
|
|
374
|
-
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
375
|
-
Bool.new @internal != other[0].internal
|
|
376
283
|
end))
|
|
377
284
|
}
|
|
378
285
|
end
|
|
379
286
|
|
|
380
287
|
def as_string
|
|
381
|
-
s = ""
|
|
288
|
+
s = "["
|
|
382
289
|
@internal.each do |item|
|
|
383
290
|
s += (stringify item) + ", "
|
|
384
291
|
end
|
|
385
|
-
s =
|
|
292
|
+
s = s[0..-3]
|
|
386
293
|
s += "]"
|
|
387
294
|
Str.new s
|
|
388
295
|
end
|
|
389
296
|
|
|
390
297
|
def as_code_string
|
|
391
|
-
s = ""
|
|
298
|
+
s = "["
|
|
392
299
|
@internal.each do |item|
|
|
393
300
|
s += (codify item) + ", "
|
|
394
301
|
end
|
|
395
|
-
s =
|
|
302
|
+
s = s[0..-3]
|
|
396
303
|
s += "]"
|
|
397
304
|
Str.new s
|
|
398
305
|
end
|
|
@@ -407,12 +314,12 @@ class List < DataType
|
|
|
407
314
|
if val
|
|
408
315
|
return val
|
|
409
316
|
else
|
|
410
|
-
return Variable.new Nil.new
|
|
317
|
+
return Variable.new Nil.new
|
|
411
318
|
end
|
|
412
319
|
end
|
|
413
320
|
|
|
414
321
|
def add(other)
|
|
415
|
-
return List.new [*@internal,
|
|
322
|
+
return List.new [*@internal, other]
|
|
416
323
|
end
|
|
417
324
|
|
|
418
325
|
def mul(other)
|
|
@@ -454,13 +361,7 @@ class Function < DataType
|
|
|
454
361
|
"__call" => (NativeFnInternal.new (lambda do |args, scope|
|
|
455
362
|
call args, scope
|
|
456
363
|
end)),
|
|
457
|
-
"__arity" => (Int.new args.size)
|
|
458
|
-
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
459
|
-
Bool.new @internal == other[0].internal
|
|
460
|
-
end)),
|
|
461
|
-
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
462
|
-
Bool.new @internal != other[0].internal
|
|
463
|
-
end))
|
|
364
|
+
"__arity" => (Int.new args.size)
|
|
464
365
|
}
|
|
465
366
|
end
|
|
466
367
|
|
|
@@ -486,13 +387,7 @@ class Block < DataType
|
|
|
486
387
|
"__call" => (NativeFnInternal.new (Proc.new do |args, scope|
|
|
487
388
|
call args, scope
|
|
488
389
|
end)),
|
|
489
|
-
"__arity" => (Int.new 1)
|
|
490
|
-
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
491
|
-
Bool.new @internal == other[0].internal
|
|
492
|
-
end)),
|
|
493
|
-
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
494
|
-
Bool.new @internal != other[0].internal
|
|
495
|
-
end))
|
|
390
|
+
"__arity" => (Int.new 1)
|
|
496
391
|
}
|
|
497
392
|
end
|
|
498
393
|
|
|
@@ -519,13 +414,7 @@ class Obj < DataType
|
|
|
519
414
|
"__new" => (NativeFnInternal.new (Proc.new do |args, scope|
|
|
520
415
|
_new args, scope
|
|
521
416
|
end)),
|
|
522
|
-
"__arity" => (Int.new args.size)
|
|
523
|
-
"__eq" => (NativeFnInternal.new (lambda do |other|
|
|
524
|
-
Bool.new @internal == other[0].internal
|
|
525
|
-
end)),
|
|
526
|
-
"__neq" => (NativeFnInternal.new (lambda do |other|
|
|
527
|
-
Bool.new @internal != other[0].internal
|
|
528
|
-
end))
|
|
417
|
+
"__arity" => (Int.new args.size)
|
|
529
418
|
}
|
|
530
419
|
end
|
|
531
420
|
|
data/lib/sdx/vm/vm.rb
CHANGED
|
@@ -2,13 +2,17 @@ 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
|
+
|
|
5
13
|
def codify(val)
|
|
6
14
|
if val.value.fields["__as_code_string"]
|
|
7
|
-
|
|
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
|
|
15
|
+
(val.value.fields["__as_code_string"].call).internal
|
|
12
16
|
else
|
|
13
17
|
val.value.pretty_inspect
|
|
14
18
|
end
|
|
@@ -18,22 +22,10 @@ class VM
|
|
|
18
22
|
attr_accessor :bc_io
|
|
19
23
|
|
|
20
24
|
def truthy(val)
|
|
21
|
-
case val.value
|
|
22
|
-
when Bool
|
|
23
|
-
return val.value.internal
|
|
24
|
-
end
|
|
25
25
|
if val.value.fields["__as_bool"]
|
|
26
|
-
|
|
26
|
+
(val.value.fields["__as_bool"].call).internal
|
|
27
27
|
else
|
|
28
|
-
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def stringify(val)
|
|
33
|
-
if val.value.fields["__as_string"]
|
|
34
|
-
(call val.value.fields["__as_string"], [], val.scope).internal
|
|
35
|
-
else
|
|
36
|
-
val.value.to_s
|
|
28
|
+
true
|
|
37
29
|
end
|
|
38
30
|
end
|
|
39
31
|
|
|
@@ -59,7 +51,7 @@ class VM
|
|
|
59
51
|
when String
|
|
60
52
|
to_var (Str.new val)
|
|
61
53
|
when Float
|
|
62
|
-
to_var (
|
|
54
|
+
to_var (Float.new val)
|
|
63
55
|
when Array
|
|
64
56
|
to_var (List.new val.map { |v| from_rb v })
|
|
65
57
|
when Nil
|
|
@@ -143,7 +135,6 @@ class VM
|
|
|
143
135
|
0x24 => :sub,
|
|
144
136
|
0x25 => :mul,
|
|
145
137
|
0x26 => :div,
|
|
146
|
-
0x27 => :mod,
|
|
147
138
|
0x12 => :bool,
|
|
148
139
|
0x13 => :int,
|
|
149
140
|
0x14 => :str,
|
|
@@ -159,14 +150,7 @@ class VM
|
|
|
159
150
|
0x30 => :object,
|
|
160
151
|
0x31 => :new,
|
|
161
152
|
0x32 => :block,
|
|
162
|
-
0x33 => :end
|
|
163
|
-
0x34 => :eq,
|
|
164
|
-
0x35 => :ne,
|
|
165
|
-
0x36 => :lt,
|
|
166
|
-
0x37 => :gt,
|
|
167
|
-
0x38 => :le,
|
|
168
|
-
0x39 => :ge,
|
|
169
|
-
0x28 => :pow,
|
|
153
|
+
0x33 => :end
|
|
170
154
|
}
|
|
171
155
|
bytes = []
|
|
172
156
|
begin
|
|
@@ -280,19 +264,12 @@ class VM
|
|
|
280
264
|
vals << pop_from_stack
|
|
281
265
|
end
|
|
282
266
|
vals.reverse!
|
|
283
|
-
push_to_stack Variable.new (List.new vals
|
|
267
|
+
push_to_stack Variable.new (List.new vals), :list, @global
|
|
284
268
|
when :block
|
|
285
269
|
size = get_string.to_i
|
|
286
270
|
body =
|
|
287
271
|
((load_bytes size, false).map { |e| e.chr }).join ""
|
|
288
272
|
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
|
|
296
273
|
when :nil
|
|
297
274
|
push_to_stack Variable.new (Nil.new), :nil, @global
|
|
298
275
|
end
|
|
@@ -302,7 +279,7 @@ class VM
|
|
|
302
279
|
res = (call a.value.fields["__add"], b.value)
|
|
303
280
|
push_to_stack (to_var res)
|
|
304
281
|
else
|
|
305
|
-
error "Cannot
|
|
282
|
+
error "Cannot add to #{a.type}"
|
|
306
283
|
end
|
|
307
284
|
when :sub
|
|
308
285
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -310,7 +287,7 @@ class VM
|
|
|
310
287
|
res = (call a.value.fields["__sub"], b.value)
|
|
311
288
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
312
289
|
else
|
|
313
|
-
error "Cannot
|
|
290
|
+
error "Cannot subtract from #{a.type}"
|
|
314
291
|
end
|
|
315
292
|
when :mul
|
|
316
293
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -318,7 +295,7 @@ class VM
|
|
|
318
295
|
res = (call a.value.fields["__mul"], b.value)
|
|
319
296
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
320
297
|
else
|
|
321
|
-
error "Cannot
|
|
298
|
+
error "Cannot multiply #{a.type}"
|
|
322
299
|
end
|
|
323
300
|
when :div
|
|
324
301
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -326,7 +303,7 @@ class VM
|
|
|
326
303
|
res = (call a.value.fields["__div"], b.value)
|
|
327
304
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
328
305
|
else
|
|
329
|
-
error "Cannot
|
|
306
|
+
error "Cannot divide #{a.type}"
|
|
330
307
|
end
|
|
331
308
|
when :mod
|
|
332
309
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -334,7 +311,7 @@ class VM
|
|
|
334
311
|
res = (call a.value.fields["__mod"], b.value)
|
|
335
312
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
336
313
|
else
|
|
337
|
-
error "Cannot
|
|
314
|
+
error "Cannot modulo #{a.type}"
|
|
338
315
|
end
|
|
339
316
|
when :pow
|
|
340
317
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -342,55 +319,7 @@ class VM
|
|
|
342
319
|
res = (call a.value.fields["__pow"], b.value)
|
|
343
320
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
344
321
|
else
|
|
345
|
-
error "Cannot
|
|
346
|
-
end
|
|
347
|
-
when :eq
|
|
348
|
-
b, a = pop_from_stack, pop_from_stack
|
|
349
|
-
if a.value.fields["__eq"]
|
|
350
|
-
res = (call a.value.fields["__eq"], b.value)
|
|
351
|
-
push_to_stack (Variable.new res, (get_type res), @global)
|
|
352
|
-
else
|
|
353
|
-
error "Cannot use == on #{a.type}"
|
|
354
|
-
end
|
|
355
|
-
when :ne
|
|
356
|
-
b, a = pop_from_stack, pop_from_stack
|
|
357
|
-
if a.value.fields["__neq"]
|
|
358
|
-
res = (call a.value.fields["__neq"], b.value)
|
|
359
|
-
push_to_stack (Variable.new res, (get_type res), @global)
|
|
360
|
-
else
|
|
361
|
-
error "Cannot use != on #{a.type}"
|
|
362
|
-
end
|
|
363
|
-
when :lt
|
|
364
|
-
b, a = pop_from_stack, pop_from_stack
|
|
365
|
-
if a.value.fields["__lt"]
|
|
366
|
-
res = (call a.value.fields["__lt"], b.value)
|
|
367
|
-
push_to_stack (Variable.new res, (get_type res), @global)
|
|
368
|
-
else
|
|
369
|
-
error "Cannot use < on #{a.type}"
|
|
370
|
-
end
|
|
371
|
-
when :gt
|
|
372
|
-
b, a = pop_from_stack, pop_from_stack
|
|
373
|
-
if a.value.fields["__gt"]
|
|
374
|
-
res = (call a.value.fields["__gt"], b.value)
|
|
375
|
-
push_to_stack (Variable.new res, (get_type res), @global)
|
|
376
|
-
else
|
|
377
|
-
error "Cannot use > on #{a.type}"
|
|
378
|
-
end
|
|
379
|
-
when :le
|
|
380
|
-
b, a = pop_from_stack, pop_from_stack
|
|
381
|
-
if a.value.fields["__le"]
|
|
382
|
-
res = (call a.value.fields["__le"], b.value)
|
|
383
|
-
push_to_stack (Variable.new res, (get_type res), @global)
|
|
384
|
-
else
|
|
385
|
-
error "Cannot use <= on #{a.type}"
|
|
386
|
-
end
|
|
387
|
-
when :ge
|
|
388
|
-
b, a = pop_from_stack, pop_from_stack
|
|
389
|
-
if a.value.fields["__ge"]
|
|
390
|
-
res = (call a.value.fields["__ge"], b.value)
|
|
391
|
-
push_to_stack (Variable.new res, (get_type res), @global)
|
|
392
|
-
else
|
|
393
|
-
error "Cannot use >= on #{a.type}"
|
|
322
|
+
error "Cannot exponentiate #{a.type}"
|
|
394
323
|
end
|
|
395
324
|
when :jmpi
|
|
396
325
|
val = pop_from_stack
|