sardonyx 0.3.1 → 0.3.6

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: cb1abb1054bac034790200a964982214b49e23fc6d7d770d0fd04891dfa6e347
4
- data.tar.gz: ae202ff163082b2cc8796a9ff961a22a0acceb31f70cfdd9b02736bc2c2c6e56
3
+ metadata.gz: d0c5b5dabcbc157b1423ad023f21c3a892937bf6046050a83da1470e7ce724a1
4
+ data.tar.gz: f386cc4507d301b80f807a231ef2bd8af4140114bb7d3760bdf2847fdd73b843
5
5
  SHA512:
6
- metadata.gz: 62fdce49d54f1a3a57b8865390a2c2145d043db991c4982821299f726b2d268e15d5cd675b4ecaa05b03d2b159ec66d1fa5f03ddcba2b0d30733677d26465201
7
- data.tar.gz: 1379d7546a69f008fd3c9b1d632ac5f42e1dfc887972c59dec4adf1b00d88251a02e8551d5fd8a03260cfe680ab860540a40191388a3bf051f4579d3b5f8d3b6
6
+ metadata.gz: 714886d2d100b7ac440f7dd56cb8e9bf0f7ae880d67778775fe900b8b179f99f9d47f5404cb313515923a32f2fbb2d0e71527da296357b3ca3e5ddbb12963120
7
+ data.tar.gz: 5fc4e31a81de969b3714ab40b1c12e4c792d27efbd96c36292d1cea570cbae0de0865b7b8e412c560113cdd910aa5d7c80df834cf12df4fc7a3b4a9b402704d8
data/bin/sdx CHANGED
@@ -5,7 +5,7 @@ require "sdx/vm/vm"
5
5
  require "stringio"
6
6
  require "readline"
7
7
 
8
- if (ENV.fetch("SDX_PATH", "").split ":")
8
+ if (ENV.fetch("SDX_PATH", "").split ":") == []
9
9
  puts "\x1b[0;33mLooks like you don't have anything in your SDX_PATH! You should install the stdlib at https://github.com/SardonyxLang/SardonyxStd.\x1b[0;0m"
10
10
  end
11
11
 
@@ -22,7 +22,7 @@ if ARGV.size == 1
22
22
  else
23
23
  path = [(File.expand_path Dir.pwd), *(ENV.fetch("SDX_PATH", "").split ":")]
24
24
  vm = VM.new StringIO.new ""
25
- puts "Sardonyx v0.3.1"
25
+ puts "Sardonyx v0.3.6"
26
26
  puts "Type :help for help, or :exit to exit"
27
27
  loop do
28
28
  begin
@@ -50,8 +50,8 @@ else
50
50
  vm.interpret false
51
51
  val = vm.stack[-1]
52
52
  vm.clear
53
- if val
54
- puts vm.stringify val
53
+ if val and State::state == :ok
54
+ puts stringify val
55
55
  end
56
56
  end
57
57
  end
@@ -197,6 +197,9 @@ module Compiler
197
197
  bc += self.encode_node item
198
198
  end
199
199
  bc += "\x21\x2c#{node.children.size}\x18"
200
+ when :return
201
+ bc += self.encode_node node.value
202
+ bc += "\x16"
200
203
  else
201
204
  nil
202
205
  end
@@ -23,6 +23,7 @@ module Parser
23
23
  /\Aobject/ => :object,
24
24
  /\Anew/ => :new,
25
25
  /\Arequire/ => :require,
26
+ /\Areturn/ => :return,
26
27
  /\A(true|false)/ => :bool,
27
28
  /\A-?[0-9]+\.[0-9]+/ => :float,
28
29
  /\A-?[0-9]+/ => :number,
@@ -674,11 +675,24 @@ Invalid code at #{line}:#{col}
674
675
  end
675
676
  [ (Node.new :require, tokens[0][0][1..-2], []), 2 ]
676
677
  end
678
+
679
+ def self.parse_return(tokens)
680
+ unless self.expect tokens, :return
681
+ return nil
682
+ end
683
+ tokens = tokens[1..-1]
684
+ res = self.parse_expr tokens
685
+ unless res
686
+ return nil
687
+ end
688
+ [ (Node.new :return, res[0], []), res[1] ]
689
+ end
677
690
 
678
691
  def self.parse_expr(tokens)
679
692
  (self.parse_op tokens) ||
680
693
  (self.parse_call tokens) ||
681
694
  (self.parse_require tokens) ||
695
+ (self.parse_return tokens) ||
682
696
  (self.parse_new tokens) ||
683
697
  (self.parse_object tokens) ||
684
698
  (self.parse_fn tokens) ||
@@ -706,14 +720,15 @@ Invalid code at #{line}:#{col}
706
720
  unless code
707
721
  error "Cannot find file #{e[0].value}.sdx anywhere in path"
708
722
  State::state = :error
723
+ return nil
709
724
  end
710
- lexed = Lexer.lex code
711
- ast = self.parse lexed, path, (code.split "\n")
712
- parsed.concat ast
725
+ tokens = tokens[e[1]..-1]
726
+ lexed, _ = Lexer.lex code
727
+ tokens = [*lexed, *tokens]
713
728
  else
714
729
  parsed << e[0]
730
+ tokens = tokens[e[1]..-1]
715
731
  end
716
- tokens = tokens[e[1]..-1]
717
732
  else
718
733
  error %{
719
734
  Unexpected token #{tokens[0][1]} at #{tokens[0][2]}:#{tokens[0][3]}
@@ -1,4 +1,5 @@
1
1
  require "stringio"
2
+ require "bigdecimal"
2
3
 
3
4
  class DataType
4
5
  attr_reader :internal
@@ -51,10 +52,10 @@ class Bool < DataType
51
52
  @internal = false
52
53
  end
53
54
  @fields = {
54
- "__as_string" => (NativeFn.new 0, (Proc.new do
55
+ "__as_str" => (NativeFn.new 0, (Proc.new do
55
56
  as_string
56
57
  end)),
57
- "__as_code_string" => (NativeFn.new 0, (Proc.new do
58
+ "__as_code_str" => (NativeFn.new 0, (Proc.new do
58
59
  as_string
59
60
  end)),
60
61
  "__eq" => (NativeFnInternal.new (lambda do |other|
@@ -77,10 +78,10 @@ class Int < DataType
77
78
  @internal = val
78
79
  end
79
80
  @fields = {
80
- "__as_string" => (NativeFnInternal.new (Proc.new do
81
+ "__as_str" => (NativeFnInternal.new (Proc.new do
81
82
  as_string
82
83
  end)),
83
- "__as_code_string" => (NativeFnInternal.new (Proc.new do
84
+ "__as_code_str" => (NativeFnInternal.new (Proc.new do
84
85
  as_string
85
86
  end)),
86
87
  "__as_bool" => (NativeFnInternal.new (Proc.new do
@@ -134,42 +135,112 @@ class Int < DataType
134
135
  end
135
136
 
136
137
  def add(other)
138
+ case other
139
+ when Int
140
+ when Num
141
+ else
142
+ error "Cannot use Int + on #{other.class}"
143
+ return nil
144
+ end
137
145
  Int.new @internal + other.internal
138
146
  end
139
147
 
140
148
  def sub(other)
149
+ case other
150
+ when Int
151
+ when Num
152
+ else
153
+ error "Cannot use Int - on #{other.class}"
154
+ return nil
155
+ end
141
156
  Int.new @internal - other.internal
142
157
  end
143
158
 
144
159
  def mul(other)
160
+ case other
161
+ when Int
162
+ when Num
163
+ else
164
+ error "Cannot use Int * on #{other.class}"
165
+ return nil
166
+ end
145
167
  Int.new @internal * other.internal
146
168
  end
147
169
 
148
170
  def div(other)
149
- Int.new @internal / other.internal
171
+ case other
172
+ when Int
173
+ when Num
174
+ else
175
+ error "Cannot use Int / on #{other.class}"
176
+ return nil
177
+ end
178
+ Num.new (@internal / (other.internal / 1.0)).to_s
150
179
  end
151
180
 
152
181
  def mod(other)
182
+ case other
183
+ when Int
184
+ when Num
185
+ else
186
+ error "Cannot use Int % on #{other.class}"
187
+ return nil
188
+ end
153
189
  Int.new @internal % other.internal
154
190
  end
155
191
 
156
192
  def pow(other)
193
+ case other
194
+ when Int
195
+ when Num
196
+ else
197
+ error "Cannot use Int ^ on #{other.class}"
198
+ return nil
199
+ end
157
200
  Int.new @internal ** other.internal
158
201
  end
159
202
 
160
203
  def lt(other)
204
+ case other
205
+ when Int
206
+ when Num
207
+ else
208
+ error "Cannot use Int < on #{other.class}"
209
+ return nil
210
+ end
161
211
  Bool.new @internal < other.internal
162
212
  end
163
213
 
164
214
  def gt(other)
215
+ case other
216
+ when Int
217
+ when Num
218
+ else
219
+ error "Cannot use Int > on #{other.class}"
220
+ return nil
221
+ end
165
222
  Bool.new @internal > other.internal
166
223
  end
167
224
 
168
225
  def le(other)
226
+ case other
227
+ when Int
228
+ when Num
229
+ else
230
+ error "Cannot use Int <= on #{other.class}"
231
+ return nil
232
+ end
169
233
  Bool.new @internal <= other.internal
170
234
  end
171
235
 
172
236
  def ge(other)
237
+ case other
238
+ when Int
239
+ when Num
240
+ else
241
+ error "Cannot use Int >= on #{other.class}"
242
+ return nil
243
+ end
173
244
  Bool.new @internal >= other.internal
174
245
  end
175
246
  end
@@ -180,10 +251,10 @@ class Str < DataType
180
251
  @internal = val
181
252
  end
182
253
  @fields = {
183
- "__as_string" => (NativeFnInternal.new (Proc.new do
254
+ "__as_str" => (NativeFnInternal.new (Proc.new do
184
255
  as_string
185
256
  end)),
186
- "__as_code_string" => (NativeFnInternal.new (Proc.new do
257
+ "__as_code_str" => (NativeFnInternal.new (Proc.new do
187
258
  as_code_string
188
259
  end)),
189
260
  "__add" => (NativeFnInternal.new (Proc.new do |other|
@@ -210,10 +281,22 @@ class Str < DataType
210
281
  end
211
282
 
212
283
  def add(other)
284
+ case other
285
+ when Str
286
+ else
287
+ error "Cannot use Str + on #{other.class}"
288
+ return nil
289
+ end
213
290
  Str.new @internal + other.internal
214
291
  end
215
292
 
216
293
  def mul(other)
294
+ case other
295
+ when Int
296
+ else
297
+ error "Cannot use Int * on #{other.class}"
298
+ return nil
299
+ end
217
300
  Str.new @internal * other.internal
218
301
  end
219
302
  end
@@ -221,13 +304,13 @@ end
221
304
  class Num < DataType
222
305
  def initialize(val=nil)
223
306
  if val != nil
224
- @internal = val
307
+ @internal = BigDecimal val
225
308
  end
226
309
  @fields = {
227
- "__as_string" => (NativeFnInternal.new (Proc.new do
310
+ "__as_str" => (NativeFnInternal.new (Proc.new do
228
311
  as_string
229
312
  end)),
230
- "__as_code_string" => (NativeFnInternal.new (Proc.new do
313
+ "__as_code_str" => (NativeFnInternal.new (Proc.new do
231
314
  as_string
232
315
  end)),
233
316
  "__as_bool" => (NativeFnInternal.new (Proc.new do
@@ -273,7 +356,7 @@ class Num < DataType
273
356
  end
274
357
 
275
358
  def as_string
276
- Str.new @internal.to_s
359
+ Str.new (@internal.to_s "F")
277
360
  end
278
361
 
279
362
  def as_bool
@@ -281,42 +364,112 @@ class Num < DataType
281
364
  end
282
365
 
283
366
  def add(other)
367
+ case other
368
+ when Num
369
+ when Int
370
+ else
371
+ error "Cannot use Num + on #{other.class}"
372
+ return nil
373
+ end
284
374
  Num.new @internal + other.internal
285
375
  end
286
376
 
287
377
  def sub(other)
378
+ case other
379
+ when Num
380
+ when Int
381
+ else
382
+ error "Cannot use Num - on #{other.class}"
383
+ return nil
384
+ end
288
385
  Num.new @internal - other.internal
289
386
  end
290
387
 
291
388
  def mul(other)
389
+ case other
390
+ when Num
391
+ when Int
392
+ else
393
+ error "Cannot use Num * on #{other.class}"
394
+ return nil
395
+ end
292
396
  Num.new @internal * other.internal
293
397
  end
294
398
 
295
399
  def div(other)
400
+ case other
401
+ when Num
402
+ when Int
403
+ else
404
+ error "Cannot use Num / on #{other.class}"
405
+ return nil
406
+ end
296
407
  Num.new @internal / other.internal
297
408
  end
298
409
 
299
410
  def mod(other)
411
+ case other
412
+ when Num
413
+ when Int
414
+ else
415
+ error "Cannot use Num % on #{other.class}"
416
+ return nil
417
+ end
300
418
  Num.new @internal % other.internal
301
419
  end
302
420
 
303
421
  def pow(other)
422
+ case other
423
+ when Num
424
+ when Int
425
+ else
426
+ error "Cannot use Num ^ on #{other.class}"
427
+ return nil
428
+ end
304
429
  Num.new @internal ** other.internal
305
430
  end
306
431
 
307
432
  def lt(other)
433
+ case other
434
+ when Num
435
+ when Int
436
+ else
437
+ error "Cannot use Num < on #{other.class}"
438
+ return nil
439
+ end
308
440
  Bool.new @internal < other.internal
309
441
  end
310
442
 
311
443
  def gt(other)
444
+ case other
445
+ when Num
446
+ when Int
447
+ else
448
+ error "Cannot use Num > on #{other.class}"
449
+ return nil
450
+ end
312
451
  Bool.new @internal > other.internal
313
452
  end
314
453
 
315
454
  def le(other)
455
+ case other
456
+ when Num
457
+ when Int
458
+ else
459
+ error "Cannot use Num <= on #{other.class}"
460
+ return nil
461
+ end
316
462
  Bool.new @internal <= other.internal
317
463
  end
318
464
 
319
465
  def ge(other)
466
+ case other
467
+ when Num
468
+ when Int
469
+ else
470
+ error "Cannot use Num >= on #{other.class}"
471
+ return nil
472
+ end
320
473
  Bool.new @internal >= other.internal
321
474
  end
322
475
  end
@@ -328,6 +481,9 @@ class Nil < DataType
328
481
  "__as_bool" => (NativeFnInternal.new (Proc.new do
329
482
  Bool.new false
330
483
  end)),
484
+ "__as_str" => (NativeFnInternal.new (Proc.new do
485
+ Str.new "nil"
486
+ end)),
331
487
  "__eq" => (NativeFnInternal.new (lambda do |other|
332
488
  Bool.new @internal == other[0].internal
333
489
  end)),
@@ -344,10 +500,10 @@ class List < DataType
344
500
  @scope = scope
345
501
  @pos = 0
346
502
  @fields = {
347
- "__as_string" => (NativeFnInternal.new (Proc.new do
503
+ "__as_str" => (NativeFnInternal.new (Proc.new do
348
504
  as_string
349
505
  end)),
350
- "__as_code_string" => (NativeFnInternal.new (Proc.new do
506
+ "__as_code_str" => (NativeFnInternal.new (Proc.new do
351
507
  as_code_string
352
508
  end)),
353
509
  "__reset" => (NativeFnInternal.new (Proc.new do
@@ -414,6 +570,12 @@ class List < DataType
414
570
  end
415
571
 
416
572
  def mul(other)
573
+ case other
574
+ when Int
575
+ else
576
+ error "Cannot use List * on #{other.class}"
577
+ return nil
578
+ end
417
579
  return List.new @internal * other.internal
418
580
  end
419
581
  end
@@ -3,17 +3,34 @@ require 'sdx/vm/datatypes'
3
3
  require 'sdx/vm/scope'
4
4
 
5
5
  def codify(val)
6
- if val.value.fields["__as_code_string"]
7
- if val.value.fields["__as_code_string"].respond_to? :call
8
- (val.value.fields["__as_code_string"].call).internal
6
+ if val.value.fields["__as_code_str"]
7
+ if val.value.fields["__as_code_str"].respond_to? :call
8
+ (val.value.fields["__as_code_str"].call).internal
9
9
  else
10
- (val.value.fields["__as_code_string"].fields["__call"].call [], val.scope).internal
10
+ (val.value.fields["__as_code_str"].fields["__call"].call [], val.scope).internal
11
11
  end
12
12
  else
13
13
  val.value.pretty_inspect
14
14
  end
15
15
  end
16
16
 
17
+ def stringify(val)
18
+ if val.value.fields["__as_str"]
19
+ if val.value.fields["__as_str"].respond_to? :fields
20
+ (val.value.fields["__as_str"].fields["__call"].call [], val.scope).internal
21
+ else
22
+ (val.value.fields["__as_str"]).call.internal
23
+ end
24
+ else
25
+ val.value.to_s
26
+ end
27
+ end
28
+
29
+ def error(msg)
30
+ puts "\x1b[0;31mError in VM: #{msg}\x1b[0;0m"
31
+ State::state = :error
32
+ end
33
+
17
34
  class VM
18
35
  attr_accessor :bc_io
19
36
 
@@ -28,14 +45,6 @@ class VM
28
45
  return true
29
46
  end
30
47
  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
37
- end
38
- end
39
48
 
40
49
  def call(val, *args)
41
50
  if val.respond_to? :value and val.value.respond_to? :fields
@@ -109,6 +118,62 @@ class VM
109
118
  args = (codify args)[1..-2]
110
119
  from_rb eval "#{name.value.internal}(#{args})"
111
120
  end)), :fn, @global)
121
+ @global.add_fn "__is_int", (Variable.new (NativeFn.new 1, (Proc.new do |n|
122
+ case n.value
123
+ when Int
124
+ to_var (Bool.new true)
125
+ else
126
+ to_var (Bool.new false)
127
+ end
128
+ end)), :fn, @global)
129
+ @global.add_fn "__is_num", (Variable.new (NativeFn.new 1, (Proc.new do |n|
130
+ case n.value
131
+ when Num
132
+ to_var (Bool.new true)
133
+ else
134
+ to_var (Bool.new false)
135
+ end
136
+ end)), :fn, @global)
137
+ @global.add_fn "__is_str", (Variable.new (NativeFn.new 1, (Proc.new do |n|
138
+ case n.value
139
+ when Str
140
+ to_var (Bool.new true)
141
+ else
142
+ to_var (Bool.new false)
143
+ end
144
+ end)), :fn, @global)
145
+ @global.add_fn "__is_list", (Variable.new (NativeFn.new 1, (Proc.new do |n|
146
+ case n.value
147
+ when List
148
+ to_var (Bool.new true)
149
+ else
150
+ to_var (Bool.new false)
151
+ end
152
+ end)), :fn, @global)
153
+ @global.add_fn "__is_bool", (Variable.new (NativeFn.new 1, (Proc.new do |n|
154
+ case n.value
155
+ when Bool
156
+ to_var (Bool.new true)
157
+ else
158
+ to_var (Bool.new false)
159
+ end
160
+ end)), :fn, @global)
161
+ @global.add_fn "__is_list", (Variable.new (NativeFn.new 1, (Proc.new do |n|
162
+ case n.value
163
+ when List
164
+ to_var (Bool.new true)
165
+ else
166
+ to_var (Bool.new false)
167
+ end
168
+ end)), :fn, @global)
169
+ @global.add_fn "__is_nil", (Variable.new (NativeFn.new 1, (Proc.new do |n|
170
+ case n.value
171
+ when Nil
172
+ to_var (Bool.new true)
173
+ else
174
+ to_var (Bool.new false)
175
+ end
176
+ end)), :fn, @global)
112
177
  @stack = []
113
178
  @byte_pos = 0
114
179
  end
@@ -271,7 +336,7 @@ class VM
271
336
  push_to_stack Variable.new (Int.new val.to_i), :int, @global
272
337
  when :num
273
338
  val = get_string
274
- push_to_stack Variable.new (Num.new val.to_f), :num, @global
339
+ push_to_stack Variable.new (Num.new val), :num, @global
275
340
  when :str
276
341
  val = get_string
277
342
  push_to_stack Variable.new (Str.new val), :str, @global
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.3.1
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - sugarfi