Class: Nydp::Compiler

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

Class Method Summary collapse

Methods included from Helper

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

Methods included from Converter

#n2r, #r2n

Class Method Details

.compile(expression, bindings) ⇒ Object



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

def self.compile expression, bindings
  compile_expr expression, bindings
rescue StandardError => e
  raise Nydp::Error.new "failed to compile expression:\n#{expression.inspect}"
end

.compile_each(expr, bindings) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/nydp/compiler.rb', line 30

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_expr(expression, bindings) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/nydp/compiler.rb', line 16

def self.compile_expr 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_pair(expression, bindings) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nydp/compiler.rb', line 40

def self.compile_pair expression, bindings
  key = expression.car
  if sym?(key, :cond)
    Cond.build expression.cdr, bindings # todo: replace with function? (cond x (fn () iftrue) (fn () iffalse)) -->> performance issues, creating two closures for every cond invocation
  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



26
27
28
# File 'lib/nydp/compiler.rb', line 26

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