Class: Practice::Implementation::Interpreter
- Inherits:
-
Object
- Object
- Practice::Implementation::Interpreter
- Defined in:
- lib/ruby/practice/implementation/interpreter.rb
Overview
Brainfuck interpreter written in Ruby.
Defined Under Namespace
Classes: Command
Class Method Summary collapse
- .command(c) ⇒ Object
- .process(cxt, bracemap) ⇒ Object
-
.symbols ⇒ Object
Get brainfuck symbols.
Instance Method Summary collapse
-
#evaluate(cxt) ⇒ Object
To evaluate the source code.
-
#initialize(source) ⇒ Interpreter
constructor
Initialize.
Constructor Details
#initialize(source) ⇒ Interpreter
Initialize.
80 81 82 83 84 85 |
# File 'lib/ruby/practice/implementation/interpreter.rb', line 80 def initialize(source) syms = Interpreter.symbols tokens = source.each_char.map { |c| c }.select { |c| syms.include?(c) } @commands = tokens.map { |c| Interpreter.command(c) } @bracemap = parse(@commands) end |
Class Method Details
.command(c) ⇒ Object
124 125 126 |
# File 'lib/ruby/practice/implementation/interpreter.rb', line 124 def command(c) @symbols[c] end |
.process(cxt, bracemap) ⇒ Object
12 13 14 15 |
# File 'lib/ruby/practice/implementation/interpreter.rb', line 12 def @lshift.process(cxt, _) cxt.cursor -= 1 raise ArgumentError.new('Subscript out of range') if cxt.cursor < 0 end |
.symbols ⇒ Object
Get brainfuck symbols
120 121 122 |
# File 'lib/ruby/practice/implementation/interpreter.rb', line 120 def symbols @symbols.keys end |
Instance Method Details
#evaluate(cxt) ⇒ Object
To evaluate the source code.
88 89 90 91 92 93 94 |
# File 'lib/ruby/practice/implementation/interpreter.rb', line 88 def evaluate(cxt) while cxt.counter < @commands.length command = @commands[cxt.counter] command.process(cxt, @bracemap) cxt.counter += 1 end end |