sardonyx 0.1.7 → 0.1.84

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: '023920f36be61ea604ad6c69e66b71c2f9f9c0696b0a5679e129d7b284e1345e'
4
- data.tar.gz: 88f14b9b1aa7a253f564c8e40e9e1ad4589b62cc6ef1ead28a4bc08461017db6
3
+ metadata.gz: f381674f5e870ec9ac7fe8f9ce699b1e851c7314ad9480201710bcd32e665185
4
+ data.tar.gz: c42b1e66d327aa8bc0e39075c3778bd3b40beb7c82d47edce91a601418a82408
5
5
  SHA512:
6
- metadata.gz: eb029ebb6e35edfa9daf634b6553c69d4adf109af83916341b1809b00e3abbd1bc07eb31175ad94c95742f367c2e5cc06d1fc0e61ce74d4329022895e189c077
7
- data.tar.gz: c955957d5e26dd5559941c4a24f14ab8d0ba5a39f82c94fd891a3afd6f24be09362bbf83f11b61bc552c277fb52e2ced701e9c6cd3facae9955750d1b5b98f9e
6
+ metadata.gz: 43c77a34a4edf0b6a87af9183004a57d8cb285f115159240b5798c8ce37723e2f9018c5f5a630d2f7c102cbf50a42f181eee30685f74cf2ede839f1e5a224a72
7
+ data.tar.gz: 83d259340d9dec63f6696de3a3d76832bc7c98880634df1139f5cd89042d7c7eb5c8cad1d27062fa027174344583a593d37698c96aac19328c7965b047d4ea34
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.7"
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,
@@ -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,14 +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
5
  def codify(val)
14
6
  if val.value.fields["__as_code_string"]
15
7
  (val.value.fields["__as_code_string"].call).internal
@@ -28,6 +20,14 @@ class VM
28
20
  true
29
21
  end
30
22
  end
23
+
24
+ def stringify(val)
25
+ if val.value.fields["__as_string"]
26
+ (call val.value.fields["__as_string"], [], val.scope).internal
27
+ else
28
+ val.value.to_s
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
@@ -43,6 +43,21 @@ class VM
43
43
  return (to_var val).value.call args
44
44
  end
45
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)
59
+ end
60
+ end
46
61
 
47
62
  def callable(val)
48
63
  if val.respond_to? :value and val.value.respond_to? :fields
@@ -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
@@ -120,6 +135,7 @@ class VM
120
135
  0x24 => :sub,
121
136
  0x25 => :mul,
122
137
  0x26 => :div,
138
+ 0x27 => :mod,
123
139
  0x12 => :bool,
124
140
  0x13 => :int,
125
141
  0x14 => :str,
@@ -135,7 +151,13 @@ class VM
135
151
  0x30 => :object,
136
152
  0x31 => :new,
137
153
  0x32 => :block,
138
- 0x33 => :end
154
+ 0x33 => :end,
155
+ 0x34 => :eq,
156
+ 0x35 => :ne,
157
+ 0x36 => :lt,
158
+ 0x37 => :gt,
159
+ 0x38 => :le,
160
+ 0x39 => :ge
139
161
  }
140
162
  bytes = []
141
163
  begin
@@ -264,7 +286,7 @@ class VM
264
286
  res = (call a.value.fields["__add"], b.value)
265
287
  push_to_stack (to_var res)
266
288
  else
267
- error "Cannot add to #{a.type}"
289
+ error "Cannot use + on #{a.type}"
268
290
  end
269
291
  when :sub
270
292
  b, a = pop_from_stack, pop_from_stack
@@ -272,7 +294,7 @@ class VM
272
294
  res = (call a.value.fields["__sub"], b.value)
273
295
  push_to_stack (Variable.new res, (get_type res), @global)
274
296
  else
275
- error "Cannot subtract from #{a.type}"
297
+ error "Cannot use - on #{a.type}"
276
298
  end
277
299
  when :mul
278
300
  b, a = pop_from_stack, pop_from_stack
@@ -280,7 +302,7 @@ class VM
280
302
  res = (call a.value.fields["__mul"], b.value)
281
303
  push_to_stack (Variable.new res, (get_type res), @global)
282
304
  else
283
- error "Cannot multiply #{a.type}"
305
+ error "Cannot use * on #{a.type}"
284
306
  end
285
307
  when :div
286
308
  b, a = pop_from_stack, pop_from_stack
@@ -288,7 +310,7 @@ class VM
288
310
  res = (call a.value.fields["__div"], b.value)
289
311
  push_to_stack (Variable.new res, (get_type res), @global)
290
312
  else
291
- error "Cannot divide #{a.type}"
313
+ error "Cannot use / on #{a.type}"
292
314
  end
293
315
  when :mod
294
316
  b, a = pop_from_stack, pop_from_stack
@@ -296,7 +318,7 @@ class VM
296
318
  res = (call a.value.fields["__mod"], b.value)
297
319
  push_to_stack (Variable.new res, (get_type res), @global)
298
320
  else
299
- error "Cannot modulo #{a.type}"
321
+ error "Cannot use % on #{a.type}"
300
322
  end
301
323
  when :pow
302
324
  b, a = pop_from_stack, pop_from_stack
@@ -304,7 +326,55 @@ class VM
304
326
  res = (call a.value.fields["__pow"], b.value)
305
327
  push_to_stack (Variable.new res, (get_type res), @global)
306
328
  else
307
- error "Cannot exponentiate #{a.type}"
329
+ error "Cannot use ^ on #{a.type}"
330
+ end
331
+ when :eq
332
+ b, a = pop_from_stack, pop_from_stack
333
+ if a.value.fields["__eq"]
334
+ res = (call a.value.fields["__eq"], b.value)
335
+ push_to_stack (Variable.new res, (get_type res), @global)
336
+ else
337
+ error "Cannot use == on #{a.type}"
338
+ end
339
+ when :ne
340
+ b, a = pop_from_stack, pop_from_stack
341
+ if a.value.fields["__neq"]
342
+ res = (call a.value.fields["__neq"], b.value)
343
+ push_to_stack (Variable.new res, (get_type res), @global)
344
+ else
345
+ error "Cannot use != on #{a.type}"
346
+ end
347
+ when :lt
348
+ b, a = pop_from_stack, pop_from_stack
349
+ if a.value.fields["__lt"]
350
+ res = (call a.value.fields["__lt"], 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 :gt
356
+ b, a = pop_from_stack, pop_from_stack
357
+ if a.value.fields["__gt"]
358
+ res = (call a.value.fields["__gt"], 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 :le
364
+ b, a = pop_from_stack, pop_from_stack
365
+ if a.value.fields["__le"]
366
+ res = (call a.value.fields["__le"], 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 :ge
372
+ b, a = pop_from_stack, pop_from_stack
373
+ if a.value.fields["__ge"]
374
+ res = (call a.value.fields["__ge"], b.value)
375
+ push_to_stack (Variable.new res, (get_type res), @global)
376
+ else
377
+ error "Cannot use >= on #{a.type}"
308
378
  end
309
379
  when :jmpi
310
380
  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.7
4
+ version: 0.1.84
5
5
  platform: ruby
6
6
  authors:
7
7
  - sugarfi