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(options = {}) ⇒ Parser

Returns a new instance of Parser.



53
54
55
56
57
58
# File 'lib/brainfucktt/parser.rb', line 53

def initialize(options={})
  options = { :input => STDIN, :output => STDOUT }.merge( convert_to_options(options) )
  
  @input, @output = options.values_at(:input, :output)
  @data, @pointer, @tree = Data.new, 0, nil
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



50
51
52
# File 'lib/brainfucktt/parser.rb', line 50

def data
  @data
end

#inputObject (readonly)

Returns the value of attribute input.



50
51
52
# File 'lib/brainfucktt/parser.rb', line 50

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



50
51
52
# File 'lib/brainfucktt/parser.rb', line 50

def output
  @output
end

#pointerObject

Returns the value of attribute pointer.



51
52
53
# File 'lib/brainfucktt/parser.rb', line 51

def pointer
  @pointer
end

#treeObject (readonly)

Returns the value of attribute tree.



50
51
52
# File 'lib/brainfucktt/parser.rb', line 50

def tree
  @tree
end

Class Method Details

.instanceBrainfucktt::Parser

Get a new or the cached instance of this class.

Returns:



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

def instance
  @instance ||= new
end

.language_parserBrainfucktt::Parser

Get a new or the cached instance of this class.

Returns:



27
28
29
# File 'lib/brainfucktt/parser.rb', line 27

def language_parser
  @language_parser ||= LanguageParser.new
end

.parse(code) ⇒ Brainfucktt::Parser

Parse the given Brainfuck code.

Parameters:

  • code (String, #to_s)

Returns:

Raises:



36
37
38
# File 'lib/brainfucktt/parser.rb', line 36

def parse(code)
  instance.parse(code)
end

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

Parse and run the given Brainfuck code.

Parameters:

  • code (String, #to_s) (defaults to: nil)
  • options (Hash, #to_hash, #to_h) (defaults to: {})


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

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

Instance Method Details

#byteBrainfucktt::Byte

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

Returns:



89
90
91
# File 'lib/brainfucktt/parser.rb', line 89

def byte
  @data[@pointer]
end

#byte=(value) ⇒ Brainfucktt::Byte

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

Returns:



96
97
98
# File 'lib/brainfucktt/parser.rb', line 96

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

#parse(code) ⇒ Brainfucktt::Parser

Parse the given Brainfuck code.

Parameters:

  • code (String, #to_s)

Returns:

Raises:



79
80
81
82
83
84
# File 'lib/brainfucktt/parser.rb', line 79

def parse(code)
  @tree = self.class.language_parser.parse(code)
  raise ParserError, self unless @tree
  
  self
end

#run(code = nil, options = {}) ⇒ Object

Run the parsed Brainfuck code.

Parameters:

  • code (String, #to_s) (defaults to: nil)

    The code to parse before running.

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

Raises:



65
66
67
68
69
70
71
72
# File 'lib/brainfucktt/parser.rb', line 65

def run(code=nil, options={})
  options = { :input => @input, :output => @output }.merge( convert_to_options(options) )
  @input, @output = options.values_at(:input, :output)
  
  parse(code) unless code.nil?
  
  @tree.run(self)
end