Sha256: 0a1fafc379eeda856e439b01442ce5852deda7db8347572d9e9cc5e20b1ddc0c

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

# Scheme booleans helper
module SchemeBooleansHelper
  def if_idx_helper(other)
    if other[0] == '('
      idx = find_bracket_idx other, 0
      (find_next_value other[idx + 1..-1])[0]
    else
      _, other = find_next_value other
      (find_next_value other)[0]
    end
  end

  def if_helper(expr, other)
    if expr == '#f'
      if_idx_helper other
    else
      (find_next_value other)[0]
    end
  end
end

# Scheme booleans module
module SchemeBooleans
  include SchemeBooleansHelper
  def equal?(other)
    raise arg_err_build 2, other.size if other.size != 2
    other[0].to_s == other[1].to_s ? '#t' : '#f'
  end

  def not(other)
    raise arg_err_build 1, other.size if other.size != 1
    valid = check_for_bool other[0]
    raise type_err '<boolean>', other[0].type unless valid
    other[0] == '#t' ? '#f' : '#t'
  end

  def if(other)
    raise arg_err_build 3, 0 if other.empty?
    expr, other = find_next_value other
    raise arg_err_build 3, other.size + 1 if other.size < 2
    if_helper expr, other
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lisp-interpreter-0.5.3 lib/lisp/interpreter/core/boolean.rb