Class: Braingasm::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/braingasm/parser.rb

Overview

Takes some input code and generates the program

Defined Under Namespace

Classes: Loop

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Parser

Returns a new instance of Parser.



9
10
11
12
13
# File 'lib/braingasm/parser.rb', line 9

def initialize(input)
  @input = input
  @program = []
  @loop_stack = []
end

Instance Attribute Details

#loop_stackObject

Returns the value of attribute loop_stack.



7
8
9
# File 'lib/braingasm/parser.rb', line 7

def loop_stack
  @loop_stack
end

#programObject

Returns the value of attribute program.



7
8
9
# File 'lib/braingasm/parser.rb', line 7

def program
  @program
end

Instance Method Details

#dec(n = 1) ⇒ Object



63
64
65
# File 'lib/braingasm/parser.rb', line 63

def dec(n=1)
  -> m { m.inst_dec(n) }
end

#inc(n = 1) ⇒ Object



59
60
61
# File 'lib/braingasm/parser.rb', line 59

def inc(n=1)
  -> m { m.inst_inc(n) }
end

#jump(to) ⇒ Object



75
76
77
# File 'lib/braingasm/parser.rb', line 75

def jump(to)
  -> m { m.inst_jump(to) }
end

#left(n = 1) ⇒ Object



55
56
57
# File 'lib/braingasm/parser.rb', line 55

def left(n=1)
  -> m { m.inst_left(n) }
end

#loop_endObject



86
87
88
89
90
91
92
# File 'lib/braingasm/parser.rb', line 86

def loop_end
    current_loop = @loop_stack.pop
    raise_parsing_error("Unmatched `]`") unless current_loop
    index = @program.size
    current_loop.stop_index = index
    jump(current_loop.start_index)
end

#loop_startObject



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

def loop_start()
    new_loop = Loop.new
    @loop_stack.push(new_loop)
    new_loop.start_index = @program.size
    new_loop
end

#parse_next(tokens) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/braingasm/parser.rb', line 24

def parse_next(tokens)
  case tokens.next
  when :right
    right()
  when :left
    left()
  when :plus
    inc()
  when :minus
    dec()
  when :period
    print()
  when :comma
    read()
  when :loop_start
    loop_start()
  when :loop_end
    loop_end()
  end
end

#parse_programObject



15
16
17
18
19
20
21
22
# File 'lib/braingasm/parser.rb', line 15

def parse_program
  loop do
    push_instruction parse_next(@input)
  end

  raise_parsing_error("Unmatched `[`") unless @loop_stack.empty?
  @program
end


67
68
69
# File 'lib/braingasm/parser.rb', line 67

def print()
  -> m { m.inst_print_cell }
end

#push_instruction(instruction) ⇒ Object



45
46
47
48
49
# File 'lib/braingasm/parser.rb', line 45

def push_instruction(instruction)
  return unless instruction
  @program.push instruction
  @program.size - 1
end

#raise_parsing_error(message) ⇒ Object

Raises:

  • (ParsingError.new(@input.line_numer, @input.column_number))


102
103
104
# File 'lib/braingasm/parser.rb', line 102

def raise_parsing_error(message)
  raise ParsingError.new(@input.line_numer, @input.column_number), message
end

#readObject



71
72
73
# File 'lib/braingasm/parser.rb', line 71

def read()
  -> m { m.inst_read_byte }
end

#right(n = 1) ⇒ Object



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

def right(n=1)
  -> m { m.inst_right(n) }
end