sardonyx 0.1.5 → 0.1.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 +4 -4
- data/bin/sdx +5 -2
- data/lib/sdx/compiler/compiler.rb +9 -0
- data/lib/sdx/compiler/parser.rb +1 -1
- data/lib/sdx/vm/datatypes.rb +30 -0
- data/lib/sdx/vm/vm.rb +17 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a1e2303504b396f3aea3b179bb7fd511de36490784a1763599712fb53c2a9448
|
|
4
|
+
data.tar.gz: 4092253d393f90075180acc3cad50bcd1f5d2a2377e3a0d2d853b93cb9902d72
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3f89fd078c871701eee5ddc6f8a8306666da58d6ffc4e6050e6bd2a80dbb2c31903dfb3c86c7155e5f1477104305016b64b8bcf336b7c0d9b63b881582bf3813
|
|
7
|
+
data.tar.gz: b9ebed85a007ca9ca7872b9db44e75173fb6db412d8a88c00f968a1685c47084b71f7baf5857a786b836ab905c84b74970b67f76eda75c4d633aa788ecfe4e88
|
data/bin/sdx
CHANGED
|
@@ -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
|
data/lib/sdx/compiler/parser.rb
CHANGED
|
@@ -171,7 +171,7 @@ module Parser
|
|
|
171
171
|
end
|
|
172
172
|
|
|
173
173
|
def self.parse_literal(tokens)
|
|
174
|
-
self.
|
|
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)
|
data/lib/sdx/vm/datatypes.rb
CHANGED
|
@@ -276,6 +276,10 @@ class List < DataType
|
|
|
276
276
|
end)),
|
|
277
277
|
"__mul" => (NativeFnInternal.new (Proc.new do |other|
|
|
278
278
|
mul other
|
|
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
|
|
@@ -341,6 +345,8 @@ def get_type(x)
|
|
|
341
345
|
:num
|
|
342
346
|
when Obj
|
|
343
347
|
:object
|
|
348
|
+
when Block
|
|
349
|
+
:block
|
|
344
350
|
end
|
|
345
351
|
end
|
|
346
352
|
|
|
@@ -373,6 +379,30 @@ class Function < DataType
|
|
|
373
379
|
end
|
|
374
380
|
end
|
|
375
381
|
|
|
382
|
+
class Block < DataType
|
|
383
|
+
def initialize(body)
|
|
384
|
+
@internal = body
|
|
385
|
+
|
|
386
|
+
@fields = {
|
|
387
|
+
"__call" => (NativeFnInternal.new (Proc.new do |args, scope|
|
|
388
|
+
call args, scope
|
|
389
|
+
end)),
|
|
390
|
+
"__arity" => (Int.new 1)
|
|
391
|
+
}
|
|
392
|
+
end
|
|
393
|
+
|
|
394
|
+
def call(passed, scope)
|
|
395
|
+
passed.reverse!
|
|
396
|
+
vm = VM.new StringIO.new @internal
|
|
397
|
+
scope.variables.each do |k|
|
|
398
|
+
vm.global.add_var k[0], (scope.get_var k[0])
|
|
399
|
+
end
|
|
400
|
+
vm.global.add_var "_", passed[0]
|
|
401
|
+
vm.interpret
|
|
402
|
+
vm.stack[-1]
|
|
403
|
+
end
|
|
404
|
+
end
|
|
405
|
+
|
|
376
406
|
class Obj < DataType
|
|
377
407
|
attr_reader :args
|
|
378
408
|
|
data/lib/sdx/vm/vm.rb
CHANGED
|
@@ -98,6 +98,10 @@ class VM
|
|
|
98
98
|
def byte_pos=(n)
|
|
99
99
|
@byte_pos = n
|
|
100
100
|
end
|
|
101
|
+
|
|
102
|
+
def clear
|
|
103
|
+
@stack = []
|
|
104
|
+
end
|
|
101
105
|
|
|
102
106
|
def load_bytes(x_bytes, to_symbol=true) # loads in next x bytes from file, returns in array
|
|
103
107
|
insts = { # just a simple hash to make interpreter code more readable
|
|
@@ -129,7 +133,9 @@ class VM
|
|
|
129
133
|
0x2d => :reset,
|
|
130
134
|
0x2e => :iter,
|
|
131
135
|
0x30 => :object,
|
|
132
|
-
0x31 => :new
|
|
136
|
+
0x31 => :new,
|
|
137
|
+
0x32 => :block,
|
|
138
|
+
0x33 => :end
|
|
133
139
|
}
|
|
134
140
|
bytes = []
|
|
135
141
|
begin
|
|
@@ -186,7 +192,7 @@ class VM
|
|
|
186
192
|
exit 1
|
|
187
193
|
end
|
|
188
194
|
|
|
189
|
-
def interpret # builds stack from bytecode
|
|
195
|
+
def interpret(do_end=true) # builds stack from bytecode
|
|
190
196
|
loop do
|
|
191
197
|
loaded_bytes = load_bytes(1) # loads in first byte for initial instruction
|
|
192
198
|
break if loaded_bytes[0] == :end_prg # end of program reached
|
|
@@ -244,6 +250,11 @@ class VM
|
|
|
244
250
|
end
|
|
245
251
|
vals.reverse!
|
|
246
252
|
push_to_stack Variable.new (List.new vals), :list, @global
|
|
253
|
+
when :block
|
|
254
|
+
size = get_string.to_i
|
|
255
|
+
body =
|
|
256
|
+
((load_bytes size, false).map { |e| e.chr }).join ""
|
|
257
|
+
push_to_stack Variable.new (Block.new body), :block, @global
|
|
247
258
|
when :nil
|
|
248
259
|
push_to_stack Variable.new (Nil.new), :nil, @global
|
|
249
260
|
end
|
|
@@ -330,6 +341,10 @@ class VM
|
|
|
330
341
|
res = call val.value.fields["__iter"]
|
|
331
342
|
push_to_stack res
|
|
332
343
|
end
|
|
344
|
+
when :end
|
|
345
|
+
if do_end
|
|
346
|
+
@stack = []
|
|
347
|
+
end
|
|
333
348
|
when :call
|
|
334
349
|
val = pop_from_stack
|
|
335
350
|
if callable val
|