Class: Lignite::BodyCompiler

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

Overview

Extends OpCompiler by

Constant Summary

Constants included from ParameterDeclarer

ParameterDeclarer::IN, ParameterDeclarer::OUT, ParameterDeclarer::PAR16, ParameterDeclarer::PAR32, ParameterDeclarer::PAR8, ParameterDeclarer::PARF, ParameterDeclarer::PARS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParameterDeclarer

#in16, #in32, #in8, #inf, #ins, #io16, #io32, #io8, #iof, #ios, #out16, #out32, #out8, #outf, #outs

Methods included from VariableDeclarer

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

Constructor Details

#initialize(globals, locals, declared_objects) ⇒ BodyCompiler

Returns a new instance of BodyCompiler.



53
54
55
56
57
58
59
# File 'lib/lignite/body_compiler.rb', line 53

def initialize(globals, locals, declared_objects)
  @bytes = ""
  @globals = globals
  @locals = locals
  @declared_objects = declared_objects
  @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.



115
116
117
118
119
# File 'lib/lignite/body_compiler.rb', line 115

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:



33
34
35
# File 'lib/lignite/body_compiler.rb', line 33

def bytes
  @bytes
end

#declared_objectsObject (readonly)

Returns the value of attribute declared_objects.



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

def declared_objects
  @declared_objects
end

#localsVariables (readonly)

Returns:



35
36
37
# File 'lib/lignite/body_compiler.rb', line 35

def locals
  @locals
end

Instance Method Details

#call(name, *args) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/lignite/body_compiler.rb', line 105

def call(name, *args)
  obj_id = declared_objects.index_of(name)
  raise "Name #{name} not found" if obj_id.nil?

  # TODO: check that args match their declaration
  super(obj_id, *args) # Ev3Ops::call
end

#clone_contextObject



61
62
63
# File 'lib/lignite/body_compiler.rb', line 61

def clone_context
  BodyCompiler.new(@globals, @locals, @declared_objects)
end

#if(flag8, &body) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/lignite/body_compiler.rb', line 65

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

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

#loop(&body) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/lignite/body_compiler.rb', line 73

def loop(&body)
  subc = BodyCompiler.new(@globals, @locals, @declared_objects)
  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(a, b) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/lignite/body_compiler.rb', line 89

def loop_while(a, b)
  if a.respond_to? :call
    loop_while_post(b, &a)
  else
    loop_while_pre(a, &b)
  end
end

#loop_while_post(condition, &body) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/lignite/body_compiler.rb', line 97

def loop_while_post(condition, &body)
  subc = BodyCompiler.new(@globals, @locals, @declared_objects)
  subc.instance_exec(&body)
  @bytes << subc.bytes
  body_size = subc.bytes.bytesize
  condition.jump_back(self, body_size)
end

#loop_while_postcond(flag8, &body) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/lignite/body_compiler.rb', line 81

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

#param_decl_headerObject



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

def param_decl_header
  parameters.param_decl_header
end

#parametersObject



44
45
46
# File 'lib/lignite/body_compiler.rb', line 44

def parameters
  locals
end

#respond_to_missing?(name, _include_private) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/lignite/body_compiler.rb', line 121

def respond_to_missing?(name, _include_private)
  @op_compiler.respond_to?(name) || super
end

#variablesObject



39
40
41
# File 'lib/lignite/body_compiler.rb', line 39

def variables
  locals
end