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



25
26
27
28
# File 'lib/lisp/interpreter/core/list.rb', line 25

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



81
82
83
84
85
86
87
88
89
# File 'lib/lisp/interpreter/core/list.rb', line 81

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



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

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

#call_car_cdr_infinite(fn, values) ⇒ Object



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

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

#car_cdr_values(other) ⇒ Object



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

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



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

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

#find_idx_for_list(tokens) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/lisp/interpreter/core/list.rb', line 13

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



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

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



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

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

#get_cons_values(tokens) ⇒ Object



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

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



69
70
71
72
# File 'lib/lisp/interpreter/core/list.rb', line 69

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



74
75
76
77
78
79
# File 'lib/lisp/interpreter/core/list.rb', line 74

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



3
4
5
6
7
8
9
10
11
# File 'lib/lisp/interpreter/core/list.rb', line 3

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



58
59
60
61
# File 'lib/lisp/interpreter/core/list.rb', line 58

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



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

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