sardonyx 0.1.6 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a1e2303504b396f3aea3b179bb7fd511de36490784a1763599712fb53c2a9448
4
- data.tar.gz: 4092253d393f90075180acc3cad50bcd1f5d2a2377e3a0d2d853b93cb9902d72
3
+ metadata.gz: 4cc308366fad5b10c92dec85090d721e7e609d461aec60e7cf8f319a5a542a0e
4
+ data.tar.gz: 033f7e434ffae2192f194ef1a7933c52f400be53ef4bdeb9b7719afecd56dfbb
5
5
  SHA512:
6
- metadata.gz: 3f89fd078c871701eee5ddc6f8a8306666da58d6ffc4e6050e6bd2a80dbb2c31903dfb3c86c7155e5f1477104305016b64b8bcf336b7c0d9b63b881582bf3813
7
- data.tar.gz: b9ebed85a007ca9ca7872b9db44e75173fb6db412d8a88c00f968a1685c47084b71f7baf5857a786b836ab905c84b74970b67f76eda75c4d633aa788ecfe4e88
6
+ metadata.gz: 5ecf245af7c547d02cefe94b2d2bd0279051e14977c4f4b4e5ee2c156fce6cd3f3407fb2474dca295fdaf6d08704bd3065ff28624700dccd40c0b679b89bd05c
7
+ data.tar.gz: 2575dc2a542a141742645687f0c4bb0e93995760d621295be3b6fbd9907159221627c6ae622b33ec14db57f4335b1e0b8022578e8c71c241b7986919ffacbff3
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 v 0.0.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 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
@@ -10,7 +10,7 @@ module Parser
10
10
  /\Aobject/ => :object,
11
11
  /\Anew/ => :new,
12
12
  /\Arequire/ => :require,
13
- /\A(<|>|<=|>=|==)/ => :op,
13
+ /\A(<|>|<=|>=|==|!=)/ => :op,
14
14
  /\A(\+|-|\*|\/|%)?=/ => :eq,
15
15
  /\A(\+|-|\*|\/|%)/ => :op,
16
16
  /\A-?[0-9]+/ => :number,
@@ -36,7 +36,7 @@ class NativeFn < DataType
36
36
  def initialize(arity, val)
37
37
  @internal = val
38
38
  @fields = {
39
- "__call" => (NativeFnInternal.new (Proc.new do |args|
39
+ "__call" => (NativeFnInternal.new (lambda do |args, scope|
40
40
  args.reverse!
41
41
  @internal.call *args
42
42
  end)),
@@ -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,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
@@ -272,34 +358,40 @@ class List < DataType
272
358
  iter
273
359
  end)),
274
360
  "__add" => (NativeFnInternal.new (Proc.new do |other|
275
- add other
361
+ add other[0]
276
362
  end)),
277
363
  "__mul" => (NativeFnInternal.new (Proc.new do |other|
278
- mul other
364
+ mul other[0]
279
365
  end)),
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
288
- s = "["
380
+ s = ""
289
381
  @internal.each do |item|
290
382
  s += (stringify item) + ", "
291
383
  end
292
- s = s[0..-3]
384
+ s = "[" + s[0..-3]
293
385
  s += "]"
294
386
  Str.new s
295
387
  end
296
388
 
297
389
  def as_code_string
298
- s = "["
390
+ s = ""
299
391
  @internal.each do |item|
300
392
  s += (codify item) + ", "
301
393
  end
302
- s = s[0..-3]
394
+ s = "[" + s[0..-3]
303
395
  s += "]"
304
396
  Str.new s
305
397
  end
@@ -314,12 +406,12 @@ class List < DataType
314
406
  if val
315
407
  return val
316
408
  else
317
- return Variable.new Nil.new
409
+ return Variable.new Nil.new, :nil, @internal[0].scope
318
410
  end
319
411
  end
320
412
 
321
413
  def add(other)
322
- return List.new [*@internal, other]
414
+ return List.new [*@internal, (Variable.new other, (get_type other), @internal[0].scope)]
323
415
  end
324
416
 
325
417
  def mul(other)
@@ -358,10 +450,16 @@ class Function < DataType
358
450
  @internal = body
359
451
 
360
452
  @fields = {
361
- "__call" => (NativeFnInternal.new (Proc.new do |args, scope|
453
+ "__call" => (NativeFnInternal.new (lambda do |args, scope|
362
454
  call args, scope
363
455
  end)),
364
- "__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))
365
463
  }
366
464
  end
367
465
 
@@ -387,7 +485,13 @@ class Block < DataType
387
485
  "__call" => (NativeFnInternal.new (Proc.new do |args, scope|
388
486
  call args, scope
389
487
  end)),
390
- "__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))
391
495
  }
392
496
  end
393
497
 
@@ -414,7 +518,13 @@ class Obj < DataType
414
518
  "__new" => (NativeFnInternal.new (Proc.new do |args, scope|
415
519
  _new args, scope
416
520
  end)),
417
- "__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))
418
528
  }
419
529
  end
420
530
 
@@ -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
@@ -35,12 +35,27 @@ class VM
35
35
  when Function
36
36
  return val.value.fields["__call"].call args.map { |x| to_var x }, val.scope
37
37
  else
38
- return val.value.fields["__call"].call *args
38
+ return val.value.fields["__call"].call args, val.scope
39
39
  end
40
40
  elsif val.respond_to? :fields
41
41
  return val.fields["__call"].call *args
42
42
  else
43
- return (to_var val).value.call *args
43
+ return (to_var val).value.call args
44
+ end
45
+ end
46
+
47
+ def from_rb(val)
48
+ case val
49
+ when Integer
50
+ to_var (Int.new val)
51
+ when String
52
+ to_var (Str.new val)
53
+ when Float
54
+ to_var (Float.new val)
55
+ when Array
56
+ to_var (List.new val.map { |v| from_rb v })
57
+ when Nil
58
+ to_var (Nil_.new)
44
59
  end
45
60
  end
46
61
 
@@ -84,7 +99,7 @@ class VM
84
99
  @global = GLOBAL_SCOPE.new
85
100
  @global.add_fn "__rb_call", (Variable.new (NativeFn.new 2, (Proc.new do |name, args|
86
101
  args = (codify args)[1..-2]
87
- eval "#{name.value.internal}(#{args})"
102
+ from_rb eval "#{name.value.internal}(#{args})"
88
103
  end)), :fn, @global)
89
104
  @stack = []
90
105
  @byte_pos = 0
@@ -135,7 +150,13 @@ class VM
135
150
  0x30 => :object,
136
151
  0x31 => :new,
137
152
  0x32 => :block,
138
- 0x33 => :end
153
+ 0x33 => :end,
154
+ 0x34 => :eq,
155
+ 0x35 => :ne,
156
+ 0x36 => :lt,
157
+ 0x37 => :gt,
158
+ 0x38 => :le,
159
+ 0x39 => :ge
139
160
  }
140
161
  bytes = []
141
162
  begin
@@ -264,7 +285,7 @@ class VM
264
285
  res = (call a.value.fields["__add"], b.value)
265
286
  push_to_stack (to_var res)
266
287
  else
267
- error "Cannot add to #{a.type}"
288
+ error "Cannot use + on #{a.type}"
268
289
  end
269
290
  when :sub
270
291
  b, a = pop_from_stack, pop_from_stack
@@ -272,7 +293,7 @@ class VM
272
293
  res = (call a.value.fields["__sub"], b.value)
273
294
  push_to_stack (Variable.new res, (get_type res), @global)
274
295
  else
275
- error "Cannot subtract from #{a.type}"
296
+ error "Cannot use - on #{a.type}"
276
297
  end
277
298
  when :mul
278
299
  b, a = pop_from_stack, pop_from_stack
@@ -280,7 +301,7 @@ class VM
280
301
  res = (call a.value.fields["__mul"], b.value)
281
302
  push_to_stack (Variable.new res, (get_type res), @global)
282
303
  else
283
- error "Cannot multiply #{a.type}"
304
+ error "Cannot use * on #{a.type}"
284
305
  end
285
306
  when :div
286
307
  b, a = pop_from_stack, pop_from_stack
@@ -288,7 +309,7 @@ class VM
288
309
  res = (call a.value.fields["__div"], b.value)
289
310
  push_to_stack (Variable.new res, (get_type res), @global)
290
311
  else
291
- error "Cannot divide #{a.type}"
312
+ error "Cannot use / on #{a.type}"
292
313
  end
293
314
  when :mod
294
315
  b, a = pop_from_stack, pop_from_stack
@@ -296,7 +317,7 @@ class VM
296
317
  res = (call a.value.fields["__mod"], b.value)
297
318
  push_to_stack (Variable.new res, (get_type res), @global)
298
319
  else
299
- error "Cannot modulo #{a.type}"
320
+ error "Cannot use % on #{a.type}"
300
321
  end
301
322
  when :pow
302
323
  b, a = pop_from_stack, pop_from_stack
@@ -304,7 +325,55 @@ class VM
304
325
  res = (call a.value.fields["__pow"], b.value)
305
326
  push_to_stack (Variable.new res, (get_type res), @global)
306
327
  else
307
- error "Cannot exponentiate #{a.type}"
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}"
308
377
  end
309
378
  when :jmpi
310
379
  val = pop_from_stack
@@ -356,16 +425,13 @@ class VM
356
425
  end
357
426
  args << this
358
427
  end
359
- f = Proc.new do |args, scope|
360
- call val, args, scope
361
- end
362
428
  scope = nil
363
429
  begin
364
430
  scope = val.scope
365
431
  rescue
366
432
  scope = @global
367
433
  end
368
- ret = f.call args, scope
434
+ ret = call val, *args
369
435
  if ret
370
436
  push_to_stack ret
371
437
  end
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.6
4
+ version: 0.1.83
5
5
  platform: ruby
6
6
  authors:
7
7
  - sugarfi