tui_calc.rb
# check for properly nested parens
# returns an array of the form [[bracket, index in nest, index in expr_arr, match index], ...]
# if valid or nil if invalid nesting
# This version of nest checking is a bit more complex because I need to track
# match indexes for my calculator implementation
check for properly nested parens
returns an array of the form [[bracket, index in nest, index in expr_arr, match index], ...]
if valid or nil if invalid nesting
This version of nest checking is a bit more complex because I need to track
match indexes for my calculator implementation
check for properly nested parens returns an array of the form [[ bracket index in nest index in expr_arr match index ] ... ] if valid or nil if invalid nesting This version of nest checking is a bit more complex because I need to track match indexes for my calculator implementation
def check_parens(expr_arr)
  nest = []  
  stack = []  
  nest_index = 0 
  count = 0  
  expr_arr.each_index do |j|
    if PAIRS.key?(expr_arr[j]) or PAIRS.value?(expr_arr[j])
      nest.push([expr_arr[j], count, j, nil])
      count += 1
    end
  end
  if nest.length == 0
    return nest
  end
  stack.push(nest[nest_index])
  nest_index += 1
  until nest_index == nest.length do
    if stack.empty?
      stack.push(nest[nest_index])
      nest_index += 1
    end
    if PAIRS[stack.last[0]] == nest[nest_index][0]
      nest[stack.last[1]][3] = nest[nest_index][2]
      nest[nest_index][3] = stack.last[2]
      nest_index += 1
      stack.pop
    else
      stack.push(nest[nest_index])
      nest_index += 1
    end
  end
  if stack.empty?
    return nest
  else
    return nil
  end
end
def check_parens ( expr_arr ) nest = [ ] stack = [ ] nest_index = 0 count = 0 expr_arr . each_index do | j | if PAIRS . key? ( expr_arr [ j ] ) or PAIRS . value? ( expr_arr [ j ] ) nest . push ( [ expr_arr [ j ] , count , j , nil ] ) count += 1 end end if nest . length == 0 return nest end stack . push ( nest [ nest_index ] ) nest_index += 1 until nest_index == nest . length do if stack . empty? stack . push ( nest [ nest_index ] ) nest_index += 1 end if PAIRS [ stack . last [ 0 ] ] == nest [ nest_index ] [ 0 ] nest [ stack . last [ 1 ] ] [ 3 ] = nest [ nest_index ] [ 2 ] nest [ nest_index ] [ 3 ] = stack . last [ 2 ] nest_index += 1 stack . pop else stack . push ( nest [ nest_index ] ) nest_index += 1 end end if stack . empty? return nest else return nil end end
check for properly nested parens
returns an array of the form [[bracket, index in nest, index in expr_arr, match index], ...]
if valid or nil if invalid nesting
This version of nest checking is a bit more complex because I need to track
match indexes for my calculator implementation
check for properly nested parens returns an array of the form [[ bracket index in nest index in expr_arr match index ] ... ] if valid or nil if invalid nesting This version of nest checking is a bit more complex because I need to track match indexes for my calculator implementation
# set up # Will hold the brackets from expr_arr. Form [[bracket, i, j, match j]] # where i is index in nest, j is index in expr_arr # array used like stack # used to track our position in nest. I would use nest.shift, # but I need to preserve nest because it has the info I want to return # used to set i from above # put brackets from expression into nest # if there are no brackets, return # initialize stack # This loop checks if current bracket in nest matches top of stack. deletes them # if yes, adds bracket from nest to stack if no # If stack is empty, init again # if match found, save index and pop stack # remember: [0] is bracket, [1] is index in nest, [2] is index in expr_arr #           and [3] is index of match in expr_arr # if stack empty after getting to end of nest, nest is valid
expr_arr
<not_specific>