Skip to content

Commit

Permalink
Added Classic mode with -C flag
Browse files Browse the repository at this point in the history
  • Loading branch information
wheatwizard committed Sep 19, 2016
1 parent 3987731 commit efc8675
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
25 changes: 17 additions & 8 deletions Interpreter.rb
Expand Up @@ -32,9 +32,10 @@ class BrainFlakInterpreter
attr_accessor :current_value, :active_stack
attr_reader :running, :left, :right, :main_stack

def initialize(source, left_in, right_in, debug)
def initialize(source, left_in, right_in, debug, classic)
# Strips the source of any characters that aren't brackets or part of debug flags
@source = source.gsub(/(?:(?<=[()\[\]{}<>])|\s|^)[^#()\[\]{}<>]*/, "")
@classic = classic
@source = source.gsub(/(?:(?<=[()\[\]{}<>])|\s|^)[^#()\[\]{}<>]*/, "")
@left = Stack.new('Left')
@right = Stack.new('Right')
@main_stack = []
Expand All @@ -54,6 +55,9 @@ def initialize(source, left_in, right_in, debug)
remove_debug_flags(debug)
@running = @source.length > 0
@run_debug = !@running && @debug_flags.size > 0
if classic then
@source = @source.gsub(/\[/,":").gsub(/]/,";")
end
end

def inactive_stack
Expand Down Expand Up @@ -160,15 +164,15 @@ def do_debug_flag(index)
when :ij then
injection = $stdin.read
STDERR.puts
sub_interpreter = BrainFlakInterpreter.new(injection, @left.get_data, @right.get_data, true)
sub_interpreter = BrainFlakInterpreter.new(injection, @left.get_data, @right.get_data, true, @classic)
sub_interpreter.active_stack = @active_stack == @left ? sub_interpreter.left : sub_interpreter.right
sub_interpreter.current_value = @current_value
begin
while sub_interpreter.step
end
if sub_interpreter.main_stack.length > 0
unmatched_brak = sub_interpreter.main_stack[0]
raise BrainFlakError.new("Unclosed '%s' character." % unmatched_brak[0], unmatched_brak[2])
raise BrainFlakError.new("Unclosed %s character." % unmatched_brak[0].gsub(/:/,"["), unmatched_brak[2])
end
rescue Interrupt
STDERR.STDERR.puts "\nKeyboard Interrupt"
Expand Down Expand Up @@ -204,12 +208,13 @@ def step()
end
@cycles += 1
current_symbol = @source[@index..@index+1] or @source[@index]
if ['()', '[]', '{}', '<>'].include? current_symbol
if ['()', '[]', '{}', '<>', ':;'].include? current_symbol
case current_symbol
when '()' then @current_value += 1
when '[]' then @current_value += @active_stack.height
when '{}' then @current_value += @active_stack.pop
when '<>' then @active_stack = @active_stack == @left ? @right : @left
when ':;' then @current_value -= 1
end
@last_op = :nilad
@index += 2
Expand All @@ -219,7 +224,7 @@ def step()
if is_opening_bracket?(current_symbol) then
if current_symbol == '{' and @active_stack.peek == 0 then
new_index = read_until_matching(@source, @index)
raise BrainFlakError.new("Unmatched {", @index + 1) if new_index == nil
raise BrainFlakError.new("Unmatched { character.", @index + 1) if new_index == nil
@index = new_index
else
@main_stack.push([current_symbol, @current_value, @index])
Expand All @@ -228,10 +233,11 @@ def step()

elsif is_closing_bracket?(current_symbol) then
data = @main_stack.pop
raise BrainFlakError.new("Unmatched " + current_symbol, @index + 1) if data == nil
raise BrainFlakError.new("Mismatched closing bracket %s. Expected to close %s at character %d" % [current_symbol, data[0], data[2] + 1], @index + 1) if not brackets_match?(data[0], current_symbol)
raise BrainFlakError.new("Unmatched %s character." %current_symbol.gsub(/;/,"]"), @index + 1) if data == nil
raise BrainFlakError.new("Mismatched closing bracket %s. Expected to close %s at character %d" % [current_symbol.gsub(/;/,"]"), data[0].gsub(/:/,"["), data[2] + 1], @index + 1) if not brackets_match?(data[0], current_symbol)

case current_symbol
when ';' then puts @current_value
when ')' then @active_stack.push(@current_value)
when ']' then @current_value *= -1
when '>' then @current_value = 0
Expand Down Expand Up @@ -269,6 +275,9 @@ def debug_info

def inspect
source = String.new(str=@source)
if @classic then
source = source.gsub(/:/,"[").gsub(/;/,"]")
end
index = @index
offset = 0
@debug_flags.each_pair do |k,v|
Expand Down
9 changes: 7 additions & 2 deletions brain_flak.rb
Expand Up @@ -3,6 +3,7 @@

require 'optparse'

classic = false
debug = false
ascii_in = false
ascii_out = false
Expand All @@ -15,6 +16,10 @@
"Usage:\n"\
"\tbrain_flak [options] source_file args...\n\n"

opts.on("-C", "--classic", "Runs code as Brain-Flak Classic code") do
classic = true
end

opts.on("-d", "--debug", "Enables parsing of debug commands") do
debug = true
end
Expand Down Expand Up @@ -111,13 +116,13 @@
end
end
numbers.reverse! if !reverse
interpreter = BrainFlakInterpreter.new(source, numbers, [], debug)
interpreter = BrainFlakInterpreter.new(source, numbers, [], debug, classic)

while interpreter.step
end
if interpreter.main_stack.length > 0
unmatched_brak = interpreter.main_stack[0]
raise BrainFlakError.new("Unclosed '%s' character." % unmatched_brak[0], unmatched_brak[2])
raise BrainFlakError.new("Unclosed %s character." % unmatched_brak[0].gsub(/:/,"["), unmatched_brak[2])
end
interpreter.active_stack.print_stack(ascii_out,reverse,utf8)
rescue BrainFlakError => e
Expand Down
6 changes: 3 additions & 3 deletions stack.rb
Expand Up @@ -67,18 +67,18 @@ def set_data(data)

def is_opening_bracket?(b)
if b != nil then
return '([{<'.include? b
return '([{<:'.include? b
end
end

def is_closing_bracket?(b)
if b != nil then
return ')]}>'.include? b
return ')]}>;'.include? b
end
end

def brackets_match?(b1, b2)
s = [b1, b2].join('')
return ['()', '[]', '{}', '<>'].include? s
return ['()', '[]', '{}', '<>', ':;'].include? s
end

0 comments on commit efc8675

Please sign in to comment.