Class: Microplane::VM

Inherits:
Object
  • Object
show all
Defined in:
lib/microplane/vm.rb

Overview

Implements the basic Microplane VM

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVM

Returns a new instance of VM.



12
13
14
15
16
17
18
# File 'lib/microplane/vm.rb', line 12

def initialize
  @stack = []
  @skip = false
  @dictionary = {}
  @libraries = []
  add_library Lib::Std
end

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



10
11
12
# File 'lib/microplane/vm.rb', line 10

def stack
  @stack
end

Class Method Details

.run(code) ⇒ Object



6
7
8
# File 'lib/microplane/vm.rb', line 6

def self.run(code)
  new.evaluate(code).pop
end

Instance Method Details

#add_library(lib) ⇒ Object



20
21
22
# File 'lib/microplane/vm.rb', line 20

def add_library(lib)
  lib.load_words(self)
end

#evaluate(code) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/microplane/vm.rb', line 24

def evaluate(code)
  tokens = code.split
  return define_new_word(tokens) if tokens.first == ':'
  tokens.each do |w|
    next if @skip == true && w != 'fi'
    parse(w)
  end
  self
end

#popObject



34
35
36
# File 'lib/microplane/vm.rb', line 34

def pop
  stack.pop
end