Class: Nydp::Compiler
- Extended by:
- Helper
- Defined in:
- lib/nydp/compiler.rb
Class Method Summary collapse
- .compile(expression, bindings, ns) ⇒ Object
- .compile_each(expr, bindings, ns) ⇒ Object
- .compile_expr(expression, bindings, ns) ⇒ Object
- .compile_pair(expression, bindings, ns) ⇒ Object
- .maybe_cons(a, b) ⇒ Object
Methods included from Helper
cons, list, literal?, pair?, sig, sym, sym?
Methods included from Converter
Class Method Details
.compile(expression, bindings, ns) ⇒ Object
11 12 13 14 15 |
# File 'lib/nydp/compiler.rb', line 11 def self.compile expression, bindings, ns compile_expr expression, bindings, ns rescue StandardError => e raise Nydp::Error.new "failed to compile expression:\n#{expression._nydp_inspect}\n#{e.}" end |
.compile_each(expr, bindings, ns) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/nydp/compiler.rb', line 38 def self.compile_each expr, bindings, ns if expr == nil expr elsif pair?(expr) maybe_cons compile(expr.car, bindings, ns), compile_each(expr.cdr, bindings, ns) else compile(expr, bindings, ns) end end |
.compile_expr(expression, bindings, ns) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/nydp/compiler.rb', line 17 def self.compile_expr expression, bindings, ns # if expression.is_a? Nydp::Symbol if expression.is_a? ::Symbol SymbolLookup.build expression, bindings, ns elsif literal? expression Literal.build expression, bindings, ns elsif expression.is_a? Nydp::Pair begin compile_pair expression, bindings, ns rescue => e raise "failed to compile #{expression._nydp_inspect}" end else raise Nydp::Error.new "failed to compile unrecognised expression:\n#{expression._nydp_inspect}\nwhich is a #{expression.class}" end end |
.compile_pair(expression, bindings, ns) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/nydp/compiler.rb', line 48 def self.compile_pair expression, bindings, ns key = expression.car if sym?(key, :cond) Cond.build expression.cdr, bindings, ns # todo: replace with function? (cond x (fn () iftrue) (fn () iffalse)) -->> performance issues, creating two closures for every cond invocation elsif sym?(key, :loop) Loop.build expression.cdr, bindings, ns elsif sym?(key, :quote) Literal.build expression.cadr, bindings, ns elsif sym?(key, :assign) Assignment.build expression.cdr, bindings, ns elsif sym?(key, :fn) InterpretedFunction.build expression.cadr, expression.cddr, bindings, ns else FunctionInvocation.build expression, bindings, ns end end |
.maybe_cons(a, b) ⇒ Object
34 35 36 |
# File 'lib/nydp/compiler.rb', line 34 def self.maybe_cons a, b (a == nil) ? b : cons(a, b) end |