Sha256: 586515e12d7bb6d26e1ea9d79ffdfe846f3f06491241e534f4b5632af186950a

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

# redefine method in Object class
class Object
  def number?
    to_f.to_s == to_s || to_i.to_s == to_s
  end

  def to_num
    return to_f if to_f.to_s == to_s
    return to_i if to_i.to_s == to_s
  end

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

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

  def list?
    return false if size < 3
    check_for_list
  end

  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

  def quote?
    return true if start_with? '\''
    false
  end

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

  def type
    return '<list>' if list?
    return '<pair>' if pair?
    return '<string>' if string?
    return '<number>' if number?
    return '<character>' if character?
    return '<boolean>' if boolean?
    '<quote>'
  end

  private

  def object_split
    result = to_s.split(/(\(|\)|\.)|\ /)
    result.delete('')
    result
  end

  def check_for_list
    res = to_a if is_a? Array
    res = object_split if is_a? String
    res[0..1].join == '\'(' && res[-1] == ')' && res[-3] != '.'
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lisp-interpreter-0.4.4 lib/lisp/interpreter/core/object.rb
lisp-interpreter-0.4.3 lib/lisp/interpreter/core/object.rb
lisp-interpreter-0.4.2 lib/lisp/interpreter/core/object.rb
lisp-interpreter-0.4.1 lib/lisp/interpreter/core/object.rb
lisp-interpreter-0.4.0 lib/lisp/interpreter/core/object.rb