Sha256: cc39824bdf7f436841195989675a59e5fcc0a54811037208ecef0ef6701def30

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 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

1 entries across 1 versions & 1 rubygems

Version Path
lisp-interpreter-0.4.0 lib/lisp/interpreter/object.rb