Module: SchemeListsHelper

Included in:
SchemeLists
Defined in:
lib/lisp/interpreter/core/list.rb

Overview

Helper functions for SchemeLists

Instance Method Summary collapse

Instance Method Details

#build_cons_from_list(values) ⇒ Object



50
51
52
53
# File 'lib/lisp/interpreter/core/list.rb', line 50

def build_cons_from_list(values)
  spacer = values[1].size == 3 ? '' : ' '
  values[0].to_s + spacer + values[1][2..-2].to_s
end

#build_list(values) ⇒ Object



46
47
48
# File 'lib/lisp/interpreter/core/list.rb', line 46

def build_list(values)
  '\'(' + values.join(' ') + ')'
end

#car_cdr_infinite_helper(value, fn) ⇒ Object



106
107
108
109
110
111
# File 'lib/lisp/interpreter/core/list.rb', line 106

def car_cdr_infinite_helper(value, fn)
  fn.reverse[1..-2].each_char do |t|
    value = t == 'a' ? (car [value]) : (cdr [value])
  end
  value
end

#car_cdr_values(other) ⇒ Object



88
89
90
91
92
# File 'lib/lisp/interpreter/core/list.rb', line 88

def car_cdr_values(other)
  raise arg_err_build 1, other.size if other.size != 1
  return find_list_function_value other if other[0].list?
  (split_list_string other[0].to_s)[2..-2] if other[0].pair?
end

#cons_helper(values) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/lisp/interpreter/core/list.rb', line 55

def cons_helper(values)
  result =
    if values[1].to_s[0..1] == '\'('
      build_cons_from_list values
    else
      values[0].to_s + ' . ' + values[1].to_s
    end
  '\'(' + result + ')'
end

#evaluate_list(tokens, no_quotes) ⇒ Object



3
4
5
# File 'lib/lisp/interpreter/core/list.rb', line 3

def evaluate_list(tokens, no_quotes)
  find_all_values_list_evaluate tokens, no_quotes
end

#find_all_values_list_evaluate(tokens, no_quotes = false) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/lisp/interpreter/core/list.rb', line 36

def find_all_values_list_evaluate(tokens, no_quotes = false)
  result = []
  until tokens.empty?
    x, tokens = find_next_value tokens
    x = x[1..-2] if no_quotes && (check_for_string x.to_s)
    result << x
  end
  result
end

#find_idx_for_list(tokens) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/lisp/interpreter/core/list.rb', line 28

def find_idx_for_list(tokens)
  if tokens[0] == '('
    find_bracket_idx tokens, 0
  elsif tokens[1] == '('
    find_bracket_idx tokens, 1
  end
end

#find_list_function_value(other) ⇒ Object



77
78
79
80
81
# File 'lib/lisp/interpreter/core/list.rb', line 77

def find_list_function_value(other)
  raise arg_err_build 1, other.size if other.size != 1
  raise data_type_err '<list>', other[0].type unless other[0].list?
  split_list_as_string other[0].to_s
end

#find_to_evaluate_or_not(tokens, no_quotes = false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/lisp/interpreter/core/list.rb', line 17

def find_to_evaluate_or_not(tokens, no_quotes = false)
  if tokens[0..1].join == '(list'
    evaluate_list tokens[2..-2], no_quotes
  elsif tokens[0..1].join == '(cons'
    result = cons tokens[2..-2]
    result[2..-2].split(' ')
  else
    no_eval_list tokens[2..-2], no_quotes
  end
end

#get_cons_values(tokens) ⇒ Object



65
66
67
68
69
# File 'lib/lisp/interpreter/core/list.rb', line 65

def get_cons_values(tokens)
  result = get_k_arguments tokens, false, 2
  raise arg_err_build 2, result.size if result.size != 2
  result
end

#map_helper(lst, func) ⇒ Object



94
95
96
97
# File 'lib/lisp/interpreter/core/list.rb', line 94

def map_helper(lst, func)
  return lst.map { |t| func.call(*t) } if func.is_a? Proc
  lst.map { |t| send func, t }
end

#map_validate_helper(other) ⇒ Object



99
100
101
102
103
104
# File 'lib/lisp/interpreter/core/list.rb', line 99

def map_validate_helper(other)
  raise arg_err_build 'at least 2', 0 if other.empty?
  func, other = valid_function other
  raise arg_err_build 'at least 2', 1 if other.empty?
  [func, other]
end

#no_eval_list(tokens, no_quotes = false) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/lisp/interpreter/core/list.rb', line 7

def no_eval_list(tokens, no_quotes = false)
  result = []
  until tokens.empty?
    value, tokens = build_next_value_as_string tokens
    value = value[1..-2] if no_quotes && (check_for_string value.to_s)
    result << value
  end
  result
end

#split_list_as_string(list_as_string) ⇒ Object



83
84
85
86
# File 'lib/lisp/interpreter/core/list.rb', line 83

def split_list_as_string(list_as_string)
  split_value = split_list_string list_as_string.to_s
  no_eval_list split_value[2..-2]
end

#split_list_string(list) ⇒ Object



71
72
73
74
75
# File 'lib/lisp/interpreter/core/list.rb', line 71

def split_list_string(list)
  result = list.split(/(\(|\)|\.)|\ /)
  result.delete('')
  result
end