Class: Lignite::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/lignite/condition.rb

Overview

Compile a conditional jump forward or backward

Direct Known Subclasses

Always, Flag, Gteq32, Lt32, Never, NotFlag

Instance Method Summary collapse

Instance Method Details

#cond_jump(_compiler, _offset) ⇒ Object

Call that instruction of the compiler that jumps by offset according to the condition that we implement

Raises:

  • (ScriptError)


6
7
8
# File 'lib/lignite/condition.rb', line 6

def cond_jump(_compiler, _offset)
  raise ScriptError, "subclasses must override this"
end

#jump_back(compiler, body_size, self_size = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/lignite/condition.rb', line 20

def jump_back(compiler, body_size, self_size = nil)
  if self_size.nil?
    fake = compiler.clone_context
    jump_back(fake, body_size, 0)
    self_size = fake.bytes.bytesize
  end

  cond_jump(compiler, - (body_size + self_size))
end

#jump_forward(compiler, body_size) ⇒ Object



16
17
18
# File 'lib/lignite/condition.rb', line 16

def jump_forward(compiler, body_size)
  cond_jump(compiler, body_size)
end

#notObject

Negation of this condition. An ‘if(cond){ body }` becomes roughly `cond.not.jump_forward(body.size); body`

Raises:

  • (ScriptError)


12
13
14
# File 'lib/lignite/condition.rb', line 12

def not
  raise ScriptError, "subclasses must override this"
end