class Lisp
Attributes
array[RW]
result[RW]
string[RW]
type[RW]
Public Class Methods
new(string, type)
click to toggle source
# File lib/calculo/lisp.rb, line 8 def initialize(string, type) @string = string @type = type @array = parse(@string) @result = eval_lisp(@array) end
Public Instance Methods
eval_lisp(array)
click to toggle source
# File lib/calculo/lisp.rb, line 43 def eval_lisp(array) length, l, r = innermost_pair(array) result = execute(array[l..r],type) if length > 2 # Inserts the result in between the portions # of the array not including the portion just # used to calculate the result new_array = array[0..(l-1)] + [result] + array[(r+1)..-1] eval_lisp(new_array) elsif length == 2 return result end end
execute(array,type)
click to toggle source
# File lib/calculo/lisp.rb, line 16 def execute(array,type) case type when 'lisp' array[2..-2].reduce(array[1].to_sym) when 'reverse-lisp' || 'reverse' array[1..-3].reverse.reduce(array[-2].to_sym) end end
innermost_pair(array)
click to toggle source
# File lib/calculo/lisp.rb, line 25 def innermost_pair(array) l_parens = [] r_parens = [] array.each_with_index do |item, index| if item == "(" l_parens.push(index) elsif item == ")" r_parens.push(index) end end total_length = l_parens.length + r_parens.length return [total_length, l_parens[-1], r_parens[0]] end