Module: ValueFinder
- Included in:
- Tokenizer
- Defined in:
- lib/lisp/interpreter/helpers/value_finder.rb
Overview
Value finder module
Instance Method Summary collapse
- #find_all_values(other) ⇒ Object
- #find_bracket_idx(other, first_bracket) ⇒ Object
- #find_list_value(other) ⇒ Object
- #find_next_raw_value(other) ⇒ Object
- #find_next_value(other) ⇒ Object
- #find_value_helper(other) ⇒ Object
- #get_raw_value(token) ⇒ Object
- #get_var(var) ⇒ Object
- #set_var(var, value) ⇒ Object
- #size_for_list_elem(values) ⇒ Object
Instance Method Details
#find_all_values(other) ⇒ Object
3 4 5 6 7 8 9 10 |
# File 'lib/lisp/interpreter/helpers/value_finder.rb', line 3 def find_all_values(other) result = [] until other.empty? x, other = find_next_value other result << x end result end |
#find_bracket_idx(other, first_bracket) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/lisp/interpreter/helpers/value_finder.rb', line 12 def find_bracket_idx(other, first_bracket) open_br = 0 other[first_bracket..other.size - 1].each_with_index do |token, idx| open_br += 1 if token == '(' open_br -= 1 if token == ')' return idx + first_bracket if open_br.zero? end end |
#find_list_value(other) ⇒ Object
33 34 35 36 |
# File 'lib/lisp/interpreter/helpers/value_finder.rb', line 33 def find_list_value(other) value = no_eval_list other[2..(find_bracket_idx other, 1) - 1] [(build_list value), other[3 + (size_for_list_elem value)..-1]] end |
#find_next_raw_value(other) ⇒ Object
38 39 40 |
# File 'lib/lisp/interpreter/helpers/value_finder.rb', line 38 def find_next_raw_value(other) [(get_var other[0].to_s), other[1..-1]] end |
#find_next_value(other) ⇒ Object
53 54 55 56 57 58 |
# File 'lib/lisp/interpreter/helpers/value_finder.rb', line 53 def find_next_value(other) return [other[0], other[1..-1]] if other[0].is_a? Proc match_inf = other[0].to_s.match(/c[ad]{2,}r/) return [(generate_infinite_car_cdr other[0]), other[1..-1]] if match_inf find_value_helper other end |
#find_value_helper(other) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/lisp/interpreter/helpers/value_finder.rb', line 42 def find_value_helper(other) if other[0] == '(' idx = find_idx_for_list other [(calc_input_val other[0..idx]), other[idx + 1..-1]] elsif other[0..1].join == '\'(' find_list_value other else find_next_raw_value other end end |
#get_raw_value(token) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/lisp/interpreter/helpers/value_finder.rb', line 77 def get_raw_value(token) if token.pair? || token.list? build_list no_eval_list token[2..-2] else return if token.empty? token = token.join('') if token.is_a? Array return (generate_infinite_car_cdr token) if token =~ /c[ad]{2,}r/ get_var token.to_s end end |
#get_var(var) ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'lib/lisp/interpreter/helpers/value_finder.rb', line 67 def get_var(var) check = check_instance_var var return @procs[var.to_s] if check val = (predefined_method_caller [var]) return val unless val.nil? valid = valid_var var var = var.to_num.to_s if check_for_num var valid ? var : (raise unbound_symbol_err var) end |
#set_var(var, value) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/lisp/interpreter/helpers/value_finder.rb', line 60 def set_var(var, value) valid = (valid_var value.to_s) || (value.is_a? Proc) raise 'Invalid parameter' unless valid || (value.is_a? Symbol) return if var == value.to_s @procs[var] = value end |
#size_for_list_elem(values) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/lisp/interpreter/helpers/value_finder.rb', line 21 def size_for_list_elem(values) result = [] values.each do |v| if v.include?('(') || v.include?(')') v.split(/(\(|\))|\ /).each { |t| result << t unless t == '' } else result << v end end result.size end |