Module: Validator

Included in:
Parser, Tokenizer
Defined in:
lib/lisp/interpreter/validator.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#balanced_brackets?(token) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
11
12
13
# File 'lib/lisp/interpreter/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) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/lisp/interpreter/validator.rb', line 15

def balanced_quotes?(token)
  token.count('"').even?
end

#valid_function(fn) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lisp/interpreter/validator.rb', line 27

def valid_function(fn)
  idx = fn[0] == '(' ? (find_bracket_idx fn, 0) : 0
  func =
    if idx.zero?
      predefined_method_caller [fn[idx]]
    else
      calc_input_val fn[0..idx]
    end
  raise 'No such procedure' if func.nil? && (!func.is_a? Proc)
  [func, fn[idx + 1..-1]]
end

#valid_var(var) ⇒ Object



23
24
25
# File 'lib/lisp/interpreter/validator.rb', line 23

def valid_var(var)
  (valid_literals var) || (valid_objects var)
end

#valid_var_name(var) ⇒ Object



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

def valid_var_name(var)
  !var.match(/^[[:alpha:]]+$/).nil?
end