Class: Tokenizer

Inherits:
Object show all
Includes:
ErrorMessages, FunctionalScheme, SchemeBooleans, SchemeChecker, SchemeLists, SchemeNumbers, SchemeStrings, TokenizerHelper, Validator, ValueFinder
Defined in:
lib/lisp/interpreter/tokenizer.rb

Overview

Tokenizer class

Instance Method Summary collapse

Methods included from FunctionalScheme

#apply, #compose, #define, #filter, #foldl, #foldr, #lambda, #member, #remove

Methods included from FunctionalSchemeHelper

#arg_finder, #arg_finder_helper, #define_func_helper, #define_function, #define_var, #equalize_lists, #eval_lambda, #fetch_define, #find_params_lambda, #foldl_helper, #foldr_helper, #member_helper, #proc_lambda, #proc_lambda_helper, #set_values_define

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

Methods included from SchemeLists

#car, #car_cdr_infinite, #cdr, #cons, #length, #list, #list?, #map, #null?, #pair?, #reverse, #shuffle

Methods included from SchemeListsHelper

#build_cons_from_list, #build_list, #car_cdr_infinite_helper, #car_cdr_values, #cons_helper, #evaluate_list, #find_all_values_list_evaluate, #find_idx_for_list, #find_list_function_value, #find_to_evaluate_or_not, #get_cons_values, #map_helper, #map_validate_helper, #no_eval_list, #split_list_as_string, #split_list_string

Methods included from SchemeBooleans

#equal?, #if, #not

Methods included from SchemeBooleansHelper

#if_helper, #if_idx_helper

Methods included from SchemeStrings

#strcontains, #strdowncase, #string?, #strjoin, #strlen, #strlist, #strprefix, #strreplace, #strsplit, #strsufix, #strupcase, #substring

Methods included from SchemeStringsHelper

#arg_function_validator, #build_as_string_helper, #build_character, #build_next_value_as_string, #find_delimeter, #remove_carriage, #string_join_helper, #strjoin_validate, #substring_builder, #substring_validator

Methods included from SchemeNumbers

#*, #+, #-, #/, #<, #<=, #>, #>=, #abs, #add1, #denominator, #max, #min, #modulo, #numerator, #quotient, #remainder, #sub1

Methods included from SchemeNumbersHelper

#compare_value_arithmetic, #convert_to_num, #divide_number, #divide_special_convert, #find_idx_numerators, #get_num_denom, #get_one_arg_function, #num_denom_helper

Methods included from Validator

#balanced_brackets?, #balanced_quotes?, #valid_function, #valid_var, #valid_var_name

Methods included from SchemeChecker

#check_for_bool, #check_for_num, #check_for_quote, #check_for_string, #check_for_symbol, #check_instance_var

Methods included from ValueFinder

#find_all_values, #find_bracket_idx, #find_next_function_value, #find_next_value, #find_next_value_helper, #find_value_helper, #size_for_list_elem

Methods included from TokenizerHelper

#get_var, #init_do_not_calculate_fn, #init_functions, #init_predefined, #init_reserved_fn, #initialize, #reset, #set_reserved_keywords, #set_var, #syntax_methods

Methods included from ErrorMessages

#arg_err_build, #data_type_err, #no_procedure_build, #unbalanced_brackets_error, #unbalanced_quotes_error, #unbound_symbol_err

Instance Method Details

#calc_input_val(arr) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/lisp/interpreter/tokenizer.rb', line 118

def calc_input_val(arr)
  get_raw = (arr.is_a? Array) && arr.size > 1 && arr[0..1].join != '\'('
  return get_raw_value arr unless get_raw
  m_name = predefined_method_caller arr
  return check_car_cdr arr if m_name.nil?
  call_predefined_method m_name, arr
end

#call_predefined_method(m_name, arr) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/lisp/interpreter/tokenizer.rb', line 146

def call_predefined_method(m_name, arr)
  return special_check_proc m_name, arr if m_name.is_a? Proc
  if @do_not_calculate.include? m_name
    send m_name.to_s, arr[2..-2]
  elsif !m_name.nil?
    values = find_all_values arr[2..-2]
    validate_call_method m_name
    send m_name.to_s, values
  end
end

#check_car_cdr(arr) ⇒ Object



112
113
114
115
116
# File 'lib/lisp/interpreter/tokenizer.rb', line 112

def check_car_cdr(arr)
  result = arr[1].match(/c[ad]{2,}r/)
  raise arr[1].to_s + ' is not a function' if result.nil?
  car_cdr_infinite arr
end

#check_for_stl_function(arr) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/lisp/interpreter/tokenizer.rb', line 126

def check_for_stl_function(arr)
  idx = find_bracket_idx arr, 1
  func, = valid_function arr[1..idx]
  values = find_all_values arr[idx + 1..-2]
  return func.call(*values) if func.is_a? Proc
  calc_input_val ['(', func, *values, ')']
end

#get_raw_value(token) ⇒ Object



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

def get_raw_value(token)
  if token.pair? || token.list?
    build_list no_eval_list token[2..-2]
  else
    return if token.empty?
    token = token.join('') if token.is_a? Array
    return token if token =~ /c[ad]{2,}r/
    get_var token.to_s
  end
end

#method_caller_checker(token, operations) ⇒ Object



165
166
167
# File 'lib/lisp/interpreter/tokenizer.rb', line 165

def method_caller_checker(token, operations)
  !token.to_s.match(/[[:alpha:]]/).nil? || (operations.include? token.to_s)
end

#predefined_method_caller(arr) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/lisp/interpreter/tokenizer.rb', line 169

def predefined_method_caller(arr)
  operations = ['+', '-', '/', '*', '<', '<=', '>', '>=']
  m_name =
    arr.each do |t|
      break t if t.is_a? Proc
      break t if method_caller_checker t, operations
      break t unless t.match(/[[:digit:]]/).nil?
    end
  predefined_method_caller_helper m_name, operations
end

#predefined_method_caller_helper(m_name, operations) ⇒ Object



157
158
159
160
161
162
163
# File 'lib/lisp/interpreter/tokenizer.rb', line 157

def predefined_method_caller_helper(m_name, operations)
  return m_name if m_name.is_a? Proc
  return @procs[m_name] if @procs.key? m_name
  return m_name if operations.include? m_name
  return @functions[m_name] if @functions.key? m_name
  m_name if @functions.value? m_name
end

#special_check_proc(m_name, arr) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/lisp/interpreter/tokenizer.rb', line 134

def special_check_proc(m_name, arr)
  if arr[0..1].join == '(('
    check_for_stl_function arr
  else
    m_name.call(*arr[2..-2])
  end
end

#tokenize(token) ⇒ Object



101
102
103
104
105
106
107
108
109
110
# File 'lib/lisp/interpreter/tokenizer.rb', line 101

def tokenize(token)
  reset
  token.delete('')
  @other = token
  begin
    calc_input_val @other
  rescue ZeroDivisionError, RuntimeError => e
    e.message
  end
end

#validate_call_method(m_name) ⇒ Object



142
143
144
# File 'lib/lisp/interpreter/tokenizer.rb', line 142

def validate_call_method(m_name)
  raise m_name.to_s + ' is not a function' if valid_var m_name.to_s
end