Class: Object

Inherits:
BasicObject
Defined in:
lib/lisp/interpreter/core/object.rb

Overview

Object class

Instance Method Summary collapse

Instance Method Details

#boolean?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/lisp/interpreter/core/object.rb', line 41

def boolean?
  ['#t', '#f'].include? self
end

#character?Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'lib/lisp/interpreter/core/object.rb', line 15

def character?
  return true if self == '#\space'
  (start_with? '#\\') && (('a'..'z').to_a.include? self[2]) && size == 3
end

#list?Boolean

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/lisp/interpreter/core/object.rb', line 25

def list?
  return false if size < 3
  check_for_list
end

#number?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/lisp/interpreter/core/object.rb', line 6

def number?
  match(/^[\+\-]?\d*\.?\d+(?:[Ee][\+\-]?\d+)?$/)
end

#pair?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/lisp/interpreter/core/object.rb', line 30

def pair?
  res = object_split if is_a? String
  res = to_a if is_a? Array
  return true if res[-3] == '.'
  list? && !res[2..-2].empty?
end

#quote?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/lisp/interpreter/core/object.rb', line 37

def quote?
  start_with? '\''
end

#string?Boolean

Returns:

  • (Boolean)


20
21
22
23
# File 'lib/lisp/interpreter/core/object.rb', line 20

def string?
  return false unless self.class == String
  (start_with? '"') && (end_with? '"') && (size != 1)
end

#to_charObject



50
51
52
# File 'lib/lisp/interpreter/core/object.rb', line 50

def to_char
  '#\\' + (self == ' ' ? 'space' : self)
end

#to_numObject



10
11
12
13
# File 'lib/lisp/interpreter/core/object.rb', line 10

def to_num
  return to_f if to_s.include? '.'
  to_i
end

#typeObject



45
46
47
48
# File 'lib/lisp/interpreter/core/object.rb', line 45

def type
  fns = %w[list pair string number character boolean quote]
  fns.each { |t| return '<' + t + '>' if send t + '?' }
end