-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparatype.go
216 lines (181 loc) · 4.88 KB
/
paratype.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
// The Main package for Paratype type analysis software.
package main
import (
"sync"
"fmt"
"Paratype/context"
"Paratype/paraparse"
"runtime"
"os"
"flag"
"time"
)
var Functions map[*context.Function]bool
var NumThreadsActive int
// Given a list of functions, will spawn function actors and resolve types
// Returns a list of type errors collected
//
func RunActors(f ...interface{}) []error {
Functions = make(map[*context.Function]bool)
// one can pass in multiple Function pointers or a slice of them
// tests are usually multiple while the parser will generate a slice; i.e.
// RunActors(f, g, h)
// and
// RunActors([]*context.Function{f, g, h})
// are equivalent
switch f[0].(type) {
case []*context.Function:
for _, fun := range f[0].([]*context.Function) {
Functions[fun] = true
}
case *context.Function:
for _, fun := range f {
Functions[fun.(*context.Function)] = true
}
}
// wait group that waits for implementation collection to finish
implementationWait := new(sync.WaitGroup)
// wait group to assist with halting when a type error is detected
killFlag := new(sync.WaitGroup)
// channel to send errors back to here from function actors
err := make(chan error, len(Functions))
for fActor := range Functions {
fActor.Initialize(implementationWait, killFlag)
//context.PrintAll(fActor)
}
for fActor := range Functions {
implementationWait.Add(1)
go fActor.Run(&Functions, err)
NumThreadsActive++
}
var s []error
// listen to error channel
for er := range err {
// every function will send something through the error channel back to
// RunActors: it will send nil if it finished without type errors and
// it will send the type error if one arose.
if er != nil {
fmt.Printf("%v\n", er.Error())
s = append(s, er)
} else {
// one goroutine finished
NumThreadsActive--
}
if NumThreadsActive == 0 {
// all goroutines finished without type errors
// close all channels
for f := range Functions {
f.Implement = true
close(f.Channel)
}
implementationWait.Wait()
close(err)
break;
} else if er != nil {
// type errors arose when goroutines ran
// set all goroutines to stop when next possible
killFlag.Add(1)
for f := range Functions {
// disable implementation collection
f.Implement = false
f.Dead = true
}
killFlag.Done()
for f := range Functions {
defer close(f.Channel)
}
defer close(err)
break;
}
}
return s
}
// Takes the number of processors and a list of functions
// Runs paratype and will print all implementations of functions to screen
//
func RunParatype(n int, out string, hprint bool, f ...interface{}) {
runtime.GOMAXPROCS(n)
var funcs []*context.Function
// f may either be an array of Function pointers or just many of them; i.e.
// RunParatype(4, f, g, h)
// and
// RunParatype(4, []*context.Function{f, g, h})
// are equivalent
switch f[0].(type) {
case []*context.Function:
for _, fun := range f[0].([]*context.Function) {
funcs = append(funcs, fun)
}
case *context.Function:
for _, fun := range f {
funcs = append(funcs, fun.(*context.Function))
}
}
// run actors, collect type errors
errors := RunActors(funcs)
if len(errors) > 0 {
// print type errors if there are any
for _, e := range errors {
fmt.Println(e)
}
} else {
/*for _, f := range funcs {
context.PrintAll(f)
}*/
noprint := false
for _, f := range funcs {
// type errros that arose during collection of implementations
// (means that there is a missing implementation)
if f.TypeError != nil {
fmt.Println(f.TypeError.Error())
noprint = true
}
}
if noprint == false && hprint == true {
fi, err := os.Create(out)
if err != nil {
panic(err)
}
defer fi.Close()
// we have working implementations! print 'em.
for _, f := range funcs {
for _, typemap := range f.Implementations {
f.PrintImplementation(typemap, fi)
}
}
}
}
}
// Dummy main function.
func main() {
procsPtr := flag.Int("procs", 4, "Number of processors.")
printPtr := flag.Bool("print", false, "Should I print?")
inFilePtr := flag.String("infile", "", "File to operate on.")
outFilePtr := flag.String("outfile", "", "File to output to.")
timePtr := flag.Bool("time", false, "Should I time?")
flag.Parse()
if *inFilePtr == "" {
fmt.Println("ERROR: OH NO! Provide an input file!")
return
}
if *printPtr == true && *outFilePtr == "" {
fmt.Println("ERROR: OH NO! Provide a file to write results to!")
return
}
begin := time.Now()
flist, err := paraparse.Setup(*inFilePtr, true)
end := time.Now()
if *timePtr == true {
fmt.Printf("SETUP: %d ", end.Sub(begin).Nanoseconds())
}
if err != nil {
fmt.Printf("%+v", err)
return
}
begin = time.Now()
RunParatype(*procsPtr, *outFilePtr, *printPtr, flist)
end = time.Now()
if *timePtr == true {
fmt.Printf("COMPLETION: %d\n", end.Sub(begin).Nanoseconds())
}
}