Skip to content

Commit

Permalink
Implements the suggestions made in #75
Browse files Browse the repository at this point in the history
  • Loading branch information
wheatwizard committed Jun 11, 2017
1 parent b6788fd commit 4ddc636
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 41 deletions.
31 changes: 31 additions & 0 deletions BraceCheck.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require_relative './BrainFlakError.rb'

def braceCheck(source)
matches = ["()","[]","<>","{}"]
stack = []
checking = true
source.split("").each_with_index do |char, i|
if char == "#"
checking = false
elsif char == "\n"
checking = true
end

if not checking
true #Do nothing
elsif "([<{".include? char
stack.push([char, i])
elsif ")]>}".include? char
if stack.empty?
raise BrainFlakError.new("Unopened '%s' character." % char,i)
elsif matches.include? stack[-1][0]+char
stack.pop
else
raise BrainFlakError.new("Expected to close '%s' from location %s but instead encountered '%s'." % [stack[-1][0],stack[-1][1],char], i)
end
end
end
if not stack.empty?
raise BrainFlakError.new("Unclosed '%s' character." % stack[-1][0],stack[-1][1])
end
end
10 changes: 0 additions & 10 deletions BrainFlakInterpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def open_square()
def open_curly()
@main_stack.push(['{', 0, @index])
new_index = read_until_matching(@source, @index)
raise BrainFlakError.new("Unmatched '{' character", @index + 1) if new_index == nil
if active_stack.peek == 0 then
@main_stack.pop()
@index = new_index
Expand All @@ -55,24 +54,18 @@ def open_angle()

def close_round()
data = @main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
@active_stack.push(@current_value)
@current_value += data[1]
end

def close_square()
data = @main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
@current_value *= -1
@current_value += data[1]
end

def close_curly()
data = @main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
if @active_stack.peek != 0 then
@index = data[2] - 1
@last_op = :close_curly
Expand All @@ -82,9 +75,6 @@ def close_curly()

def close_angle()
data = @main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
#Here I use @source[@index] I am not sure why but @current symbol always yields nothing
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
@current_value = data[1]
end

Expand Down
10 changes: 0 additions & 10 deletions BrainFlueueInterpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def open_square()
def open_curly()
@main_stack.push(['{', 0, @index])
new_index = read_until_matching(@source, @index)
raise BrainFlakError.new("Unmatched '{' character", @index + 1) if new_index == nil
if not active_stack.data.first then
@main_stack.pop()
@index = new_index
Expand All @@ -55,24 +54,18 @@ def open_angle()

def close_round()
data = @main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
@active_stack.data.unshift(@current_value)
@current_value += data[1]
end

def close_square()
data = @main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
@current_value *= -1
@current_value += data[1]
end

def close_curly()
data = @main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
if @active_stack.data.first then
@index = data[2] - 1
@last_op = :close_curly
Expand All @@ -82,9 +75,6 @@ def close_curly()

def close_angle()
data = @main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
#Here I use @source[@index] I am not sure why but @current symbol always yields nothing
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
@current_value = data[1]
end

Expand Down
9 changes: 0 additions & 9 deletions ClassicInterpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def open_square()
def open_curly()
@main_stack.push(['{', 0, @index])
new_index = read_until_matching(@source, @index)
raise BrainFlakError.new("Unmatched '{' character", @index + 1) if new_index == nil
if active_stack.peek == 0 then
@main_stack.pop()
@index = new_index
Expand All @@ -55,24 +54,18 @@ def open_angle()

def close_round()
data = main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
@active_stack.push(@current_value)
@current_value += data[1]
end

def close_square()
data = main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
puts @current_value
@current_value += data[1]
end

def close_curly()
data = main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
if @active_stack.peek != 0 then
@index = data[2] - 1
@last_op = :close_curly
Expand All @@ -82,8 +75,6 @@ def close_curly()

def close_angle()
data = main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
@current_value = data[1]
end

Expand Down
7 changes: 0 additions & 7 deletions MiniFlakInterpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def open_square()
def open_curly()
@main_stack.push(['{', 0, @index])
new_index = read_until_matching(@source, @index)
raise BrainFlakError.new("Unmatched '{' character", @index + 1) if new_index == nil
if active_stack.peek == 0 then
@main_stack.pop()
@index = new_index
Expand All @@ -54,24 +53,18 @@ def open_angle()

def close_round()
data = main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
@active_stack.push(@current_value)
@current_value += data[1]
end

def close_square()
data = main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
@current_value *= -1
@current_value += data[1]
end

def close_curly()
data = main_stack.pop()
raise BrainFlakError.new("Unmatched '" + @source[@index] + "' character", @index + 1) if data == nil
raise BrainFlakError.new("Expected to close '%s' from location %d but instead encountered '%s' " % [data[0] , data[2] + 1, @source[@index]], @index + 1) if not brackets_match?(data[0], @source[@index])
if @active_stack.peek != 0 then
@index = data[2] - 1
@last_op = :close_curly
Expand Down
7 changes: 6 additions & 1 deletion brain_flak.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
require_relative './BrainFlueueInterpreter.rb'
require_relative './ClassicInterpreter.rb'
require_relative './MiniFlakInterpreter.rb'
require_relative './BraceCheck.rb'

VERSION_STRING = "Brain-Flak Ruby Interpreter v1.4.2"
VERSION_STRING = "Brain-Flak Ruby Interpreter v1.5.0-dev"

require 'optparse'

Expand Down Expand Up @@ -163,6 +164,10 @@
numbers.map!(&:ord)
end
numbers.reverse! if !reverse

#Check the braces are matched
braceCheck(source)

case mode
when "brainflak"
interpreter = BrainFlakInterpreter.new(source, numbers, [], debug, max_cycles)
Expand Down
4 changes: 0 additions & 4 deletions stack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,4 @@ def is_closing_bracket?(b)
end
end

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

0 comments on commit 4ddc636

Please sign in to comment.