Sha256: bd3f66663c9f563df0f57b70a71232d7162ba46370c03e411fe6060d28fb49d7

Contents?: true

Size: 928 Bytes

Versions: 3

Compression:

Stored size: 928 Bytes

Contents

module Lignite
  # Extends {OpCompiler} by
  # - variable declarations: {VariableDeclarer}
  # - high level flow control: {#loop}
  class BodyCompiler

    # {#locals} are {Variables}
    module VariableDeclarer
      def data32(id)
        locals.add(id, 4)
      end

      def datas(id, size)
        locals.add(id, size)
      end
    end

    include VariableDeclarer
    attr_reader :bytes
    attr_reader :locals

    def initialize(locals)
      @bytes = ""
      @locals = locals
      @op_compiler = OpCompiler.new(nil, @locals)
    end

    def loop(&body)
      subc = BodyCompiler.new(@locals)
      subc.instance_exec(&body)
      @bytes << subc.bytes
      # the jump takes up 4 bytes: JR, LC2, LO, HI
      jr(Complex(- (subc.bytes.bytesize + 4), 2))
    end

    def method_missing(name, *args)
      super unless @op_compiler.respond_to?(name)

      @bytes += @op_compiler.send(name, *args)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lignite-0.1.2 lib/lignite/body_compiler.rb
lignite-0.1.1 lib/lignite/body_compiler.rb
lignite-0.1.0 lib/lignite/body_compiler.rb