class Campa::Lisp::Cond

Constants

FALSEY

Attributes

evaler[R]
printer[R]

Public Class Methods

new() click to toggle source
# File lib/campa/lisp/cond.rb, line 6
def initialize
  @printer = Printer.new
  @evaler = Evaler.new
end

Public Instance Methods

call(*conditions, env:) click to toggle source
# File lib/campa/lisp/cond.rb, line 15
def call(*conditions, env:)
  found = conditions.find do |cond|
    raise illegal_argument(cond) if !cond.is_a?(List)

    !FALSEY.include? evaler.call(cond.head, env)
  end

  eval_result(found, env)
end
macro?() click to toggle source
# File lib/campa/lisp/cond.rb, line 11
def macro?
  true
end

Private Instance Methods

eval_result(found, env) click to toggle source
# File lib/campa/lisp/cond.rb, line 33
def eval_result(found, env)
  return if found.nil?

  # For when condition is a "truethy" value
  #
  # (cond
  #   ((eq 1 2) 'no)
  #   (true 'yes)
  # )
  # => 'yes
  to_eval = found.tail == List::EMPTY ? [found.head] : found.tail.to_a
  to_eval.reduce(nil) do |_, expr|
    evaler.call(expr, env)
  end
end
illegal_argument(thing) click to toggle source
# File lib/campa/lisp/cond.rb, line 29
def illegal_argument(thing)
  Error::IllegalArgument.new(printer.call(thing), "list")
end