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



39
40
41
42
# File 'lib/lisp/interpreter/core/list.rb', line 39

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

#build_function_car_cdr(fn) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/lisp/interpreter/core/list.rb', line 95

def build_function_car_cdr(fn)
  prepare_call = ['(', fn, 'x', ')']
  fn[1..-2].each_char do |t|
    prepare_call += (t == 'a' ? ['(', 'car'] : ['(', 'cdr'])
  end
  prepare_call << 'x'
  (fn.size - 2).times { prepare_call << ')' }
  prepare_call
end

#build_list(values) ⇒ Object



35
36
37
# File 'lib/lisp/interpreter/core/list.rb', line 35

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

#call_car_cdr_infinite(fn, values) ⇒ Object



110
111
112
# File 'lib/lisp/interpreter/core/list.rb', line 110

def call_car_cdr_infinite(fn, values)
  @procs[fn.to_s].call values
end

#car_cdr_values(other) ⇒ Object



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

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



44
45
46
47
48
49
50
51
52
# File 'lib/lisp/interpreter/core/list.rb', line 44

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



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

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



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

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



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

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

#generate_infinite_car_cdr(fn) ⇒ Object



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

def generate_infinite_car_cdr(fn)
  prepare_call = build_function_car_cdr fn
  define_function prepare_call
end

#get_cons_values(tokens) ⇒ Object



54
55
56
57
58
# File 'lib/lisp/interpreter/core/list.rb', line 54

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



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

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



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

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



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

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



60
61
62
63
64
# File 'lib/lisp/interpreter/core/list.rb', line 60

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