sardonyx 0.1.5 → 0.1.82

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: 134db5d432fc5be113fff903e155f16cfd8d756b3ee89846cb542fe282d831d0
4
- data.tar.gz: a89c2707cb7a86c09f6ac5afe265bff9913d55c87346f7ffc75c274c0436b042
3
+ metadata.gz: a384634a4bfdce5ea555978895b81d96a5d4d916375bc192a739493920d6d8bf
4
+ data.tar.gz: 38fc8ac4918a9cbbe7f3e4a06e7b053ee99132b4a49c2b434fe82384b78d2f9d
5
5
  SHA512:
6
- metadata.gz: ae6c5e8daab94393ae813c3980950ad3cf22e075820f6cdbef46cab336283098bf2aa9d80008185cba53493ea78297fec3b5c0171124082fa50f38243f83b9c7
7
- data.tar.gz: 2279b27b460bc419e3992d78548e46503eec909695f813e0a6072dcc777ecfc35da19e8ab614de60f0e0356a22711ad391753bafa732b848745fe04cd35db64b
6
+ metadata.gz: 6cc678adbf5ae8f5059b05934d8934e0c33e43cda2842bdf54e606238d221fba49c0133984cb7bcbe0de0a79a3cb692758c60797d3cad53732b6bc2ade48324d
7
+ data.tar.gz: 417c8e9ee3cf5cf62b3f95962712f60b72f156bd4cdbe9a685101c6df9fed101c7533be14037057481e273e5b7fb79efb57f87ea9e4f8fbbfc330b9e248dbcdf
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 "> "
@@ -25,8 +25,11 @@ else
25
25
  bc = Compiler::Compiler.compile ast
26
26
  vm.bc_io = StringIO.new bc
27
27
  vm.byte_pos = 0
28
- vm.interpret
28
+ vm.interpret false
29
29
  val = vm.stack[-1]
30
- puts stringify val
30
+ vm.clear
31
+ if val
32
+ puts stringify val
33
+ end
31
34
  end
32
35
  end
@@ -4,6 +4,7 @@ module Compiler
4
4
  bc = ""
5
5
  ast.each do |line|
6
6
  bc += self.encode_node line
7
+ bc += "\x33"
7
8
  end
8
9
  bc + "\x16"
9
10
  end
@@ -34,6 +35,14 @@ module Compiler
34
35
  bc += "\x21\x15#{node.value}\x18"
35
36
  when :string
36
37
  bc += "\x21\x14#{node.value}\x18"
38
+ when :block
39
+ b = ""
40
+ node.children.each do |child|
41
+ b += (self.encode_node child) + "\x33"
42
+ end
43
+ b = b[0..-2]
44
+ b += "\x16"
45
+ bc += "\x21\x32#{b.size}\x18#{b}"
37
46
  when :call
38
47
  node.children.each do |item|
39
48
  bc += self.encode_node item
@@ -171,7 +171,7 @@ module Parser
171
171
  end
172
172
 
173
173
  def self.parse_literal(tokens)
174
- self.parse_float(tokens) || (self.parse_block tokens) || (self.parse_name tokens) || (self.parse_number tokens) || (self.parse_list tokens) || (self.parse_string tokens) || (self.parse_nil 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)
175
175
  end
176
176
 
177
177
  def self.parse_call(tokens)
@@ -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)),
@@ -83,22 +83,22 @@ class Int < DataType
83
83
  as_bool
84
84
  end)),
85
85
  "__add" => (NativeFnInternal.new (Proc.new do |other|
86
- add other
86
+ add other[0]
87
87
  end)),
88
88
  "__sub" => (NativeFnInternal.new (Proc.new do |other|
89
- sub other
89
+ sub other[0]
90
90
  end)),
91
91
  "__mul" => (NativeFnInternal.new (Proc.new do |other|
92
- mul other
92
+ mul other[0]
93
93
  end)),
94
94
  "__div" => (NativeFnInternal.new (Proc.new do |other|
95
- div other
95
+ div other[0]
96
96
  end)),
97
97
  "__mod" => (NativeFnInternal.new (Proc.new do |other|
98
- mod other
98
+ mod other[0]
99
99
  end)),
100
100
  "__pow" => (NativeFnInternal.new (Proc.new do |other|
101
- pow other
101
+ pow other[0]
102
102
  end))
103
103
  }
104
104
  end
@@ -149,10 +149,10 @@ class Str < DataType
149
149
  as_code_string
150
150
  end)),
151
151
  "__add" => (NativeFnInternal.new (Proc.new do |other|
152
- add other
152
+ add other[0]
153
153
  end)),
154
154
  "__mul" => (NativeFnInternal.new (Proc.new do |other|
155
- mul other
155
+ mul other[0]
156
156
  end))
157
157
  }
158
158
  end
@@ -162,7 +162,7 @@ class Str < DataType
162
162
  end
163
163
 
164
164
  def as_code_string
165
- (Str.new "\"#{@internal}\"")
165
+ (Str.new @internal.dump)
166
166
  end
167
167
 
168
168
  def add(other)
@@ -190,22 +190,22 @@ class Num < DataType
190
190
  as_bool
191
191
  end)),
192
192
  "__add" => (NativeFnInternal.new (Proc.new do |other|
193
- add other
193
+ add other[0]
194
194
  end)),
195
195
  "__sub" => (NativeFnInternal.new (Proc.new do |other|
196
- sub other
196
+ sub other[0]
197
197
  end)),
198
198
  "__mul" => (NativeFnInternal.new (Proc.new do |other|
199
- mul other
199
+ mul other[0]
200
200
  end)),
201
201
  "__div" => (NativeFnInternal.new (Proc.new do |other|
202
- div other
202
+ div other[0]
203
203
  end)),
204
204
  "__mod" => (NativeFnInternal.new (Proc.new do |other|
205
- mod other
205
+ mod other[0]
206
206
  end)),
207
207
  "__pow" => (NativeFnInternal.new (Proc.new do |other|
208
- pow other
208
+ pow other[0]
209
209
  end))
210
210
  }
211
211
  end
@@ -272,30 +272,32 @@ class List < DataType
272
272
  iter
273
273
  end)),
274
274
  "__add" => (NativeFnInternal.new (Proc.new do |other|
275
- add other
275
+ add other[0]
276
276
  end)),
277
277
  "__mul" => (NativeFnInternal.new (Proc.new do |other|
278
- mul other
278
+ mul other[0]
279
+ end)),
280
+ "__arity" => (Int.new 1),
281
+ "__call" => (NativeFnInternal.new (Proc.new do |args, scope|
282
+ @internal[args[0].value.internal]
279
283
  end))
280
284
  }
281
285
  end
282
286
 
283
287
  def as_string
284
- s = "["
285
288
  @internal.each do |item|
286
289
  s += (stringify item) + ", "
287
290
  end
288
- s = s[0..-3]
291
+ s = "[" + s[0..-3]
289
292
  s += "]"
290
293
  Str.new s
291
294
  end
292
295
 
293
296
  def as_code_string
294
- s = "["
295
297
  @internal.each do |item|
296
298
  s += (codify item) + ", "
297
299
  end
298
- s = s[0..-3]
300
+ s = "[" + s[0..-3]
299
301
  s += "]"
300
302
  Str.new s
301
303
  end
@@ -341,6 +343,8 @@ def get_type(x)
341
343
  :num
342
344
  when Obj
343
345
  :object
346
+ when Block
347
+ :block
344
348
  end
345
349
  end
346
350
 
@@ -352,7 +356,7 @@ class Function < DataType
352
356
  @internal = body
353
357
 
354
358
  @fields = {
355
- "__call" => (NativeFnInternal.new (Proc.new do |args, scope|
359
+ "__call" => (NativeFnInternal.new (lambda do |args, scope|
356
360
  call args, scope
357
361
  end)),
358
362
  "__arity" => (Int.new args.size)
@@ -373,6 +377,30 @@ class Function < DataType
373
377
  end
374
378
  end
375
379
 
380
+ class Block < DataType
381
+ def initialize(body)
382
+ @internal = body
383
+
384
+ @fields = {
385
+ "__call" => (NativeFnInternal.new (Proc.new do |args, scope|
386
+ call args, scope
387
+ end)),
388
+ "__arity" => (Int.new 1)
389
+ }
390
+ end
391
+
392
+ def call(passed, scope)
393
+ passed.reverse!
394
+ vm = VM.new StringIO.new @internal
395
+ scope.variables.each do |k|
396
+ vm.global.add_var k[0], (scope.get_var k[0])
397
+ end
398
+ vm.global.add_var "_", passed[0]
399
+ vm.interpret
400
+ vm.stack[-1]
401
+ end
402
+ end
403
+
376
404
  class Obj < DataType
377
405
  attr_reader :args
378
406
 
@@ -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
@@ -98,6 +113,10 @@ class VM
98
113
  def byte_pos=(n)
99
114
  @byte_pos = n
100
115
  end
116
+
117
+ def clear
118
+ @stack = []
119
+ end
101
120
 
102
121
  def load_bytes(x_bytes, to_symbol=true) # loads in next x bytes from file, returns in array
103
122
  insts = { # just a simple hash to make interpreter code more readable
@@ -129,7 +148,9 @@ class VM
129
148
  0x2d => :reset,
130
149
  0x2e => :iter,
131
150
  0x30 => :object,
132
- 0x31 => :new
151
+ 0x31 => :new,
152
+ 0x32 => :block,
153
+ 0x33 => :end
133
154
  }
134
155
  bytes = []
135
156
  begin
@@ -186,7 +207,7 @@ class VM
186
207
  exit 1
187
208
  end
188
209
 
189
- def interpret # builds stack from bytecode
210
+ def interpret(do_end=true) # builds stack from bytecode
190
211
  loop do
191
212
  loaded_bytes = load_bytes(1) # loads in first byte for initial instruction
192
213
  break if loaded_bytes[0] == :end_prg # end of program reached
@@ -244,6 +265,11 @@ class VM
244
265
  end
245
266
  vals.reverse!
246
267
  push_to_stack Variable.new (List.new vals), :list, @global
268
+ when :block
269
+ size = get_string.to_i
270
+ body =
271
+ ((load_bytes size, false).map { |e| e.chr }).join ""
272
+ push_to_stack Variable.new (Block.new body), :block, @global
247
273
  when :nil
248
274
  push_to_stack Variable.new (Nil.new), :nil, @global
249
275
  end
@@ -330,6 +356,10 @@ class VM
330
356
  res = call val.value.fields["__iter"]
331
357
  push_to_stack res
332
358
  end
359
+ when :end
360
+ if do_end
361
+ @stack = []
362
+ end
333
363
  when :call
334
364
  val = pop_from_stack
335
365
  if callable val
@@ -341,16 +371,13 @@ class VM
341
371
  end
342
372
  args << this
343
373
  end
344
- f = Proc.new do |args, scope|
345
- call val, args, scope
346
- end
347
374
  scope = nil
348
375
  begin
349
376
  scope = val.scope
350
377
  rescue
351
378
  scope = @global
352
379
  end
353
- ret = f.call args, scope
380
+ ret = call val, *args
354
381
  if ret
355
382
  push_to_stack ret
356
383
  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.5
4
+ version: 0.1.82
5
5
  platform: ruby
6
6
  authors:
7
7
  - sugarfi