module FunctionalSchemeHelper

FunctionalScheme helper

Public Instance Methods

arg_finder(args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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