Class: Ookie::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/ookie/interpreter.rb

Constant Summary collapse

RE_OOK_CODE =
/\s*(Ook[!?\.])\s*/
RE_BFK_CODE =
/\s*([<>\+-\.,\[\]])\s*/
CodeInfo =
Class.new(StandardError)
EndOfInstructions =
Class.new(CodeInfo)
CodeError =
Class.new(StandardError)
NoCode =
Class.new(CodeError)
UnmatchedStartLoop =
Class.new(CodeError)
UnmatchedEndLoop =
Class.new(CodeError)
OokCodeError =
Class.new(StandardError)
OddNumberOfOoks =
Class.new(OokCodeError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code = '', ifd = STDIN, ofd = STDOUT, efd = STDERR) ⇒ Interpreter

Returns a new instance of Interpreter.



36
37
38
39
40
41
# File 'lib/ookie/interpreter.rb', line 36

def initialize(code = '', ifd = STDIN, ofd = STDOUT, efd = STDERR)
  @verbose = false
  @code, @ifd, @ofd, @efd = code, ifd, ofd, efd
  @lang = :ook
  reset!
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



29
30
31
# File 'lib/ookie/interpreter.rb', line 29

def code
  @code
end

#efdObject

Returns the value of attribute efd.



29
30
31
# File 'lib/ookie/interpreter.rb', line 29

def efd
  @efd
end

#ifdObject

Returns the value of attribute ifd.



29
30
31
# File 'lib/ookie/interpreter.rb', line 29

def ifd
  @ifd
end

#langObject

Returns the value of attribute lang.



29
30
31
# File 'lib/ookie/interpreter.rb', line 29

def lang
  @lang
end

#loopsObject (readonly)

Returns the value of attribute loops.



27
28
29
# File 'lib/ookie/interpreter.rb', line 27

def loops
  @loops
end

#memObject (readonly)

Returns the value of attribute mem.



27
28
29
# File 'lib/ookie/interpreter.rb', line 27

def mem
  @mem
end

#ofdObject

Returns the value of attribute ofd.



29
30
31
# File 'lib/ookie/interpreter.rb', line 29

def ofd
  @ofd
end

#ooksObject (readonly)

Returns the value of attribute ooks.



27
28
29
# File 'lib/ookie/interpreter.rb', line 27

def ooks
  @ooks
end

#pcObject (readonly)

Returns the value of attribute pc.



27
28
29
# File 'lib/ookie/interpreter.rb', line 27

def pc
  @pc
end

#verbose(msg = nil) ⇒ Object



31
32
33
34
# File 'lib/ookie/interpreter.rb', line 31

def verbose(msg = nil)
  @efd.puts "#{self.class}: #{msg}" if @verbose and msg
  @verbose
end

Class Method Details

.run(code, lang = :ook) ⇒ Object



49
50
51
# File 'lib/ookie/interpreter.rb', line 49

def self.run(code, lang = :ook)
  new.run(code, lang)
end

Instance Method Details

#reset!Object



43
44
45
46
47
# File 'lib/ookie/interpreter.rb', line 43

def reset!
  @pc = 0
  @loops = []
  @mem = MemoryArray.new
end

#run(code = nil, lang = nil) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/ookie/interpreter.rb', line 53

def run(code = nil, lang = nil)
  @code = code if code
  @lang = lang if lang
  send("parse_#@lang")
  loop { stepi }
rescue EndOfInstructions
  verbose 'program finished correctly'
end

#stepiObject



62
63
64
65
66
67
68
69
70
# File 'lib/ookie/interpreter.rb', line 62

def stepi
  insn = next_insn
  if insn.nil?
    raise UnmatchedStartLoop unless @loops.empty?
    raise EndOfInstructions
  end
  send insn
  @pc += 1
end