Sha256: f6d81ebd4dac47b0eb65f8296d9efd3392e288a0b9d66b15523b93efb11acd80

Contents?: true

Size: 1.25 KB

Versions: 6

Compression:

Stored size: 1.25 KB

Contents

module WLang
  class Compiler
    class ToRubyCode < Temple::Generator

      define_options [:idgen, :myid]

      class IdGen
        def initialize; @current = 0;  end
        def next;       @current += 1; end
        def to_s;       @current.to_s; end
      end

      def idgen
        options[:idgen] ? options[:idgen] : (@idgen ||= IdGen.new)
      end

      def myid
        options[:myid] || 0
      end

      def call(x)
        compile(x)
      end

      def on_template(fn)
        call(fn)
      end

      def on_dispatch(meth, *procs)
        procs = procs.map{|p| call(p)}.join(', ')
        "d#{myid}.#{meth}(b#{myid}, #{procs})"
      end

      def on_arg(code)
        code.inspect
      end

      def on_modulo(dialect, fn)
        if fn.first == :arg
          call(fn)
        else
          id   = idgen.next
          code = call(fn)
          "Proc.new{|d#{id},b#{id}| #{code}.call(#{dialect}.new(d#{id}.options, d#{id}.template), b#{id}) }"
        end
      end

      def on_proc(code)
        id   = idgen.next
        gen  = ToRubyCode.new(:buffer => "b#{id}", :idgen => idgen, :myid => id)
        code = gen.call(code)
        "Proc.new{|d#{id},b#{id}| #{code} }"
      end

    end # class ToRubyCode
  end # class Compiler
end # module WLang

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
wlang-3.0.1 lib/wlang/compiler/to_ruby_code.rb
wlang-3.0.0 lib/wlang/compiler/to_ruby_code.rb
wlang-2.3.1 lib/wlang/compiler/to_ruby_code.rb
wlang-2.3.0 lib/wlang/compiler/to_ruby_code.rb
wlang-2.2.4 lib/wlang/compiler/to_ruby_code.rb
wlang-2.2.3 lib/wlang/compiler/to_ruby_code.rb