Module: FunctionalSchemeHelper

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

Overview

FunctionalScheme helper

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



201
202
203
204
205
206
207
208
209
# File 'lib/lisp/interpreter/core/functional.rb', line 201

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



192
193
194
195
196
197
198
199
# File 'lib/lisp/interpreter/core/functional.rb', line 192

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



187
188
189
190
# File 'lib/lisp/interpreter/core/functional.rb', line 187

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

#define_function(other) ⇒ Object



211
212
213
214
215
216
# File 'lib/lisp/interpreter/core/functional.rb', line 211

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



170
171
172
173
174
# File 'lib/lisp/interpreter/core/functional.rb', line 170

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



120
121
122
123
# File 'lib/lisp/interpreter/core/functional.rb', line 120

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

#eval_lambda(other) ⇒ Object



137
138
139
140
141
# File 'lib/lisp/interpreter/core/functional.rb', line 137

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



162
163
164
165
166
167
168
# File 'lib/lisp/interpreter/core/functional.rb', line 162

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



131
132
133
134
135
# File 'lib/lisp/interpreter/core/functional.rb', line 131

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



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

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



113
114
115
116
117
118
# File 'lib/lisp/interpreter/core/functional.rb', line 113

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



125
126
127
128
129
# File 'lib/lisp/interpreter/core/functional.rb', line 125

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

#proc_lambda(other, is_call = false) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/lisp/interpreter/core/functional.rb', line 149

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



143
144
145
146
147
# File 'lib/lisp/interpreter/core/functional.rb', line 143

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



176
177
178
179
180
181
182
183
184
185
# File 'lib/lisp/interpreter/core/functional.rb', line 176

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