class Braingasm::Compiler

Constants

READ_CELL

Attributes

loop_stack[RW]
prefixes[RW]

Public Class Methods

new() click to toggle source
# File lib/braingasm/compiler.rb, line 7
def initialize
  @prefixes = PrefixStack.new
  @loop_stack = []
end

Public Instance Methods

compare() click to toggle source
# File lib/braingasm/compiler.rb, line 134
def compare
  ->(m) { m.inst_compare_cells }
end
dec() click to toggle source
# File lib/braingasm/compiler.rb, line 78
def dec
  @prefixes.fix_params ->(n, m) { m.inst_dec(n) }
end
divide() click to toggle source
# File lib/braingasm/compiler.rb, line 86
def divide
  @prefixes.fix_params ->(n, m) { m.inst_divide(n) }, 2
end
inc() click to toggle source
# File lib/braingasm/compiler.rb, line 74
def inc
  @prefixes.fix_params ->(n, m) { m.inst_inc(n) }
end
jump(to) click to toggle source
# File lib/braingasm/compiler.rb, line 150
def jump(to)
  ->(m) { m.inst_jump(to) }
end
left() click to toggle source
# File lib/braingasm/compiler.rb, line 70
def left
  @prefixes.fix_params ->(n, m) { m.inst_left(n) }
end
loop_end(index) click to toggle source
# File lib/braingasm/compiler.rb, line 171
def loop_end(index)
  current_loop = @loop_stack.pop
  raise BraingasmError, "Unmatched `]`" unless current_loop
  current_loop.stop_index = index
  jump(current_loop.start_index)
end
loop_start(start_index) click to toggle source
# File lib/braingasm/compiler.rb, line 154
def loop_start(start_index)
  return prefixed_loop(start_index) unless @prefixes.empty?

  new_loop = Loop.new
  @loop_stack.push(new_loop)
  new_loop.start_index = start_index
  new_loop
end
multiply() click to toggle source
# File lib/braingasm/compiler.rb, line 82
def multiply
  @prefixes.fix_params ->(n, m) { m.inst_multiply(n) }, 2
end
oddity() click to toggle source
# File lib/braingasm/compiler.rb, line 55
def oddity
  if @prefixes.last.is_a?(Integer)
    x = @prefixes.pop
    read_cell if @prefixes.empty?
    push_prefix @prefixes.fix_params(->(n, m) { n % x == 0 ? 0 : 1 })
  else
    read_cell if @prefixes.empty?
    push_prefix @prefixes.fix_params(->(n, m) { n % 2 })
  end
end
parity() click to toggle source
# File lib/braingasm/compiler.rb, line 44
def parity
  if @prefixes.last.is_a?(Integer)
    x = @prefixes.pop
    read_cell if @prefixes.empty?
    push_prefix @prefixes.fix_params(->(n, m) { n % x == 0 ? 1 : 0 })
  else
    read_cell if @prefixes.empty?
    push_prefix @prefixes.fix_params(->(n, m) { (n % 2) ^ 1 })
  end
end
pos() click to toggle source
# File lib/braingasm/compiler.rb, line 22
def pos
  push_prefix ->(m) { m.pos }
end
prefixed_loop(start_index) click to toggle source
# File lib/braingasm/compiler.rb, line 163
def prefixed_loop(start_index)
  new_loop = FixedLoop.new
  @loop_stack.push(new_loop)
  new_loop.start_index = start_index + 1
  push_ctrl = @prefixes.fix_params ->(n, m) { m.inst_push_ctrl(n) }
  [push_ctrl, new_loop]
end
print() click to toggle source
print_int() click to toggle source
push_prefix(prefix) click to toggle source
# File lib/braingasm/compiler.rb, line 12
def push_prefix(prefix)
  @prefixes << prefix
  prefix
end
quit() click to toggle source
# File lib/braingasm/compiler.rb, line 138
def quit
  @prefixes.fix_params ->(n, m) { m.inst_quit(n) }, 1
end
random() click to toggle source
# File lib/braingasm/compiler.rb, line 26
def random
  random = proc { |n, _| rand n }
  return_max_value = proc { |_, _| Options[:cell_limit] }
  push_prefix @prefixes.fix_params(random, return_max_value)
end
read() click to toggle source
# File lib/braingasm/compiler.rb, line 106
def read
  if @prefixes.empty?
    ->(m) { m.inst_read_byte }
  elsif @prefixes.first.is_a? String
    string = @prefixes.first

    @prefixes.fix_params ->(n, m) {
      from, to = m.dp, m.dp + string.size

      if m.tape_limit && to > m.tape_limit
        limit = m.tape_limit
        cutoff = to - limit

        m.tape[from..limit] = string.bytes[0..cutoff]
        m.tape[0...cutoff] = string.bytes[(cutoff+1)..-1]
      else
        m.tape[from...to] = string.bytes
      end
    }
  else
    @prefixes.fix_params ->(n, m) { m.cell = n }
  end
end
read_cell() click to toggle source
# File lib/braingasm/compiler.rb, line 18
def read_cell
  push_prefix @prefixes.fix_params(READ_CELL)
end
read_int() click to toggle source
# File lib/braingasm/compiler.rb, line 130
def read_int
  @prefixes.fix_params ->(n, m) { m.inst_read_int(n) }, 10
end
right() click to toggle source
# File lib/braingasm/compiler.rb, line 66
def right
  @prefixes.fix_params ->(n, m) { m.inst_right(n) }
end
signed() click to toggle source
# File lib/braingasm/compiler.rb, line 38
def signed
  read_cell if @prefixes.empty?

  push_prefix @prefixes.fix_params(->(n, m) { n < 0 ? 1 : 0 })
end
tape_limit() click to toggle source
# File lib/braingasm/compiler.rb, line 142
def tape_limit
  if @prefixes.empty?
    push_prefix ->(m) { x = m.pos; x + (x < 0 ? -1 : 1) }
  end

  @prefixes.fix_params ->(n, m) { m.limit_tape(n) }
end
zero() click to toggle source
# File lib/braingasm/compiler.rb, line 32
def zero
  read_cell if @prefixes.empty?

  push_prefix @prefixes.fix_params(->(n, m) { n.zero? ? 1 : 0 })
end