Class: Gisele::VM::Bytecode::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/gisele/vm/bytecode/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace = nil) ⇒ Builder

Returns a new instance of Builder.



8
9
10
11
12
# File 'lib/gisele/vm/bytecode/builder.rb', line 8

def initialize(namespace = nil)
  @namespace     = namespace
  @bytecode      = [:gvm]
  @current_block = nil
end

Instance Attribute Details

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/gisele/vm/bytecode/builder.rb', line 6

def namespace
  @namespace
end

Instance Method Details

#at(label = nil, auto = true) ⇒ Object

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gisele/vm/bytecode/builder.rb', line 21

def at(label = nil, auto=true)
  raise BadUsageError, "Previous block not dumped." if @current_block
  if label
    label = label(label) if auto
  else
    label = (namespace || "main").to_s.to_sym
  end
  @current_block = [:block, label]
  if block_given?
    yield(self)
    end_block
  end
end

#current_blockObject



14
15
16
17
18
19
# File 'lib/gisele/vm/bytecode/builder.rb', line 14

def current_block
  unless @current_block
    raise BadUsageError, "Bytecode builder misused: no current block"
  end
  @current_block
end

#end_blockObject



35
36
37
38
39
# File 'lib/gisele/vm/bytecode/builder.rb', line 35

def end_block
  bl, @current_block = @current_block, nil
  @bytecode << bl
  bl
end

#instruction(which, args) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/gisele/vm/bytecode/builder.rb', line 41

def instruction(which, args)
  instr = [which] + args
  if Grammar[which] === instr
    current_block << instr
    instr
  else
    raise InvalidBytecodeError, "Invalid instruction: #{instr.inspect}"
  end
end

#to_aObject



51
52
53
# File 'lib/gisele/vm/bytecode/builder.rb', line 51

def to_a
  @bytecode
end