Class: Gisele::Compiling::Gts2Bytecode

Inherits:
Object
  • Object
show all
Defined in:
lib/gisele/compiling/gts2bytecode.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(builder = VM::Bytecode::Builder.new) ⇒ Gts2Bytecode

Returns a new instance of Gts2Bytecode.



7
8
9
# File 'lib/gisele/compiling/gts2bytecode.rb', line 7

def initialize(builder = VM::Bytecode::Builder.new)
  @builder = builder
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



5
6
7
# File 'lib/gisele/compiling/gts2bytecode.rb', line 5

def builder
  @builder
end

Class Method Details

.call(ts, namespace = nil) ⇒ Object



11
12
13
14
# File 'lib/gisele/compiling/gts2bytecode.rb', line 11

def self.call(ts, namespace = nil)
  builder = VM::Bytecode::Builder.new(namespace)
  Gts2Bytecode.new(builder).call(ts)
end

Instance Method Details

#call(ts) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/gisele/compiling/gts2bytecode.rb', line 16

def call(ts)
  builder.at do |b|
    b.then label(ts.initial_state)
  end
  ts.each_state do |s|
    send :"on_#{s[:kind]}", s
  end
  VM::Bytecode.coerce(builder.to_a)
end

#on_end(state) ⇒ Object



100
101
102
103
104
105
106
107
# File 'lib/gisele/compiling/gts2bytecode.rb', line 100

def on_end(state)
  unless state.out_edges.size <= 1
    raise ArgumentError, "Invalid :end state: #{state}"
  end
  at(state) do |b|
    b.then :notify
  end
end

#on_event(state) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gisele/compiling/gts2bytecode.rb', line 49

def on_event(state)
  unless state.out_edges.size == 1
    raise ArgumentError, "Invalid :event state #{state.inspect}"
  end
  edge = state.out_edges.first
  at(state) do |b|
    b.then label(edge.target)
    b.then label(edge)
  end
  at(edge) do |b|
    b.push  edge[:event_args] || []
    b.event edge.symbol
  end
end

#on_fork(state) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gisele/compiling/gts2bytecode.rb', line 75

def on_fork(state)
  join_edges = state.out_edges.select{|e| e.symbol == :"(wait)"}
  unless join_edges.size == 1
    raise ArgumentError, "Invalid :fork state"
  end
  join_state = join_edges.first.target
  targets = state.out_adjacent_states - [ join_state ]
  at(state) do |b|
    b.push label(join_state)
    b.push targets.map{|t| label(t)}
    b.then :fork
  end
end

#on_join(state) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/gisele/compiling/gts2bytecode.rb', line 89

def on_join(state)
  unless state.out_edges.size == 1
    raise ArgumentError, "Invalid :join state"
  end
  target = state.out_edges.first.target
  at(state) do |b|
    b.push :wake => label(target)
    b.then :join
  end
end

#on_launch(state) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gisele/compiling/gts2bytecode.rb', line 36

def on_launch(state)
  h = {}
  state.out_edges.each do |edge|
    h[edge.symbol] = label(edge.target)
  end
  at(state) do |b|
    b.push h
    b.flip
    b.get
    b.then
  end
end

#on_listen(state) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/gisele/compiling/gts2bytecode.rb', line 64

def on_listen(state)
  h = {}
  state.out_edges.each do |edge|
    h[edge.symbol] = label(edge.target)
  end
  at(state) do |b|
    b.push h
    b.then :listen
  end
end

#on_nop(state) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/gisele/compiling/gts2bytecode.rb', line 26

def on_nop(state)
  unless state.out_edges.size == 1
    raise ArgumentError, "Invalid :nop state"
  end
  edge = state.out_edges.first
  at(state) do |b|
    b.then label(edge.target)
  end
end