Class: Brainfucktt::Parser

Inherits:
Object
  • Object
show all
Includes:
ConversionHelpers
Defined in:
lib/brainfucktt/parser.rb

Overview

The Brainfuck parser.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tree) ⇒ Parser

Returns a new instance of Parser.



47
48
49
# File 'lib/brainfucktt/parser.rb', line 47

def initialize(tree)
  @data, @tree, @pointer = Data.new, tree, 0
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



44
45
46
# File 'lib/brainfucktt/parser.rb', line 44

def data
  @data
end

#inputObject (readonly)

Returns the value of attribute input.



44
45
46
# File 'lib/brainfucktt/parser.rb', line 44

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



44
45
46
# File 'lib/brainfucktt/parser.rb', line 44

def output
  @output
end

#pointerObject

Returns the value of attribute pointer.



45
46
47
# File 'lib/brainfucktt/parser.rb', line 45

def pointer
  @pointer
end

#treeObject (readonly)

Returns the value of attribute tree.



44
45
46
# File 'lib/brainfucktt/parser.rb', line 44

def tree
  @tree
end

Class Method Details

.instanceBrainfucktt::LanguageParser

Get a new or the cached instance of this class.

Returns:

  • (Brainfucktt::LanguageParser)


20
21
22
# File 'lib/brainfucktt/parser.rb', line 20

def instance
  @instance ||= LanguageParser.new
end

.parse(code) ⇒ Brainfucktt::Parser

Parse the given Brainfuck code.

Parameters:

  • code (String, #to_s)

Returns:

Raises:



29
30
31
32
33
34
# File 'lib/brainfucktt/parser.rb', line 29

def parse(code)
  tree = instance.parse(code)
  raise ParserError, instance unless tree
  
  new(tree)
end

.run(code, options = {}) ⇒ Object

Parse and run the given Brainfuck code.

Parameters:

  • code (String, #to_s)


39
40
41
# File 'lib/brainfucktt/parser.rb', line 39

def run(code, options={})
  parse(code).run(options)
end

Instance Method Details

#byteBrainfucktt::Byte

Returns the Byte instance within the @data collection at pointer.

Returns:



65
66
67
# File 'lib/brainfucktt/parser.rb', line 65

def byte
  @data[@pointer]
end

#byte=(value) ⇒ Brainfucktt::Byte

Set the value of the Byte instance within the @data collection at pointer.

Returns:



72
73
74
# File 'lib/brainfucktt/parser.rb', line 72

def byte=(value)
  @data[@pointer] = value
end

#run(options = {}) ⇒ Object

Run the parsed Brainfuck code.

Parameters:

  • options (Hash, #to_hash, #to_h) (defaults to: {})

Raises:



55
56
57
58
59
60
# File 'lib/brainfucktt/parser.rb', line 55

def run(options={})
  options = { input: STDIN, output: STDOUT }.merge( convert_to_options(options) )
  
  @input, @output = options.values_at(:input, :output)
  @tree.run(self)
end