module Validator

Validator module is used to validate if the user input is correct

Public Instance Methods

balanced_brackets?(token) click to toggle source
# File lib/lisp/interpreter/helpers/validator.rb, line 3
def balanced_brackets?(token)
  strim = token.gsub(/[^\[\]\(\)\{\}]/, '')
  return true if strim.empty?
  return false if strim.size.odd?
  loop do
    s = strim.gsub('()', '').gsub('[]', '').gsub('{}', '')
    return true if s.empty?
    return false if s == strim
    strim = s
  end
end
balanced_quotes?(token) click to toggle source
# File lib/lisp/interpreter/helpers/validator.rb, line 15
def balanced_quotes?(token)
  token.count('"').even?
end
valid_function(fn) click to toggle source
# File lib/lisp/interpreter/helpers/validator.rb, line 28
def valid_function(fn)
  idx = fn[0] == '(' ? (find_bracket_idx fn, 0) : 0
  f =
    if idx.zero?
      predefined_method_caller [fn[idx]]
    else
      calc_input_val fn[0..idx]
    end
  valid_function? f, fn, idx
  [f, fn[idx + 1..-1]]
end
valid_var(var) click to toggle source
# File lib/lisp/interpreter/helpers/validator.rb, line 24
def valid_var(var)
  (valid_literals var) || (valid_objects var)
end
valid_var_name(var) click to toggle source
# File lib/lisp/interpreter/helpers/validator.rb, line 19
def valid_var_name(var)
  symbols = %r{[<|<=|=|>|>=|*|\/|+|-|&|^|#|@|]}
  !var.match(/[[:alpha:]]/).nil? || !var.match(symbols).nil?
end

Private Instance Methods

valid_function?(f, fn, idx) click to toggle source
# File lib/lisp/interpreter/helpers/validator.rb, line 55
def valid_function?(f, fn, idx)
  idx = find_bracket_idx fn, 1 if fn[idx] == '\''
  raise no_procedure_build fn[0..idx].join if f.nil? && (!f.is_a? Proc)
end
valid_literals(var) click to toggle source
# File lib/lisp/interpreter/helpers/validator.rb, line 42
def valid_literals(var)
  number = check_for_num var
  string = check_for_string var
  boolean = check_for_bool var
  symbol = check_for_symbol var
  quote = check_for_quote var
  number || string || boolean || symbol || quote
end
valid_objects(var) click to toggle source
# File lib/lisp/interpreter/helpers/validator.rb, line 51
def valid_objects(var)
  var.list? || var.pair?
end
validate_call_method(m_name) click to toggle source
# File lib/lisp/interpreter/helpers/validator.rb, line 60
def validate_call_method(m_name)
  raise no_procedure_build m_name.to_s if valid_var m_name.to_s
end