Class: Nydp::VM
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Helper
#cons, #list, #literal?, #pair?, #sym, #sym?
Constructor Details
#initialize ⇒ VM
Returns a new instance of VM.
6
7
8
9
10
11
|
# File 'lib/nydp/vm.rb', line 6
def initialize
@instructions = []
@args = []
@contexts = []
@locals = { }
end
|
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
4
5
6
|
# File 'lib/nydp/vm.rb', line 4
def args
@args
end
|
#contexts ⇒ Object
Returns the value of attribute contexts.
4
5
6
|
# File 'lib/nydp/vm.rb', line 4
def contexts
@contexts
end
|
#current_context ⇒ Object
Returns the value of attribute current_context.
4
5
6
|
# File 'lib/nydp/vm.rb', line 4
def current_context
@current_context
end
|
#instructions ⇒ Object
Returns the value of attribute instructions.
4
5
6
|
# File 'lib/nydp/vm.rb', line 4
def instructions
@instructions
end
|
#locals ⇒ Object
Returns the value of attribute locals.
4
5
6
|
# File 'lib/nydp/vm.rb', line 4
def locals
@locals
end
|
Instance Method Details
#error ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/nydp/vm.rb', line 45
def error
msg = ""
msg << "\n"
msg << "\ninstruction stack"
msg << "\n================="
instructions.each_with_index do |ii, ix|
msg << "\ninstructions##{ix} : #{ii} #{ii.source if ii.respond_to?(:source)}"
end
msg << "\n"
msg << "\n"
msg << "\ncontext stack"
msg << "\n================="
contexts.each_with_index do |ctx, ix|
msg << "\ncontext##{ix} :\n#{ctx}"
end
msg << "\n"
msg << "\n"
instructions = []
contexts = []
args = [Nydp.NIL]
msg
end
|
#peek_arg ⇒ Object
28
|
# File 'lib/nydp/vm.rb', line 28
def peek_arg; args.last; end
|
#peek_context ⇒ Object
25
|
# File 'lib/nydp/vm.rb', line 25
def peek_context; current_context; end
|
#pop_arg ⇒ Object
29
|
# File 'lib/nydp/vm.rb', line 29
def pop_arg; args.pop; end
|
#pop_args(count, tail = Nydp.NIL) ⇒ Object
36
37
38
39
40
41
42
43
|
# File 'lib/nydp/vm.rb', line 36
def pop_args count, tail=Nydp.NIL
case count
when 0
tail
else
pop_args(count - 1, cons(pop_arg, tail))
end
end
|
#pop_context ⇒ Object
26
|
# File 'lib/nydp/vm.rb', line 26
def pop_context; contexts.pop; end
|
#push_arg(a) ⇒ Object
27
|
# File 'lib/nydp/vm.rb', line 27
def push_arg a; args.push a; end
|
#push_instructions(ii, ctx) ⇒ Object
31
32
33
34
|
# File 'lib/nydp/vm.rb', line 31
def push_instructions ii, ctx
instructions.push ii
contexts.push ctx
end
|
#thread(expr) ⇒ Object
13
14
15
16
17
18
19
20
21
22
23
|
# File 'lib/nydp/vm.rb', line 13
def thread expr
instructions.push expr
while instructions.length > 0
self.current_context = contexts.last
ii = instructions.pop
i = ii.car
ii.cdr.repush instructions, contexts
i.execute(self)
end
pop_arg
end
|