sardonyx 0.1.8 → 0.1.85
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 +15 -1
- data/lib/sdx/compiler/parser.rb +71 -19
- data/lib/sdx/vm/datatypes.rb +138 -27
- data/lib/sdx/vm/vm.rb +90 -20
- 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: 8c24e6115400ff31393984b7220c4226ea6f6a4fc8f9ba146f8d9f3ce51f749b
|
|
4
|
+
data.tar.gz: e1b92b4d17872047f11fe869d988370898dbebc74f5fe2ba4cb5e8d0ede648ec
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c87037ad33284bec4da7576040d214a45978ab5e018c5ef62152a2ae31858484df6f5f2e7899d6b29f9618a875d145f45960aa8c1efa22b97e60ff661b579eea
|
|
7
|
+
data.tar.gz: 2ed22a60d030ac9236075a3f97d992956753dc9c3cda125783f3e05f5c74c9d20ce3fe84a848b96b7933b2063efc9d1dd9a899f95d26957050dd7979de75d957
|
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
|
|
@@ -91,10 +103,12 @@ module Compiler
|
|
|
91
103
|
end
|
|
92
104
|
i += "\x2a#{e.size}\x18"
|
|
93
105
|
end
|
|
94
|
-
bc += "\
|
|
106
|
+
bc += "\x2b#{i.size}\x18" + i
|
|
95
107
|
if e
|
|
96
108
|
bc += e
|
|
97
109
|
end
|
|
110
|
+
when :bool
|
|
111
|
+
bc += "\x21\x12#{node.value}\x18"
|
|
98
112
|
when :name
|
|
99
113
|
bc += "\x20#{node.value}\x18"
|
|
100
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 ]
|
|
@@ -113,6 +123,23 @@ module Parser
|
|
|
113
123
|
end
|
|
114
124
|
end
|
|
115
125
|
|
|
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
|
+
|
|
116
143
|
def self.parse_list(tokens)
|
|
117
144
|
unless (self.expect tokens, :lbrack)
|
|
118
145
|
return nil
|
|
@@ -155,7 +182,7 @@ module Parser
|
|
|
155
182
|
while true
|
|
156
183
|
if self.expect tokens, :rbrace
|
|
157
184
|
total += 1
|
|
158
|
-
|
|
185
|
+
return [ (Node.new :block, "", children), total ]
|
|
159
186
|
end
|
|
160
187
|
e = self.parse_expr tokens
|
|
161
188
|
if e
|
|
@@ -167,23 +194,26 @@ module Parser
|
|
|
167
194
|
Kernel.exit 1
|
|
168
195
|
end
|
|
169
196
|
end
|
|
197
|
+
total += 1
|
|
170
198
|
[ (Node.new :block, "", children), total ]
|
|
171
199
|
end
|
|
172
200
|
|
|
173
201
|
def self.parse_literal(tokens)
|
|
174
|
-
(self.parse_block tokens) ||
|
|
202
|
+
(self.parse_block tokens) || (self.parse_bool 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) || (self.parse_parens tokens)
|
|
175
203
|
end
|
|
176
204
|
|
|
177
205
|
def self.parse_call(tokens)
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
206
|
+
unless (self.parse_literal tokens)
|
|
207
|
+
return nil
|
|
208
|
+
end
|
|
209
|
+
callee = (self.parse_literal tokens)
|
|
210
|
+
total = callee[1]
|
|
211
|
+
tokens = tokens[total..tokens.size]
|
|
212
|
+
callee = callee[0]
|
|
213
|
+
if self.expect tokens, :lpar
|
|
184
214
|
args = []
|
|
185
|
-
tokens = tokens[
|
|
186
|
-
total
|
|
215
|
+
tokens = tokens[1..tokens.size]
|
|
216
|
+
total += 1
|
|
187
217
|
if self.expect tokens, :rpar
|
|
188
218
|
return [ (Node.new :call, callee, args), total + 1 ]
|
|
189
219
|
end
|
|
@@ -330,7 +360,7 @@ module Parser
|
|
|
330
360
|
return [ (Node.new :for, e, [name, block]), total ]
|
|
331
361
|
end
|
|
332
362
|
|
|
333
|
-
def self.
|
|
363
|
+
def self.parse_term(tokens)
|
|
334
364
|
total = 0
|
|
335
365
|
unless self.parse_literal tokens
|
|
336
366
|
return nil
|
|
@@ -338,16 +368,38 @@ module Parser
|
|
|
338
368
|
lhs, part = self.parse_literal tokens
|
|
339
369
|
total += part
|
|
340
370
|
tokens = tokens[part..tokens.size]
|
|
341
|
-
unless self.expect tokens, :
|
|
371
|
+
unless self.expect tokens, :l2op
|
|
372
|
+
return [lhs, part]
|
|
373
|
+
end
|
|
374
|
+
op = tokens[0][0]
|
|
375
|
+
total += 1
|
|
376
|
+
tokens = tokens[1..tokens.size]
|
|
377
|
+
unless self.parse_literal tokens
|
|
378
|
+
return nil
|
|
379
|
+
end
|
|
380
|
+
rhs, part = self.parse_literal tokens
|
|
381
|
+
total += part
|
|
382
|
+
return [ (Node.new :op, op, [lhs, rhs]), total]
|
|
383
|
+
end
|
|
384
|
+
|
|
385
|
+
def self.parse_op(tokens)
|
|
386
|
+
total = 0
|
|
387
|
+
unless self.parse_term tokens
|
|
342
388
|
return nil
|
|
343
389
|
end
|
|
390
|
+
lhs, part = self.parse_term tokens
|
|
391
|
+
total += part
|
|
392
|
+
tokens = tokens[part..tokens.size]
|
|
393
|
+
unless self.expect tokens, :l1op
|
|
394
|
+
return [lhs, part]
|
|
395
|
+
end
|
|
344
396
|
op = tokens[0][0]
|
|
345
397
|
total += 1
|
|
346
398
|
tokens = tokens[1..tokens.size]
|
|
347
|
-
unless self.
|
|
399
|
+
unless self.parse_term tokens
|
|
348
400
|
return nil
|
|
349
401
|
end
|
|
350
|
-
rhs, part = self.
|
|
402
|
+
rhs, part = self.parse_term tokens
|
|
351
403
|
total += part
|
|
352
404
|
return [ (Node.new :op, op, [lhs, rhs]), total]
|
|
353
405
|
end
|
|
@@ -483,7 +535,7 @@ module Parser
|
|
|
483
535
|
end
|
|
484
536
|
|
|
485
537
|
def self.parse_expr(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)
|
|
538
|
+
(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_term tokens) || (self.parse_call tokens) || (self.parse_literal tokens) || (self.parse_if tokens) || (self.parse_while tokens) || (self.parse_for tokens)
|
|
487
539
|
end
|
|
488
540
|
|
|
489
541
|
def self.parse(tokens, path)
|
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
|
|
@@ -83,22 +89,40 @@ class Int < DataType
|
|
|
83
89
|
as_bool
|
|
84
90
|
end)),
|
|
85
91
|
"__add" => (NativeFnInternal.new (Proc.new do |other|
|
|
86
|
-
add other
|
|
92
|
+
add other[0]
|
|
87
93
|
end)),
|
|
88
94
|
"__sub" => (NativeFnInternal.new (Proc.new do |other|
|
|
89
|
-
sub other
|
|
95
|
+
sub other[0]
|
|
90
96
|
end)),
|
|
91
97
|
"__mul" => (NativeFnInternal.new (Proc.new do |other|
|
|
92
|
-
mul other
|
|
98
|
+
mul other[0]
|
|
93
99
|
end)),
|
|
94
100
|
"__div" => (NativeFnInternal.new (Proc.new do |other|
|
|
95
|
-
div other
|
|
101
|
+
div other[0]
|
|
96
102
|
end)),
|
|
97
103
|
"__mod" => (NativeFnInternal.new (Proc.new do |other|
|
|
98
|
-
mod other
|
|
104
|
+
mod other[0]
|
|
99
105
|
end)),
|
|
100
106
|
"__pow" => (NativeFnInternal.new (Proc.new do |other|
|
|
101
|
-
pow other
|
|
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
|
|
@@ -149,10 +189,16 @@ class Str < DataType
|
|
|
149
189
|
as_code_string
|
|
150
190
|
end)),
|
|
151
191
|
"__add" => (NativeFnInternal.new (Proc.new do |other|
|
|
152
|
-
add other
|
|
192
|
+
add other[0]
|
|
153
193
|
end)),
|
|
154
194
|
"__mul" => (NativeFnInternal.new (Proc.new do |other|
|
|
155
|
-
mul other
|
|
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
|
|
@@ -162,7 +208,7 @@ class Str < DataType
|
|
|
162
208
|
end
|
|
163
209
|
|
|
164
210
|
def as_code_string
|
|
165
|
-
(Str.new
|
|
211
|
+
(Str.new @internal.dump)
|
|
166
212
|
end
|
|
167
213
|
|
|
168
214
|
def add(other)
|
|
@@ -190,22 +236,40 @@ class Num < DataType
|
|
|
190
236
|
as_bool
|
|
191
237
|
end)),
|
|
192
238
|
"__add" => (NativeFnInternal.new (Proc.new do |other|
|
|
193
|
-
add other
|
|
239
|
+
add other[0]
|
|
194
240
|
end)),
|
|
195
241
|
"__sub" => (NativeFnInternal.new (Proc.new do |other|
|
|
196
|
-
sub other
|
|
242
|
+
sub other[0]
|
|
197
243
|
end)),
|
|
198
244
|
"__mul" => (NativeFnInternal.new (Proc.new do |other|
|
|
199
|
-
mul other
|
|
245
|
+
mul other[0]
|
|
200
246
|
end)),
|
|
201
247
|
"__div" => (NativeFnInternal.new (Proc.new do |other|
|
|
202
|
-
div other
|
|
248
|
+
div other[0]
|
|
203
249
|
end)),
|
|
204
250
|
"__mod" => (NativeFnInternal.new (Proc.new do |other|
|
|
205
|
-
mod other
|
|
251
|
+
mod other[0]
|
|
206
252
|
end)),
|
|
207
253
|
"__pow" => (NativeFnInternal.new (Proc.new do |other|
|
|
208
|
-
pow other
|
|
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,14 +329,21 @@ 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
|
|
255
341
|
end
|
|
256
342
|
|
|
257
343
|
class List < DataType
|
|
258
|
-
def initialize(val)
|
|
344
|
+
def initialize(val, scope=nil)
|
|
259
345
|
@internal = val
|
|
346
|
+
@scope = scope
|
|
260
347
|
@pos = 0
|
|
261
348
|
@fields = {
|
|
262
349
|
"__as_string" => (NativeFnInternal.new (Proc.new do
|
|
@@ -272,34 +359,40 @@ class List < DataType
|
|
|
272
359
|
iter
|
|
273
360
|
end)),
|
|
274
361
|
"__add" => (NativeFnInternal.new (Proc.new do |other|
|
|
275
|
-
add other
|
|
362
|
+
add other[0]
|
|
276
363
|
end)),
|
|
277
364
|
"__mul" => (NativeFnInternal.new (Proc.new do |other|
|
|
278
|
-
mul other
|
|
365
|
+
mul other[0]
|
|
279
366
|
end)),
|
|
280
367
|
"__arity" => (Int.new 1),
|
|
281
368
|
"__call" => (NativeFnInternal.new (Proc.new do |args, scope|
|
|
282
369
|
@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
|
|
283
376
|
end))
|
|
284
377
|
}
|
|
285
378
|
end
|
|
286
379
|
|
|
287
380
|
def as_string
|
|
288
|
-
s = "
|
|
381
|
+
s = ""
|
|
289
382
|
@internal.each do |item|
|
|
290
383
|
s += (stringify item) + ", "
|
|
291
384
|
end
|
|
292
|
-
s = s[0..-3]
|
|
385
|
+
s = "[" + s[0..-3]
|
|
293
386
|
s += "]"
|
|
294
387
|
Str.new s
|
|
295
388
|
end
|
|
296
389
|
|
|
297
390
|
def as_code_string
|
|
298
|
-
s = "
|
|
391
|
+
s = ""
|
|
299
392
|
@internal.each do |item|
|
|
300
393
|
s += (codify item) + ", "
|
|
301
394
|
end
|
|
302
|
-
s = s[0..-3]
|
|
395
|
+
s = "[" + s[0..-3]
|
|
303
396
|
s += "]"
|
|
304
397
|
Str.new s
|
|
305
398
|
end
|
|
@@ -314,12 +407,12 @@ class List < DataType
|
|
|
314
407
|
if val
|
|
315
408
|
return val
|
|
316
409
|
else
|
|
317
|
-
return Variable.new Nil.new
|
|
410
|
+
return Variable.new Nil.new, :nil, @internal[0].scope
|
|
318
411
|
end
|
|
319
412
|
end
|
|
320
413
|
|
|
321
414
|
def add(other)
|
|
322
|
-
return List.new [*@internal, other]
|
|
415
|
+
return List.new [*@internal, (Variable.new other, (get_type other), @scope || @internal[0].scope)]
|
|
323
416
|
end
|
|
324
417
|
|
|
325
418
|
def mul(other)
|
|
@@ -361,7 +454,13 @@ class Function < DataType
|
|
|
361
454
|
"__call" => (NativeFnInternal.new (lambda do |args, scope|
|
|
362
455
|
call args, scope
|
|
363
456
|
end)),
|
|
364
|
-
"__arity" => (Int.new args.size)
|
|
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))
|
|
365
464
|
}
|
|
366
465
|
end
|
|
367
466
|
|
|
@@ -387,7 +486,13 @@ class Block < DataType
|
|
|
387
486
|
"__call" => (NativeFnInternal.new (Proc.new do |args, scope|
|
|
388
487
|
call args, scope
|
|
389
488
|
end)),
|
|
390
|
-
"__arity" => (Int.new 1)
|
|
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))
|
|
391
496
|
}
|
|
392
497
|
end
|
|
393
498
|
|
|
@@ -414,7 +519,13 @@ class Obj < DataType
|
|
|
414
519
|
"__new" => (NativeFnInternal.new (Proc.new do |args, scope|
|
|
415
520
|
_new args, scope
|
|
416
521
|
end)),
|
|
417
|
-
"__arity" => (Int.new args.size)
|
|
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))
|
|
418
529
|
}
|
|
419
530
|
end
|
|
420
531
|
|
data/lib/sdx/vm/vm.rb
CHANGED
|
@@ -2,17 +2,13 @@ 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
5
|
def codify(val)
|
|
14
6
|
if val.value.fields["__as_code_string"]
|
|
15
|
-
|
|
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
|
|
16
12
|
else
|
|
17
13
|
val.value.pretty_inspect
|
|
18
14
|
end
|
|
@@ -22,10 +18,22 @@ class VM
|
|
|
22
18
|
attr_accessor :bc_io
|
|
23
19
|
|
|
24
20
|
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
|
-
(val.value.fields["__as_bool"].call).internal
|
|
26
|
+
return (val.value.fields["__as_bool"].call).internal
|
|
27
27
|
else
|
|
28
|
-
true
|
|
28
|
+
return true
|
|
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
|
|
29
37
|
end
|
|
30
38
|
end
|
|
31
39
|
|
|
@@ -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
|
|
@@ -135,6 +143,7 @@ class VM
|
|
|
135
143
|
0x24 => :sub,
|
|
136
144
|
0x25 => :mul,
|
|
137
145
|
0x26 => :div,
|
|
146
|
+
0x27 => :mod,
|
|
138
147
|
0x12 => :bool,
|
|
139
148
|
0x13 => :int,
|
|
140
149
|
0x14 => :str,
|
|
@@ -150,7 +159,13 @@ class VM
|
|
|
150
159
|
0x30 => :object,
|
|
151
160
|
0x31 => :new,
|
|
152
161
|
0x32 => :block,
|
|
153
|
-
0x33 => :end
|
|
162
|
+
0x33 => :end,
|
|
163
|
+
0x34 => :eq,
|
|
164
|
+
0x35 => :ne,
|
|
165
|
+
0x36 => :lt,
|
|
166
|
+
0x37 => :gt,
|
|
167
|
+
0x38 => :le,
|
|
168
|
+
0x39 => :ge
|
|
154
169
|
}
|
|
155
170
|
bytes = []
|
|
156
171
|
begin
|
|
@@ -264,12 +279,19 @@ class VM
|
|
|
264
279
|
vals << pop_from_stack
|
|
265
280
|
end
|
|
266
281
|
vals.reverse!
|
|
267
|
-
push_to_stack Variable.new (List.new vals), :list, @global
|
|
282
|
+
push_to_stack Variable.new (List.new vals, @global), :list, @global
|
|
268
283
|
when :block
|
|
269
284
|
size = get_string.to_i
|
|
270
285
|
body =
|
|
271
286
|
((load_bytes size, false).map { |e| e.chr }).join ""
|
|
272
287
|
push_to_stack Variable.new (Block.new body), :block, @global
|
|
288
|
+
when :bool
|
|
289
|
+
val = get_string
|
|
290
|
+
t = {
|
|
291
|
+
"true" => true,
|
|
292
|
+
"false" => false,
|
|
293
|
+
}
|
|
294
|
+
push_to_stack Variable.new (Bool.new t[val]), :bool, @global
|
|
273
295
|
when :nil
|
|
274
296
|
push_to_stack Variable.new (Nil.new), :nil, @global
|
|
275
297
|
end
|
|
@@ -279,7 +301,7 @@ class VM
|
|
|
279
301
|
res = (call a.value.fields["__add"], b.value)
|
|
280
302
|
push_to_stack (to_var res)
|
|
281
303
|
else
|
|
282
|
-
error "Cannot
|
|
304
|
+
error "Cannot use + on #{a.type}"
|
|
283
305
|
end
|
|
284
306
|
when :sub
|
|
285
307
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -287,7 +309,7 @@ class VM
|
|
|
287
309
|
res = (call a.value.fields["__sub"], b.value)
|
|
288
310
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
289
311
|
else
|
|
290
|
-
error "Cannot
|
|
312
|
+
error "Cannot use - on #{a.type}"
|
|
291
313
|
end
|
|
292
314
|
when :mul
|
|
293
315
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -295,7 +317,7 @@ class VM
|
|
|
295
317
|
res = (call a.value.fields["__mul"], b.value)
|
|
296
318
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
297
319
|
else
|
|
298
|
-
error "Cannot
|
|
320
|
+
error "Cannot use * on #{a.type}"
|
|
299
321
|
end
|
|
300
322
|
when :div
|
|
301
323
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -303,7 +325,7 @@ class VM
|
|
|
303
325
|
res = (call a.value.fields["__div"], b.value)
|
|
304
326
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
305
327
|
else
|
|
306
|
-
error "Cannot
|
|
328
|
+
error "Cannot use / on #{a.type}"
|
|
307
329
|
end
|
|
308
330
|
when :mod
|
|
309
331
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -311,7 +333,7 @@ class VM
|
|
|
311
333
|
res = (call a.value.fields["__mod"], b.value)
|
|
312
334
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
313
335
|
else
|
|
314
|
-
error "Cannot
|
|
336
|
+
error "Cannot use % on #{a.type}"
|
|
315
337
|
end
|
|
316
338
|
when :pow
|
|
317
339
|
b, a = pop_from_stack, pop_from_stack
|
|
@@ -319,7 +341,55 @@ class VM
|
|
|
319
341
|
res = (call a.value.fields["__pow"], b.value)
|
|
320
342
|
push_to_stack (Variable.new res, (get_type res), @global)
|
|
321
343
|
else
|
|
322
|
-
error "Cannot
|
|
344
|
+
error "Cannot use ^ on #{a.type}"
|
|
345
|
+
end
|
|
346
|
+
when :eq
|
|
347
|
+
b, a = pop_from_stack, pop_from_stack
|
|
348
|
+
if a.value.fields["__eq"]
|
|
349
|
+
res = (call a.value.fields["__eq"], 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 :ne
|
|
355
|
+
b, a = pop_from_stack, pop_from_stack
|
|
356
|
+
if a.value.fields["__neq"]
|
|
357
|
+
res = (call a.value.fields["__neq"], 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 :lt
|
|
363
|
+
b, a = pop_from_stack, pop_from_stack
|
|
364
|
+
if a.value.fields["__lt"]
|
|
365
|
+
res = (call a.value.fields["__lt"], 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 :gt
|
|
371
|
+
b, a = pop_from_stack, pop_from_stack
|
|
372
|
+
if a.value.fields["__gt"]
|
|
373
|
+
res = (call a.value.fields["__gt"], b.value)
|
|
374
|
+
push_to_stack (Variable.new res, (get_type res), @global)
|
|
375
|
+
else
|
|
376
|
+
error "Cannot use > on #{a.type}"
|
|
377
|
+
end
|
|
378
|
+
when :le
|
|
379
|
+
b, a = pop_from_stack, pop_from_stack
|
|
380
|
+
if a.value.fields["__le"]
|
|
381
|
+
res = (call a.value.fields["__le"], b.value)
|
|
382
|
+
push_to_stack (Variable.new res, (get_type res), @global)
|
|
383
|
+
else
|
|
384
|
+
error "Cannot use <= on #{a.type}"
|
|
385
|
+
end
|
|
386
|
+
when :ge
|
|
387
|
+
b, a = pop_from_stack, pop_from_stack
|
|
388
|
+
if a.value.fields["__ge"]
|
|
389
|
+
res = (call a.value.fields["__ge"], b.value)
|
|
390
|
+
push_to_stack (Variable.new res, (get_type res), @global)
|
|
391
|
+
else
|
|
392
|
+
error "Cannot use >= on #{a.type}"
|
|
323
393
|
end
|
|
324
394
|
when :jmpi
|
|
325
395
|
val = pop_from_stack
|