sardonyx 0.1.8 → 0.1.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 92d4b64ff337b649c89dd0ec78dded37c71f508dfd3e9705127ee8d186fae2a4
4
- data.tar.gz: e087de5f23ad9c9fc9ff85e0c7ffcfaaa3cdbaf19cffecaf589cb2926c1f2dc7
3
+ metadata.gz: 2c327818bc6930f9d380e18ca947ea03abcc10ce8069118649a7b181944335a4
4
+ data.tar.gz: ef98de56189f67a7c6d5ddbe81a6aff5bedfe2d8323df69145ee432190a17480
5
5
  SHA512:
6
- metadata.gz: bc7fd1b23d60b16e2a9070fc172ce67fcf64fd1c033418e8d7693348a573635d264d94b8d0ed7809fa38f98546e8b98a9efd9d8e9ccda710de790855366f120c
7
- data.tar.gz: b61e5f8f7d035638fb14f1d15e017260263b50daeb48835e774f9399e15f8ce016b00b462c1c54681d061a1378bb47549a93cd88d658def4e45b8f1c3ed01c90
6
+ metadata.gz: cae9757402ebf5dce3e44aa0df039d406c27b875dcac20b49f34493321c33f04a77140d09a5dfdbe2bb2ed3f5b84e4b051f769d461f20ad7ce72392e79ab9bfd
7
+ data.tar.gz: 6f8104b6729eff7b2e3d572ac2620a2f4589187fcdd73d152b166166fa92e2d538823987e224633fbd9f6493a69d91994bca5c5cb46c145de20e9a8b0173fbe3
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.8"
18
+ puts "Sardonyx v0.1.9"
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 stringify val
32
+ puts vm.stringify val
33
33
  end
34
34
  end
35
35
  end
@@ -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 += "\x29#{i.size}\x18" + i
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
@@ -10,11 +10,13 @@ module Parser
10
10
  /\Aobject/ => :object,
11
11
  /\Anew/ => :new,
12
12
  /\Arequire/ => :require,
13
- /\A(<|>|<=|>=|==)/ => :op,
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
- break
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) || self.parse_float(tokens) || (self.parse_name tokens) || (self.parse_number tokens) || (self.parse_list tokens) || (self.parse_string tokens) || (self.parse_nil 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
- 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]
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[2..tokens.size]
186
- total = 2
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
@@ -195,14 +225,15 @@ module Parser
195
225
  total += part
196
226
  tokens = tokens[part..tokens.size]
197
227
  args << arg
198
- total += 1
199
228
  if self.expect tokens, :rpar
200
229
  tokens = tokens[1..tokens.size]
230
+ total += 1
201
231
  break
202
232
  end
203
233
  unless (self.expect tokens, :comma)
204
234
  return nil
205
235
  end
236
+ total += 1
206
237
  tokens = tokens[1..tokens.size]
207
238
  end
208
239
  return [ (Node.new :call, callee, args), total ]
@@ -330,26 +361,92 @@ module Parser
330
361
  return [ (Node.new :for, e, [name, block]), total ]
331
362
  end
332
363
 
333
- def self.parse_op(tokens)
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)
334
379
  total = 0
335
- unless self.parse_literal tokens
380
+ unless self.parse_factor tokens
336
381
  return nil
337
382
  end
338
- lhs, part = self.parse_literal tokens
383
+ lhs, part = self.parse_factor tokens
339
384
  total += part
340
385
  tokens = tokens[part..tokens.size]
341
- unless self.expect tokens, :op
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
+ def self.parse_op(tokens)
416
+ total = 0
417
+ unless self.parse_term tokens
342
418
  return nil
343
419
  end
420
+ lhs, part = self.parse_term tokens
421
+ total += part
422
+ tokens = tokens[part..tokens.size]
423
+ unless self.expect tokens, :l1op
424
+ return [lhs, part]
425
+ end
344
426
  op = tokens[0][0]
345
427
  total += 1
346
428
  tokens = tokens[1..tokens.size]
347
- unless self.parse_expr tokens
429
+ unless self.parse_term tokens
348
430
  return nil
349
431
  end
350
- rhs, part = self.parse_expr tokens
432
+ rhs, part = self.parse_term tokens
351
433
  total += part
352
- return [ (Node.new :op, op, [lhs, rhs]), total]
434
+ tokens = tokens[part..tokens.size]
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]
353
450
  end
354
451
 
355
452
  def self.parse_assign(tokens)
@@ -483,7 +580,17 @@ module Parser
483
580
  end
484
581
 
485
582
  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)
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)
487
594
  end
488
595
 
489
596
  def self.parse(tokens, path)
@@ -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 "\"#{@internal}\"")
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
 
@@ -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
- (val.value.fields["__as_code_string"].call).internal
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 (Str.new val)
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,14 @@ 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,
169
+ 0x28 => :pow,
154
170
  }
155
171
  bytes = []
156
172
  begin
@@ -264,12 +280,19 @@ class VM
264
280
  vals << pop_from_stack
265
281
  end
266
282
  vals.reverse!
267
- push_to_stack Variable.new (List.new vals), :list, @global
283
+ push_to_stack Variable.new (List.new vals, @global), :list, @global
268
284
  when :block
269
285
  size = get_string.to_i
270
286
  body =
271
287
  ((load_bytes size, false).map { |e| e.chr }).join ""
272
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
273
296
  when :nil
274
297
  push_to_stack Variable.new (Nil.new), :nil, @global
275
298
  end
@@ -279,7 +302,7 @@ class VM
279
302
  res = (call a.value.fields["__add"], b.value)
280
303
  push_to_stack (to_var res)
281
304
  else
282
- error "Cannot add to #{a.type}"
305
+ error "Cannot use + on #{a.type}"
283
306
  end
284
307
  when :sub
285
308
  b, a = pop_from_stack, pop_from_stack
@@ -287,7 +310,7 @@ class VM
287
310
  res = (call a.value.fields["__sub"], b.value)
288
311
  push_to_stack (Variable.new res, (get_type res), @global)
289
312
  else
290
- error "Cannot subtract from #{a.type}"
313
+ error "Cannot use - on #{a.type}"
291
314
  end
292
315
  when :mul
293
316
  b, a = pop_from_stack, pop_from_stack
@@ -295,7 +318,7 @@ class VM
295
318
  res = (call a.value.fields["__mul"], b.value)
296
319
  push_to_stack (Variable.new res, (get_type res), @global)
297
320
  else
298
- error "Cannot multiply #{a.type}"
321
+ error "Cannot use * on #{a.type}"
299
322
  end
300
323
  when :div
301
324
  b, a = pop_from_stack, pop_from_stack
@@ -303,7 +326,7 @@ class VM
303
326
  res = (call a.value.fields["__div"], b.value)
304
327
  push_to_stack (Variable.new res, (get_type res), @global)
305
328
  else
306
- error "Cannot divide #{a.type}"
329
+ error "Cannot use / on #{a.type}"
307
330
  end
308
331
  when :mod
309
332
  b, a = pop_from_stack, pop_from_stack
@@ -311,7 +334,7 @@ class VM
311
334
  res = (call a.value.fields["__mod"], b.value)
312
335
  push_to_stack (Variable.new res, (get_type res), @global)
313
336
  else
314
- error "Cannot modulo #{a.type}"
337
+ error "Cannot use % on #{a.type}"
315
338
  end
316
339
  when :pow
317
340
  b, a = pop_from_stack, pop_from_stack
@@ -319,7 +342,55 @@ class VM
319
342
  res = (call a.value.fields["__pow"], b.value)
320
343
  push_to_stack (Variable.new res, (get_type res), @global)
321
344
  else
322
- error "Cannot exponentiate #{a.type}"
345
+ error "Cannot use ^ on #{a.type}"
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}"
323
394
  end
324
395
  when :jmpi
325
396
  val = pop_from_stack
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sardonyx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - sugarfi