/
ast.go
250 lines (218 loc) · 5.78 KB
/
ast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package ast
import (
"fmt"
"github.com/antha-lang/antha/antha/anthalib/wtype"
"github.com/antha-lang/antha/graph"
)
// Options for computing dependencies
const (
AllDeps = iota // Follow all AST edges
DataDeps // Follow only consumer-producer edges
)
// A Location is a physical place
type Location interface{}
// A Node is the input to code generation. An abstract syntax tree generated
// via execution of an Antha protocol.
//
// The basic design philosophy is to capture the semantics of the Antha
// language while reducing the cases for code generation. A secondary goal is
// to ease the creation of the AST at runtime (e.g., online, incremental
// generation of nodes).
//
// Conveniently, a tree naturally expresses the single-use (i.e., destructive
// update) aspect of physical things, so the code generation keeps this
// representation longer than a traditional compiler flow would.
type Node interface {
graph.Node
NodeString() string
}
// A Command is high-level instruction. Both UseComp and Bundles are
// currently issued only by the ast, maker, and codegen areas of
// code. Command is different: the instruction trace is populated by
// commandInst objects, each of which contain a Command
// object. However, at that stage some of the fields of Command are
// not correctly populated, and so these are modified by the maker at
// the same point at which UseComp objects are issued.
type Command struct {
From []Node // Inputs
Request Request // Requirements for device selection
Inst interface{} // Command-specific data
Output []Inst // Output from compilation
}
// NodeString implements graph pretty printing
func (a *Command) NodeString() string {
return fmt.Sprintf("%+v", struct {
Request interface{}
Inst string
}{
Request: a.Request,
Inst: fmt.Sprintf("%T", a.Inst),
})
}
// A UseComp is a use of a liquid component
type UseComp struct {
From []Node
Value *wtype.Liquid
}
// NodeString implements graph pretty printing
func (a *UseComp) NodeString() string {
return fmt.Sprintf("%+v", struct {
Value interface{}
}{
Value: a.Value,
})
}
// A Bundle is an unordered collection of expressions
type Bundle struct {
From []Node
}
// NodeString implements graph pretty printing
func (a *Bundle) NodeString() string {
return ""
}
// A Graph is a view of the AST as a graph
type Graph struct {
Nodes []Node
whichDeps int
}
// NumNodes implements a Graph
func (a *Graph) NumNodes() int {
return len(a.Nodes)
}
// Node implements a Graph
func (a *Graph) Node(i int) graph.Node {
return a.Nodes[i]
}
func setOut(n Node, i, deps int, x Node) {
switch n := n.(type) {
case *UseComp:
n.From[i] = x
case *Bundle:
n.From[i] = x
case *Command:
n.From[i] = x
default:
panic(fmt.Sprintf("ast.setOut: unknown node type %T", n))
}
}
func getOut(n Node, i, deps int) Node {
switch n := n.(type) {
case *UseComp:
return n.From[i]
case *Bundle:
return n.From[i]
case *Command:
return n.From[i]
default:
panic(fmt.Sprintf("ast.getOut: unknown node type %T", n))
}
}
func numOuts(n Node, deps int) int {
switch n := n.(type) {
case *UseComp:
return len(n.From)
case *Bundle:
return len(n.From)
case *Command:
return len(n.From)
default:
panic(fmt.Sprintf("ast.numOuts: unknown node type %T", n))
}
}
// NumOuts implements a Graph
func (a *Graph) NumOuts(n graph.Node) int {
return numOuts(n.(Node), a.whichDeps)
}
// Out implements a Graph
func (a *Graph) Out(n graph.Node, i int) graph.Node {
return getOut(n.(Node), i, a.whichDeps)
}
// SetOut updates the ith output of node n
func (a *Graph) SetOut(n Node, i int, x Node) {
setOut(n.(Node), a.whichDeps, i, x)
}
// A ToGraphOpt are options for ToGraph
type ToGraphOpt struct {
Roots []Node // Roots of program
WhichDeps int // Edges to follow when building graph
}
// ToGraph creates a graph from a list of roots. Include any referenced ast
// nodes in the resulting graph.
func ToGraph(opt ToGraphOpt) *Graph {
g := &Graph{
whichDeps: opt.WhichDeps,
}
seen := make(map[graph.Node]bool)
for _, root := range opt.Roots {
// Traverse doesn't use Graph.NumNodes() or Graph.Node(int), so we can pass
// in our partially constructed graph to extract the reachable nodes in the
// AST
results, _ := graph.Visit(graph.VisitOpt{
Graph: g,
Root: root,
Visitor: func(n graph.Node) error {
if seen[n] {
return graph.ErrNextNode
}
return nil
},
})
for _, k := range results.Seen.Values() {
if seen[k] {
continue
}
g.Nodes = append(g.Nodes, k.(Node))
seen[k] = true
}
}
return g
}
// Deps constructs the data dependencies between a set of commands.
func Deps(roots []Node) graph.Graph {
g := ToGraph(ToGraphOpt{Roots: roots, WhichDeps: DataDeps})
root := make(map[graph.Node]bool)
for _, r := range roots {
root[r] = true
}
return graph.Eliminate(graph.EliminateOpt{
Graph: g,
In: func(n graph.Node) bool {
return root[n]
},
})
}
// FindReachingCommands returns the set of commands that have a path to the
// given nodes without any intervening commands.
func FindReachingCommands(nodes []Node) []*Command {
g := ToGraph(ToGraphOpt{Roots: nodes, WhichDeps: DataDeps})
var cmds []*Command
var queue []graph.Node
// Add immediate children to queue
for _, node := range nodes {
for i := 0; i < g.NumOuts(node); i++ {
queue = append(queue, g.Out(node, i))
}
}
// Breath-first search on queue
seen := make(map[graph.Node]bool)
for len(queue) > 0 {
node := queue[0]
queue = queue[1:]
// Check if we've been here before
if seen[node] {
continue
}
seen[node] = true
cmd, ok := node.(*Command)
if ok {
// Found a command, stop here
cmds = append(cmds, cmd)
} else {
// Keep looking
for i := 0; i < g.NumOuts(node); i++ {
queue = append(queue, g.Out(node, i))
}
}
}
return cmds
}