sardonyx 0.1.8 → 0.1.841

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: 54495fa9b457fb450d812a6a09b57a3284b948e8e93e44ab97521a241bef0a3b
4
+ data.tar.gz: '06619369fdff53a992b948401115f98ba11319ff0d06bd0a29d73030e39e892e'
5
5
  SHA512:
6
- metadata.gz: bc7fd1b23d60b16e2a9070fc172ce67fcf64fd1c033418e8d7693348a573635d264d94b8d0ed7809fa38f98546e8b98a9efd9d8e9ccda710de790855366f120c
7
- data.tar.gz: b61e5f8f7d035638fb14f1d15e017260263b50daeb48835e774f9399e15f8ce016b00b462c1c54681d061a1378bb47549a93cd88d658def4e45b8f1c3ed01c90
6
+ metadata.gz: ceb7a82b5e59a72b23063caa984e26ce4f3d9d44d41e826d1702613bdad7634f06606dd622c6751aa6f477d042a58c2191cda025bebea6aeb84c356ca9fec984
7
+ data.tar.gz: 71514ddd0dfe83eb7542b5686b194c396878f8d9a6da7652b7ca1f2f38cb4f6c114b31e84293df9d37d73777df4fa248a69c37170524d9c009c1d7c8de76ceb6
data/bin/sdx CHANGED
@@ -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,
@@ -113,6 +113,23 @@ module Parser
113
113
  end
114
114
  end
115
115
 
116
+ def self.parse_parens(tokens)
117
+ if self.expect tokens, :lpar
118
+ tokens = tokens[1..tokens.size]
119
+ unless self.parse_expr tokens
120
+ return nil
121
+ end
122
+ e, part = self.parse_expr tokens
123
+ tokens = tokens[part..tokens.size]
124
+ unless self.expect tokens, :rpar
125
+ return nil
126
+ end
127
+ return [e, part + 2]
128
+ else
129
+ return nil
130
+ end
131
+ end
132
+
116
133
  def self.parse_list(tokens)
117
134
  unless (self.expect tokens, :lbrack)
118
135
  return nil
@@ -155,7 +172,7 @@ module Parser
155
172
  while true
156
173
  if self.expect tokens, :rbrace
157
174
  total += 1
158
- break
175
+ return [ (Node.new :block, "", children), total ]
159
176
  end
160
177
  e = self.parse_expr tokens
161
178
  if e
@@ -167,23 +184,26 @@ module Parser
167
184
  Kernel.exit 1
168
185
  end
169
186
  end
187
+ total += 1
170
188
  [ (Node.new :block, "", children), total ]
171
189
  end
172
190
 
173
191
  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)
192
+ (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) || (self.parse_parens tokens)
175
193
  end
176
194
 
177
195
  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]
196
+ unless (self.parse_literal tokens)
197
+ return nil
198
+ end
199
+ callee = (self.parse_literal tokens)
200
+ total = callee[1]
201
+ tokens = tokens[total..tokens.size]
202
+ callee = callee[0]
203
+ if self.expect tokens, :lpar
184
204
  args = []
185
- tokens = tokens[2..tokens.size]
186
- total = 2
205
+ tokens = tokens[1..tokens.size]
206
+ total += 1
187
207
  if self.expect tokens, :rpar
188
208
  return [ (Node.new :call, callee, args), total + 1 ]
189
209
  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)
@@ -361,7 +453,13 @@ class Function < DataType
361
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,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
@@ -28,6 +24,14 @@ class VM
28
24
  true
29
25
  end
30
26
  end
27
+
28
+ def stringify(val)
29
+ if val.value.fields["__as_string"]
30
+ (call val.value.fields["__as_string"], [], val.scope).internal
31
+ else
32
+ val.value.to_s
33
+ end
34
+ end
31
35
 
32
36
  def call(val, *args)
33
37
  if val.respond_to? :value and val.value.respond_to? :fields
@@ -51,7 +55,7 @@ class VM
51
55
  when String
52
56
  to_var (Str.new val)
53
57
  when Float
54
- to_var (Str.new val)
58
+ to_var (Float.new val)
55
59
  when Array
56
60
  to_var (List.new val.map { |v| from_rb v })
57
61
  when Nil
@@ -135,6 +139,7 @@ class VM
135
139
  0x24 => :sub,
136
140
  0x25 => :mul,
137
141
  0x26 => :div,
142
+ 0x27 => :mod,
138
143
  0x12 => :bool,
139
144
  0x13 => :int,
140
145
  0x14 => :str,
@@ -150,7 +155,13 @@ class VM
150
155
  0x30 => :object,
151
156
  0x31 => :new,
152
157
  0x32 => :block,
153
- 0x33 => :end
158
+ 0x33 => :end,
159
+ 0x34 => :eq,
160
+ 0x35 => :ne,
161
+ 0x36 => :lt,
162
+ 0x37 => :gt,
163
+ 0x38 => :le,
164
+ 0x39 => :ge
154
165
  }
155
166
  bytes = []
156
167
  begin
@@ -279,7 +290,7 @@ class VM
279
290
  res = (call a.value.fields["__add"], b.value)
280
291
  push_to_stack (to_var res)
281
292
  else
282
- error "Cannot add to #{a.type}"
293
+ error "Cannot use + on #{a.type}"
283
294
  end
284
295
  when :sub
285
296
  b, a = pop_from_stack, pop_from_stack
@@ -287,7 +298,7 @@ class VM
287
298
  res = (call a.value.fields["__sub"], b.value)
288
299
  push_to_stack (Variable.new res, (get_type res), @global)
289
300
  else
290
- error "Cannot subtract from #{a.type}"
301
+ error "Cannot use - on #{a.type}"
291
302
  end
292
303
  when :mul
293
304
  b, a = pop_from_stack, pop_from_stack
@@ -295,7 +306,7 @@ class VM
295
306
  res = (call a.value.fields["__mul"], b.value)
296
307
  push_to_stack (Variable.new res, (get_type res), @global)
297
308
  else
298
- error "Cannot multiply #{a.type}"
309
+ error "Cannot use * on #{a.type}"
299
310
  end
300
311
  when :div
301
312
  b, a = pop_from_stack, pop_from_stack
@@ -303,7 +314,7 @@ class VM
303
314
  res = (call a.value.fields["__div"], b.value)
304
315
  push_to_stack (Variable.new res, (get_type res), @global)
305
316
  else
306
- error "Cannot divide #{a.type}"
317
+ error "Cannot use / on #{a.type}"
307
318
  end
308
319
  when :mod
309
320
  b, a = pop_from_stack, pop_from_stack
@@ -311,7 +322,7 @@ class VM
311
322
  res = (call a.value.fields["__mod"], b.value)
312
323
  push_to_stack (Variable.new res, (get_type res), @global)
313
324
  else
314
- error "Cannot modulo #{a.type}"
325
+ error "Cannot use % on #{a.type}"
315
326
  end
316
327
  when :pow
317
328
  b, a = pop_from_stack, pop_from_stack
@@ -319,7 +330,55 @@ class VM
319
330
  res = (call a.value.fields["__pow"], b.value)
320
331
  push_to_stack (Variable.new res, (get_type res), @global)
321
332
  else
322
- error "Cannot exponentiate #{a.type}"
333
+ error "Cannot use ^ on #{a.type}"
334
+ end
335
+ when :eq
336
+ b, a = pop_from_stack, pop_from_stack
337
+ if a.value.fields["__eq"]
338
+ res = (call a.value.fields["__eq"], b.value)
339
+ push_to_stack (Variable.new res, (get_type res), @global)
340
+ else
341
+ error "Cannot use == on #{a.type}"
342
+ end
343
+ when :ne
344
+ b, a = pop_from_stack, pop_from_stack
345
+ if a.value.fields["__neq"]
346
+ res = (call a.value.fields["__neq"], b.value)
347
+ push_to_stack (Variable.new res, (get_type res), @global)
348
+ else
349
+ error "Cannot use != on #{a.type}"
350
+ end
351
+ when :lt
352
+ b, a = pop_from_stack, pop_from_stack
353
+ if a.value.fields["__lt"]
354
+ res = (call a.value.fields["__lt"], b.value)
355
+ push_to_stack (Variable.new res, (get_type res), @global)
356
+ else
357
+ error "Cannot use < on #{a.type}"
358
+ end
359
+ when :gt
360
+ b, a = pop_from_stack, pop_from_stack
361
+ if a.value.fields["__gt"]
362
+ res = (call a.value.fields["__gt"], b.value)
363
+ push_to_stack (Variable.new res, (get_type res), @global)
364
+ else
365
+ error "Cannot use > on #{a.type}"
366
+ end
367
+ when :le
368
+ b, a = pop_from_stack, pop_from_stack
369
+ if a.value.fields["__le"]
370
+ res = (call a.value.fields["__le"], b.value)
371
+ push_to_stack (Variable.new res, (get_type res), @global)
372
+ else
373
+ error "Cannot use <= on #{a.type}"
374
+ end
375
+ when :ge
376
+ b, a = pop_from_stack, pop_from_stack
377
+ if a.value.fields["__ge"]
378
+ res = (call a.value.fields["__ge"], b.value)
379
+ push_to_stack (Variable.new res, (get_type res), @global)
380
+ else
381
+ error "Cannot use >= on #{a.type}"
323
382
  end
324
383
  when :jmpi
325
384
  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.841
5
5
  platform: ruby
6
6
  authors:
7
7
  - sugarfi