Class: Nydp::VM

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/nydp/vm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#cons, #list, #literal?, #pair?, #sym, #sym?

Constructor Details

#initializeVM

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

#argsObject

Returns the value of attribute args.



4
5
6
# File 'lib/nydp/vm.rb', line 4

def args
  @args
end

#contextsObject

Returns the value of attribute contexts.



4
5
6
# File 'lib/nydp/vm.rb', line 4

def contexts
  @contexts
end

#current_contextObject

Returns the value of attribute current_context.



4
5
6
# File 'lib/nydp/vm.rb', line 4

def current_context
  @current_context
end

#instructionsObject

Returns the value of attribute instructions.



4
5
6
# File 'lib/nydp/vm.rb', line 4

def instructions
  @instructions
end

#localsObject

Returns the value of attribute locals.



4
5
6
# File 'lib/nydp/vm.rb', line 4

def locals
  @locals
end

Instance Method Details

#errorObject



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_argObject



28
# File 'lib/nydp/vm.rb', line 28

def peek_arg;      args.last;            end

#peek_contextObject



25
# File 'lib/nydp/vm.rb', line 25

def peek_context;  current_context;      end

#pop_argObject



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_contextObject



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