Top Level Namespace
Defined Under Namespace
Classes: QAtom, QNum, QQuote, QStr, QSym, QVM, QVal, QuarkParse, QuarkTransform
Constant Summary
collapse
- QuarkError =
Class.new(RuntimeError)
Instance Method Summary
collapse
-
#+ ⇒ Object
-
#apply_core_func(name, vm, eval_func) ⇒ Object
-
#apply_runtime_func(name, vm) ⇒ Object
-
#deep_clone(x) ⇒ Object
-
#def_cf(name, type_sig, &code) ⇒ Object
-
#pattern_match(stack_args, args) ⇒ Object
-
#qparse(str) ⇒ Object
-
#qreduce(vm) ⇒ Object
-
#qrepl(vm) ⇒ Object
-
#qrun(str, stack = [], bindings = {}) ⇒ Object
-
#type_check(stack, type_sig) ⇒ Object
matches a type signature against a data stack.
-
#type_match(a, b) ⇒ Object
compares two qtype to see if they are equivelent type comparison is not symmetric ‘a` is the signature type, so (:Any, :Empty) will match, but (:Empty, :Any) won’t.
-
#type_to_qitem(t) ⇒ Object
converts a qtype to a qitem representation (used in the ‘type` function).
Instance Method Details
#+ ⇒ Object
48
49
50
|
# File 'lib/qeval.rb', line 48
def_cf('+', [:Num, :Num]) do |vm|
vm.stack.push QNum.new(vm.stack.pop.val + vm.stack.pop.val)
end
|
#apply_core_func(name, vm, eval_func) ⇒ Object
15
16
17
18
19
|
# File 'lib/qeval.rb', line 15
def apply_core_func(name, vm, eval_func)
eq, expected, got = type_check(vm.stack, $core_func_type[name])
raise(QuarkError, "Type error with function #{name}\n expected a stack of #{expected}\n but got #{got}") if not eq
$core_func[name].call(vm, eval_func)
end
|
#apply_runtime_func(name, vm) ⇒ Object
21
22
23
24
|
# File 'lib/qeval.rb', line 21
def apply_runtime_func(name, vm)
quote = vm.bindings[name]
vm.program.unshift(*[quote, QAtom.new('call')])
end
|
#deep_clone(x) ⇒ Object
41
42
43
|
# File 'lib/qeval.rb', line 41
def deep_clone x
Marshal.load(Marshal.dump(x))
end
|
#def_cf(name, type_sig, &code) ⇒ Object
10
11
12
13
|
# File 'lib/qeval.rb', line 10
def def_cf(name, type_sig, &code)
$core_func_type[name] = type_sig
$core_func[name] = code
end
|
#pattern_match(stack_args, args) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/qeval.rb', line 26
def pattern_match(stack_args, args)
return false if stack_args.length < args.length
return {} if args.length == 0
bindings = {}
args.zip(stack_args).each do |a, s|
if a.is_a? QAtom
return false if (bindings.has_key? a.val) && (bindings[a.val] != s)
bindings[a.val] = s
else
return false if a != s
end
end
return bindings
end
|
#qparse(str) ⇒ Object
37
38
39
40
41
42
|
# File 'lib/qparse.rb', line 37
def qparse str
parsed = QuarkParse.new.parse(str)
QuarkTransform.new.apply parsed
rescue Parslet::ParseFailed => e
e.cause.ascii_tree
end
|
#qreduce(vm) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/qrun.rb', line 13
def qreduce vm
until vm.program.empty?
item = vm.program.shift
if item.is_a? QAtom
if $core_func.has_key? item.val
apply_core_func(item.val, vm, ->(*x){qrun(*x)})
elsif vm.bindings.has_key? item.val
quote = vm.bindings[item.val]
vm.program.unshift(*[quote.dup, QAtom.new('call')])
else
raise QuarkError, "No such function: #{item}"
end
else
vm.stack.push item
end
end
return vm
end
|
#qrepl(vm) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/qrun.rb', line 32
def qrepl(vm)
loop do
print ':> '
begin
input = $stdin.gets.chomp
case input.strip
when '*q'
exit!
when '*f'
vm.bindings.sort.each { |k, v| puts "#{k}\n #{v.to_s}\n\n"}
when /\*f\s+(.+)/
if vm.bindings.has_key? $1
puts vm.bindings[$1]
else puts "No such function: #{$1}" end
else
vm = qrun(input, vm.stack.dup, vm.bindings.dup)
end
puts vm.stack.map { |x| x.is_a?(QQuote) ? x.to_s(20) : x.to_s }.join(' ')
rescue Exception => e
puts e
end
end
end
|
#qrun(str, stack = [], bindings = {}) ⇒ Object
9
10
11
|
# File 'lib/qrun.rb', line 9
def qrun(str, stack=[], bindings={})
qreduce QVM.new(stack, qparse(str), bindings)
end
|
#type_check(stack, type_sig) ⇒ Object
matches a type signature against a data stack
126
127
128
129
130
131
132
133
134
|
# File 'lib/qtypes.rb', line 126
def type_check(stack, type_sig)
return false if stack.length < type_sig.length
return true if type_sig.length == 0
stack_type = stack.last(type_sig.length).map(&:qtype)
types_eq = type_sig.zip(stack_type)
.map { |sig, type| type_match(sig, type) }
.inject(true) { |a, b| a && b}
return types_eq, type_sig, stack_type
end
|
#type_match(a, b) ⇒ Object
compares two qtype to see if they are equivelent type comparison is not symmetric ‘a` is the signature type, so (:Any, :Empty) will match, but (:Empty, :Any) won’t
120
121
122
123
|
# File 'lib/qtypes.rb', line 120
def type_match(a, b)
return true if a == :Any
a == b
end
|
#type_to_qitem(t) ⇒ Object
converts a qtype to a qitem representation (used in the ‘type` function)
137
138
139
|
# File 'lib/qtypes.rb', line 137
def type_to_qitem t
return QSym.new(t.to_s)
end
|