Class: Ookie::Interpreter
- Inherits:
-
Object
- Object
- Ookie::Interpreter
- 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
-
#code ⇒ Object
Returns the value of attribute code.
-
#efd ⇒ Object
Returns the value of attribute efd.
-
#ifd ⇒ Object
Returns the value of attribute ifd.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#loops ⇒ Object
readonly
Returns the value of attribute loops.
-
#mem ⇒ Object
readonly
Returns the value of attribute mem.
-
#ofd ⇒ Object
Returns the value of attribute ofd.
-
#ooks ⇒ Object
readonly
Returns the value of attribute ooks.
-
#optimize ⇒ Object
writeonly
Sets the attribute optimize.
-
#pc ⇒ Object
readonly
Returns the value of attribute pc.
- #verbose(msg = nil) ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(code = '', ifd = STDIN, ofd = STDOUT, efd = STDERR) ⇒ Interpreter
constructor
A new instance of Interpreter.
- #reset! ⇒ Object
- #run(code = nil, lang = nil) ⇒ Object
- #stepi ⇒ Object
Constructor Details
#initialize(code = '', ifd = STDIN, ofd = STDOUT, efd = STDERR) ⇒ Interpreter
Returns a new instance of Interpreter.
36 37 38 39 40 41 42 |
# 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 @optimize = true reset! end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
29 30 31 |
# File 'lib/ookie/interpreter.rb', line 29 def code @code end |
#efd ⇒ Object
Returns the value of attribute efd.
29 30 31 |
# File 'lib/ookie/interpreter.rb', line 29 def efd @efd end |
#ifd ⇒ Object
Returns the value of attribute ifd.
29 30 31 |
# File 'lib/ookie/interpreter.rb', line 29 def ifd @ifd end |
#lang ⇒ Object
Returns the value of attribute lang.
29 30 31 |
# File 'lib/ookie/interpreter.rb', line 29 def lang @lang end |
#loops ⇒ Object (readonly)
Returns the value of attribute loops.
27 28 29 |
# File 'lib/ookie/interpreter.rb', line 27 def loops @loops end |
#mem ⇒ Object (readonly)
Returns the value of attribute mem.
27 28 29 |
# File 'lib/ookie/interpreter.rb', line 27 def mem @mem end |
#ofd ⇒ Object
Returns the value of attribute ofd.
29 30 31 |
# File 'lib/ookie/interpreter.rb', line 29 def ofd @ofd end |
#ooks ⇒ Object (readonly)
Returns the value of attribute ooks.
27 28 29 |
# File 'lib/ookie/interpreter.rb', line 27 def ooks @ooks end |
#optimize=(value) ⇒ Object
Sets the attribute optimize
29 30 31 |
# File 'lib/ookie/interpreter.rb', line 29 def optimize=(value) @optimize = value end |
#pc ⇒ Object (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
50 51 52 |
# File 'lib/ookie/interpreter.rb', line 50 def self.run(code, lang = :ook) new.run(code, lang) end |
Instance Method Details
#reset! ⇒ Object
44 45 46 47 48 |
# File 'lib/ookie/interpreter.rb', line 44 def reset! @pc = 0 @loops = [] @mem = MemoryArray.new end |
#run(code = nil, lang = nil) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/ookie/interpreter.rb', line 54 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 |
#stepi ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/ookie/interpreter.rb', line 63 def stepi insn = next_insn if insn.nil? raise UnmatchedStartLoop unless @loops.empty? raise EndOfInstructions end send insn @pc += 1 end |