Class: Nydp::Compiler

Inherits:
Object
  • Object
show all
Extended by:
Helper
Defined in:
lib/nydp/compiler.rb

Class Method Summary collapse

Methods included from Helper

cons, list, literal?, pair?, sym, sym?

Class Method Details

.compile(expression, bindings) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/nydp/compiler.rb', line 10

def self.compile expression, bindings
  if expression.is_a? Nydp::Symbol
    SymbolLookup.build expression, bindings
  elsif literal? expression
    Literal.build expression, bindings
  elsif expression.is_a? Nydp::Pair
    compile_pair expression, bindings
  end
end

.compile_each(expr, bindings) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/nydp/compiler.rb', line 24

def self.compile_each expr, bindings
  if Nydp.NIL.is?(expr)
    expr
  elsif pair?(expr)
    maybe_cons compile(expr.car, bindings), compile_each(expr.cdr, bindings)
  else
    compile(expr, bindings)
  end
end

.compile_pair(expression, bindings) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nydp/compiler.rb', line 34

def self.compile_pair expression, bindings
  key = expression.car
  if sym?(key, :cond)
    Cond.build expression.cdr, bindings
  elsif sym?(key, :quote)
    Literal.build expression.cadr, bindings
  elsif sym?(key, :assign)
    Assignment.build expression.cdr, bindings
  elsif sym?(key, :fn)
    InterpretedFunction.build expression.cadr, expression.cddr, bindings
  else
    FunctionInvocation.build expression, bindings
  end
end

.maybe_cons(a, b) ⇒ Object



20
21
22
# File 'lib/nydp/compiler.rb', line 20

def self.maybe_cons a, b
  Nydp.NIL.is?(a) ? b : cons(a, b)
end