Class: Tokenizer

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

Overview

Tokenizer class

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 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_function_car_cdr, #build_list, #call_car_cdr_infinite, #car_cdr_values, #cons_helper, #find_idx_for_list, #find_list_function_value, #generate_infinite_car_cdr, #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_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, #num_denom_validator, #rationalize_num

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_list_value, #find_next_raw_value, #find_next_value, #find_value_helper, #get_raw_value, #get_var, #set_var, #size_for_list_elem

Methods included from ErrorMessages

#arg_err_build, #no_procedure_build, #type_err, #unbalanced_brackets_error, #unbalanced_quotes_error, #unbound_symbol_err

Methods included from StlLoader

#initialize

Instance Method Details

#calc_input_val(arr) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/lisp/interpreter/tokenizer.rb', line 39

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



63
64
65
66
67
68
69
70
71
72
# File 'lib/lisp/interpreter/tokenizer.rb', line 63

def call_predefined_method(m_name, arr)
  return special_check_proc m_name, arr if m_name.is_a? Proc
  if DO_NOT_CALCULATE_FUNCTIONS.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



33
34
35
36
37
# File 'lib/lisp/interpreter/tokenizer.rb', line 33

def check_car_cdr(arr)
  result = arr[1].match(/c[ad]{2,}r/)
  raise no_procedure_build arr[1].to_s if result.nil?
  car_cdr_infinite arr
end

#check_for_stl_function(arr) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/lisp/interpreter/tokenizer.rb', line 47

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

#method_caller_checker(token, operations) ⇒ Object



82
83
84
# File 'lib/lisp/interpreter/tokenizer.rb', line 82

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

#predefined_method_caller(arr) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/lisp/interpreter/tokenizer.rb', line 86

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



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

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



55
56
57
58
59
60
61
# File 'lib/lisp/interpreter/tokenizer.rb', line 55

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

#syntax_methodsObject



19
20
21
# File 'lib/lisp/interpreter/tokenizer.rb', line 19

def syntax_methods
  @functions
end

#tokenize(token) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/lisp/interpreter/tokenizer.rb', line 23

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