Module: FunctionalSchemeHelper

Includes:
Optimize
Included in:
FunctionalScheme
Defined in:
lib/lisp/interpreter/core/functional.rb

Overview

FunctionalScheme helper

Constant Summary

Constants included from SchemeStl

SchemeStl::DO_NOT_CALCULATE_FUNCTIONS, SchemeStl::PREDEFINED_FUNCTIONS, SchemeStl::RESERVED_KEYWORDS, SchemeStl::SPECIAL_CHARACTER_FUNCTIONS

Instance Method Summary collapse

Methods included from Optimize

#apply_helper, #build_compose_expr, #call_compose, #call_compose_helper, #define_var_stl, #do_not_call_compose, #fetch_inner_scope, #filter_helper, #fold_values_helper, #get_fold_values, #inner_scope_replace, #rm_from_in_scope

Instance Method Details

#arg_finder(args) ⇒ Object



204
205
206
207
208
209
210
211
212
# File 'lib/lisp/interpreter/core/functional.rb', line 204

def arg_finder(args)
  result = []
  until args.empty?
    name = predefined_method_caller [args[0]]
    temp, args = arg_finder_helper name, args
    result << temp
  end
  result
end

#arg_finder_helper(name, args) ⇒ Object



195
196
197
198
199
200
201
202
# File 'lib/lisp/interpreter/core/functional.rb', line 195

def arg_finder_helper(name, args)
  if !name.nil?
    args = args[1..-1]
    [name, args]
  else
    find_next_value args
  end
end

#define_func_helper(other, params, args) ⇒ Object



190
191
192
193
# File 'lib/lisp/interpreter/core/functional.rb', line 190

def define_func_helper(other, params, args)
  temp = set_values_define other, params, args
  (find_all_values temp)[-1]
end

#define_function(other) ⇒ Object



214
215
216
217
218
219
# File 'lib/lisp/interpreter/core/functional.rb', line 214

def define_function(other)
  idx = find_bracket_idx other, 0
  name, *params = other[1..idx - 1]
  build_fn = ['(', 'lambda', '(', *params, ')', *other[idx + 1..-1], ')']
  define_var name, (find_all_values build_fn)
end

#define_var(var, values) ⇒ Object



173
174
175
176
177
# File 'lib/lisp/interpreter/core/functional.rb', line 173

def define_var(var, values)
  raise arg_err_build 1, values.size if values.size != 1
  raise var.to_s + ' is not valid variable name' unless valid_var_name var
  define_var_stl var, values
end

#equalize_lists(other) ⇒ Object



123
124
125
126
# File 'lib/lisp/interpreter/core/functional.rb', line 123

def equalize_lists(other)
  min = other.map(&:size).min
  other.map { |t| t[0..min - 1] }
end

#eval_lambda(other) ⇒ Object



140
141
142
143
144
# File 'lib/lisp/interpreter/core/functional.rb', line 140

def eval_lambda(other)
  idx = find_bracket_idx other.unshift('('), 0
  to_eval = other[1..idx - 1]
  (proc_lambda to_eval, true).call(*other[idx + 1..-1])
end

#fetch_define(other) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/lisp/interpreter/core/functional.rb', line 165

def fetch_define(other)
  if other[0] == '('
    define_function other
  else
    define_var other[0].to_s, (find_all_values other[1..-1])
  end
end

#find_params_lambda(other) ⇒ Object



134
135
136
137
138
# File 'lib/lisp/interpreter/core/functional.rb', line 134

def find_params_lambda(other)
  raise 'Invalid syntax' if other[0] != '('
  idx = find_bracket_idx other, 0
  [other[1..idx - 1], other[idx + 1..-1]]
end

#foldl_helper(func, accum, lst) ⇒ Object



109
110
111
112
113
114
# File 'lib/lisp/interpreter/core/functional.rb', line 109

def foldl_helper(func, accum, lst)
  return accum if lst.empty?
  value = func.call(*lst[0], accum.to_s) if func.is_a? Proc
  value = send func, [*lst[0], accum] if value.nil?
  foldl_helper func, value.to_s, lst[1..-1]
end

#foldr_helper(func, accum, lst) ⇒ Object



116
117
118
119
120
121
# File 'lib/lisp/interpreter/core/functional.rb', line 116

def foldr_helper(func, accum, lst)
  return accum if lst.empty?
  value = foldr_helper func, accum, lst[1..-1]
  return func.call(*lst[0], value.to_s) if func.is_a? Proc
  send func, [*lst[0], value.to_s]
end

#member_helper(to_check, values) ⇒ Object



128
129
130
131
132
# File 'lib/lisp/interpreter/core/functional.rb', line 128

def member_helper(to_check, values)
  return FALSE unless values.include? to_check
  idx = values.index(to_check)
  build_list values[idx..-1]
end

#proc_lambda(other, is_call = false) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/lisp/interpreter/core/functional.rb', line 152

def proc_lambda(other, is_call = false)
  params, other = find_params_lambda other
  other = fetch_inner_scope other
  valid = params.all? { |t| valid_var_name t } || is_call
  raise 'Invalid syntax' unless valid
  to_return = other[0..1].join == '(compose' && params.empty?
  return calc_input_val other if to_return
  proc = proc do |*args|
    proc_lambda_helper other, params, args
  end
  proc
end

#proc_lambda_helper(other, params, args) ⇒ Object



146
147
148
149
150
# File 'lib/lisp/interpreter/core/functional.rb', line 146

def proc_lambda_helper(other, params, args)
  args = arg_finder args
  raise arg_err_build params.size, args.size unless params.size == args.size
  define_func_helper other.dup, params.dup, args
end

#set_values_define(other, params, args) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/lisp/interpreter/core/functional.rb', line 179

def set_values_define(other, params, args)
  args = [args] unless args.is_a? Array
  other.each_with_index do |t, idx|
    if params.include? t
      i = params.index t
      other[idx] = args[i]
    end
  end
  other
end