Class: Lignite::BodyCompiler

Inherits:
Object
  • Object
show all
Includes:
VariableDeclarer
Defined in:
lib/lignite/body_compiler.rb

Overview

Extends OpCompiler by

Defined Under Namespace

Modules: VariableDeclarer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VariableDeclarer

#data32, #datas

Constructor Details

#initialize(locals) ⇒ BodyCompiler

Returns a new instance of BodyCompiler.



22
23
24
25
26
# File 'lib/lignite/body_compiler.rb', line 22

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



36
37
38
39
40
# File 'lib/lignite/body_compiler.rb', line 36

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

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

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



19
20
21
# File 'lib/lignite/body_compiler.rb', line 19

def bytes
  @bytes
end

#localsObject (readonly)

Returns the value of attribute locals.



20
21
22
# File 'lib/lignite/body_compiler.rb', line 20

def locals
  @locals
end

Instance Method Details

#loop(&body) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/lignite/body_compiler.rb', line 28

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