Class: Lisp::Logical

Inherits:
Object show all
Defined in:
lib/rubylisp/logical.rb

Class Method Summary collapse

Class Method Details

.and_impl(args, env) ⇒ Object



26
27
28
29
30
# File 'lib/rubylisp/logical.rb', line 26

def self.and_impl(args, env)
  return Lisp::Debug.process_error("and needs at least 2 arguments", env) unless args.length > 1
  value = !!args.inject(true) {|acc, item| acc && item.evaluate(env).value}
  return Lisp::Boolean.with_value(value)
end

.not_impl(args, env) ⇒ Object



32
33
34
35
# File 'lib/rubylisp/logical.rb', line 32

def self.not_impl(args, env)
  return Lisp::Debug.process_error("not needs a single argument", env) unless args.length == 1
  return Lisp::Boolean.with_value(!(args.car.evaluate(env).value))
end

.or_impl(args, env) ⇒ Object



20
21
22
23
24
# File 'lib/rubylisp/logical.rb', line 20

def self.or_impl(args, env)
  return Lisp::Debug.process_error("or needs at least 2 arguments", env) unless args.length > 1
  value = !!args.inject(false) {|acc, item| acc || item.evaluate(env).value}
  return Lisp::Boolean.with_value(value)
end

.registerObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rubylisp/logical.rb', line 5

def self.register
  Primitive.register("or",
                     "(or expression ...\n\nThe expressions are evaluated from left to right, and the value of the first expression that evaluates to a true value is returned. Any remaining expressions are not evaluated. If all expressions evaluate to false values, the value of the last expression is returned. If there are no expressions then #f is returned.",
                     true)  do |args, env|
                       Lisp::Logical::or_impl(args, env)
                     end
  Primitive.register("and",
                     "(and expression ...)\n\nThe expressions are evaluated from left to right, and the value of the first expression that evaluates to a false value is returned. Any remaining expressions are not evaluated. If all the expressions evaluate to true values, the value of the last expression is returned. If there are no expressions then #t is returned.",
                     true) do |args, env|
                       Lisp::Logical::and_impl(args, env)
                     end
  Primitive.register("not") {|args, env| Lisp::Logical::not_impl(args, env) }
end