Module: ValueFinder

Included in:
Tokenizer
Defined in:
lib/lisp/interpreter/value_finder.rb

Overview

Value finder module

Instance Method Summary collapse

Instance Method Details

#find_all_values(other) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/lisp/interpreter/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/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_next_function_value(other) ⇒ Object



21
22
23
24
25
26
# File 'lib/lisp/interpreter/value_finder.rb', line 21

def find_next_function_value(other)
  idx = (find_bracket_idx other, 0)
  value = calc_input_val other[0..idx]
  other = other[idx + 1..other.size]
  [value, other]
end

#find_next_value(other) ⇒ Object



56
57
58
59
# File 'lib/lisp/interpreter/value_finder.rb', line 56

def find_next_value(other)
  return [other[0], other[1..-1]] if other[0].is_a? Proc
  find_value_helper other
end

#find_next_value_helper(other) ⇒ Object



40
41
42
43
# File 'lib/lisp/interpreter/value_finder.rb', line 40

def find_next_value_helper(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_value_helper(other) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/lisp/interpreter/value_finder.rb', line 45

def find_value_helper(other)
  if other[0] == '('
    find_next_function_value other
  elsif other[0..1].join == '\'('
    find_next_value_helper other
  else
    value = get_var other[0].to_s
    [value, other[1..-1]]
  end
end

#size_for_list_elem(values) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lisp/interpreter/value_finder.rb', line 28

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