Class: Esoteric::VM

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(esm, logger = nil) ⇒ VM

Returns a new instance of VM.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/esoteric/vm.rb', line 13

def initialize(esm, logger=nil)
  @insns    = parse(esm)
  @stack    = []
  @heap     = Hash.new { raise RuntimeError, 'can not read uninitialized heap value' }
  @pc       = 0
  @labels   = {}
  @skip_to  = nil
  @caller   = []
  unless @logger = logger
    @logger = Logger.new(STDOUT)
    @logger.level = Logger::ERROR
  end
end

Class Method Details

.run(esm, logger = nil) ⇒ Object



9
10
11
# File 'lib/esoteric/vm.rb', line 9

def self.run(esm, logger=nil)
  new(esm,logger).run
end

Instance Method Details

#runObject



27
28
29
30
31
32
# File 'lib/esoteric/vm.rb', line 27

def run
  step while @pc < @insns.size
  raise RuntimeError, ':exit missing'
rescue ProgramInterrapt => e
  # successfully exit
end