Class: Lignite::BodyCompiler

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

Overview

Extends OpCompiler by

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VariableDeclarer

#array8, #data16, #data32, #data8, #dataf, #datas

Constructor Details

#initialize(globals, locals) ⇒ BodyCompiler

Returns a new instance of BodyCompiler.



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Delegate the ops to the OpCompiler, but also aggregate the result in @bytes.



49
50
51
52
53
# File 'lib/lignite/body_compiler.rb', line 49

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

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

Instance Attribute Details

#bytesByteString (readonly)

Returns:



7
8
9
# File 'lib/lignite/body_compiler.rb', line 7

def bytes
  @bytes
end

#localsVariables (readonly)

Returns:



9
10
11
# File 'lib/lignite/body_compiler.rb', line 9

def locals
  @locals
end

Instance Method Details

#if(flag8, &body) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/lignite/body_compiler.rb', line 23

def if(flag8, &body)
  subc = BodyCompiler.new(@globals, @locals)
  subc.instance_exec(&body)

  jr_false(flag8, Complex(subc.bytes.bytesize, 2))
  @bytes << subc.bytes
end

#loop(&body) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/lignite/body_compiler.rb', line 31

def loop(&body)
  subc = BodyCompiler.new(@globals, @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

#loop_while_postcond(flag8, &body) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/lignite/body_compiler.rb', line 39

def loop_while_postcond(flag8, &body)
  subc = BodyCompiler.new(@globals, @locals)
  subc.instance_exec(&body)
  @bytes << subc.bytes
  # the jump takes up 4 bytes: JR_TRUE, LV0(flag8), LC2, LO, HI
  jr_true(flag8, Complex(- (subc.bytes.bytesize + 5), 2))
end

#variablesObject



11
12
13
# File 'lib/lignite/body_compiler.rb', line 11

def variables
  locals
end