sardonyx 0.3.5 → 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: b6cb7504c3a8d3c20d609b052304da6f29006b61d4606b738fe0a33fc92753c8
4
- data.tar.gz: 573d3b61d0031f2c1016ab4ba6d8b856f6ba8770e19b93610080735fb01efabf
3
+ metadata.gz: d0c5b5dabcbc157b1423ad023f21c3a892937bf6046050a83da1470e7ce724a1
4
+ data.tar.gz: f386cc4507d301b80f807a231ef2bd8af4140114bb7d3760bdf2847fdd73b843
5
5
  SHA512:
6
- metadata.gz: f8d4f48ba42b07b7bc29a367cb67492c7ca7f0c481f7556a2cecb28d2085be081330ebc3040469cadee489db420f015c2bf31969a2818f3b285ec24c0d101b4f
7
- data.tar.gz: d6569e3d3607dfc50628b72952195e44289ac642412804330b54f82d8b6e3ad3afb863888a8f80c57e736c73e02f9bbee25f033a76dda87845d869753c4dbf9d
6
+ metadata.gz: 714886d2d100b7ac440f7dd56cb8e9bf0f7ae880d67778775fe900b8b179f99f9d47f5404cb313515923a32f2fbb2d0e71527da296357b3ca3e5ddbb12963120
7
+ data.tar.gz: 5fc4e31a81de969b3714ab40b1c12e4c792d27efbd96c36292d1cea570cbae0de0865b7b8e412c560113cdd910aa5d7c80df834cf12df4fc7a3b4a9b402704d8
data/bin/sdx CHANGED
@@ -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.4"
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,7 +50,7 @@ else
50
50
  vm.interpret false
51
51
  val = vm.stack[-1]
52
52
  vm.clear
53
- if val
53
+ if val and State::state == :ok
54
54
  puts stringify val
55
55
  end
56
56
  end
@@ -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,11 +3,11 @@ 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
@@ -15,17 +15,22 @@ def codify(val)
15
15
  end
16
16
 
17
17
  def stringify(val)
18
- if val.value.fields["__as_string"]
19
- if val.value.fields["__as_string"].respond_to? :fields
20
- (val.value.fields["__as_string"].fields["__call"].call [], val.scope).internal
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
21
  else
22
- (val.value.fields["__as_string"]).call.internal
22
+ (val.value.fields["__as_str"]).call.internal
23
23
  end
24
24
  else
25
25
  val.value.to_s
26
26
  end
27
27
  end
28
28
 
29
+ def error(msg)
30
+ puts "\x1b[0;31mError in VM: #{msg}\x1b[0;0m"
31
+ State::state = :error
32
+ end
33
+
29
34
  class VM
30
35
  attr_accessor :bc_io
31
36
 
@@ -113,6 +118,62 @@ class VM
113
118
  args = (codify args)[1..-2]
114
119
  from_rb eval "#{name.value.internal}(#{args})"
115
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)
116
177
  @stack = []
117
178
  @byte_pos = 0
118
179
  end
@@ -275,7 +336,7 @@ class VM
275
336
  push_to_stack Variable.new (Int.new val.to_i), :int, @global
276
337
  when :num
277
338
  val = get_string
278
- push_to_stack Variable.new (Num.new val.to_f), :num, @global
339
+ push_to_stack Variable.new (Num.new val), :num, @global
279
340
  when :str
280
341
  val = get_string
281
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.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - sugarfi