Module: Klam::CompilationStages::EmitRuby

Includes:
Template
Included in:
Klam::Compiler
Defined in:
lib/klam/compilation_stages/emit_ruby.rb

Overview

Emit Ruby

This is the final stage in the compilation pipeline. It is responsible for converting the compiler’s internal representation to a string containing Ruby code that is suitable for execution via instance_eval in the context of a Klam::Environment object.

By the time compilation reaches this stage, all of the heavy lifting should be complete. Emitting Ruby is simply a matter of transliterating the simplified s-expression into Ruby.

Constant Summary collapse

PRIMITIVE_TEMPLATES =
{
  :+ => [2, '($1 + $2)'],
  :- => [2, '($1 - $2)'],
  :* => [2, '($1 * $2)'],
  :< => [2, '($1 < $2)'],
  :> => [2, '($1 > $2)'],
  :<= => [2, '($1 <= $2)'],
  :>= => [2, '($1 >= $2)'],
  :"=" => [2, '($1 == $2)'],
  :absvector => [1, '::Klam::Absvector.new($1)'],
  :absvector? => [1, '$1.instance_of?(::Klam::Absvector)'],
  :"<-address" => [2, '$1[$2]'],
  :"address->" => [3, '$1.store($2, $3)'],
  :cons => [2, '::Klam::Cons.new($1, $2)'],
  :cons? => [1, '$1.instance_of?(::Klam::Cons)'],
  :hd => [1, '$1.hd'],
  :set => [2, '(@assignments[$1] = $2)'],
  :tl => [1, '$1.tl'],
  :value => [1, '@assignments[$1]']
}

Instance Method Summary collapse

Methods included from Template

#render_string

Instance Method Details

#emit_ruby(sexp) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/klam/compilation_stages/emit_ruby.rb', line 37

def emit_ruby(sexp)
  case sexp
  when Symbol
    emit_symbol(sexp)
  when String
    emit_string(sexp)
  when Klam::Constant, Klam::Variable, Numeric, true, false
    sexp.to_s
  when Array
    if sexp.empty?
      Klam::Primitives::Lists::EMPTY_LIST.inspect
    else
      emit_compound_form(sexp)
    end
  end
end