<commit_before>class Stack
  def initialize(name)
    @name = name
    @data = []
  end

  def pop
    if @data.length != 0 then
      return @data.pop
    else
      return 0
    end
  end

  def push(n)
    @data.push(n)
  end
  
  def peek
    return @data.last
  end

  def print
    while @data.length > 1 do
        puts pop
    end
  end

  def talk
    puts @name
  end
end

def is_opening_bracket(b)
  return "([{<".include? b
end

def is_closing_bracket(b)
  return ")]}>".include? b
end

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

def read_until_stack_end(s, start)
  stack_height = 0
  for i in (start + 1 .. s.length)
    if s[i] == '{' then
      stack_height += 1
    elsif s[i] == '}'
      stack_height -= 1
      if stack_height == -1 then
        return i
      end
    end
  end
  return nil
end

left = Stack.new("Left")
right = Stack.new("Right")
main_stack = Stack.new("Main")
active = left

source_file = File.open("source.brack", "r")
source = source_file.read
len = source.length

i = 0
n = 0
error = false
while true do
  cur = source[i..i+1] or source[i]
  if i >= source.length then
    break
  end

  if ['()', '[]', '{}', '<>'].include? cur then
    if cur == '()' then
      n += 1
    elsif cur == '[]' then
      n -= 1
    elsif cur == '{}' then
      n += active.pop
    else
      if active == left then 
        active = right
      else
        active = left
      end
    end
    i += 2
  else
    cur = cur[0]
    if is_opening_bracket(cur) then
      if cur == '{' and active.peek == 0 then
        new_index = read_until_stack_end(source, i)
        if new_index == nil then
          error = true
          break
        else
          i = new_index
        end
      else
        main_stack.push([cur, n, i])
      end
      n = 0
    elsif is_closing_bracket(cur) then
      data = main_stack.pop
      if not brackets_match(data[0], cur) then
        error = true
        break
      elsif cur == ')' then
        active.push(n)
      elsif cur == ']' then
        puts n
      elsif cur == '>' then
        n = 0
      elsif cur == '}' then
        if active.peek != 0 then
          i = data[2] - 1
        end
      end
    end
    i += 1
  end

  if i >= len then
    break
  end

end

if error then
  puts "Invalid character in source file!"
  exit
end

active.print<commit_msg>Tidying code according to QPaysTaxes recommendations.<commit_after>class Stack
  def initialize(name)
    @name = name
    @data = []
  end

  def pop
    if @data.length != 0 then
      return @data.pop
    else
      return 0
    end
  end

  def push(current_value)
    @data.push(current_value)
  end
  
  def peek
    return @data.last
  end

  def print
    while @data.length > 1 do
        puts pop
    end
  end

  def talk
    puts @name
  end
end

def is_opening_bracket?(b)
  return '([{<'.include? b
end

def is_closing_bracket?(b)
  return ')]}>'.include? b
end

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

def read_until_stack_end(s, start)
  stack_height = 0
  for i in (start + 1..s.length)
    if s[i] == '{' then
      stack_height += 1
    elsif s[i] == '}'
      stack_height -= 1
      if stack_height == -1 then
        return i
      end
    end
  end
  return nil
end

left = Stack.new('Left')
right = Stack.new('Right')
main_stack = Stack.new('Main')
active = left

source_file = File.open('source.brack', 'r')
source = source_file.read
source_length = source.length

source_index = 0
current_value = 0
error = false

while true do
  current_symbol = source[source_index..source_index+1] or source[source_index]
  if source_index >= source.length then
    break
  end

  if ['()', '[]', '{}', '<>'].include? current_symbol 
    case current_symbol
      when '()' then current_value += 1
      when '[]' then current_value -= 1
      when '{}' then current_value += active.pop
      when '<>' then active = active == left ? right : left
      else
        error = true
        break
    end
    source_index += 2

  else
    current_symbol = current_symbol[0]
    if is_opening_bracket?(current_symbol) then
      if current_symbol == '{' and active.peek == 0 then
        new_index = read_until_stack_end(source, source_index)
        if new_index == nil then
          error = true
          break
        else
          source_index = new_index
        end
      else
        main_stack.push([current_symbol, current_value, source_index])
      end
      current_value = 0
    elsif is_closing_bracket?(current_symbol) then
      data = main_stack.pop
      if not brackets_match?(data[0], current_symbol) then
        error = true
        break
      end

      case current_symbol
        when ')' then active.push(current_value)
        when ']' then puts current_value
        when '>' then current_value = 0
        when '}' then source_index = data[2] - 1 if active.peek != 0
        end
    end
    source_index += 1
  end

  if source_index >= source_length then
    break
  end

end

if error then
  puts 'Invalid character in source file!'
  exit
end

active.print
{'fraction_non_alphanumeric': 0.11400271897455817, 'fraction_numerical': 0.01048747329578559, 'mean_word_length': 2.0893821235752847, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5027047', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1734, 'n_words': 522}