-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
206 lines (175 loc) · 5.21 KB
/
cli.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
package cmd
import (
"flag"
"fmt"
"grits/benchmarks"
"grits/parser"
"grits/process"
"grits/webserver"
"log"
"runtime"
"time"
)
/*
Usage of ./grits:
--benchmark
run benchmarks for current program
--sample-benchmarks
run sample benchmarks
--maxcores
sets the maximum number of cores to use while doing the benchmarks (0 = maximum number of available cores)
--execute
execute processes (default true)
--noexecute
do not execute processes (equivalent to -execute=false)
--typecheck
run typechecker (default true)
--notypecheck
skip typechecker (equivalent to -typecheck=false)
--repeat uint
number of repetitions do when benchmarking (default 1)
--verbosity int
verbosity level (1 = least, 3 = most) (default 1)
--webserver
start webserver
--addr string
webserver address (default ":8081")
*/
// Entry point to run via CLI
func Cli() {
// Execution Flags
typecheck := flag.Bool("typecheck", true, "run typechecker")
noTypecheck := flag.Bool("notypecheck", false, "skip typechecker (equivalent to -typecheck=false)")
execute := flag.Bool("execute", true, "execute processes")
noExecute := flag.Bool("noexecute", false, "do not execute processes (equivalent to -execute=false)")
logLevel := flag.Int("verbosity", 1, "verbosity level (1 = least, 3 = most)")
// Execution Flags
syncSemantics := flag.Bool("sync", false, "execute using synchronous version (non-polarized) (default set to --async)")
asyncSemantics := flag.Bool("async", true, "execute using asynchronous version (polarized) (default, refer to --sync for alternative)")
// Benchmarking flags
benchmark := flag.Bool("benchmark", false, "run benchmarks for current program")
benchmarkRepeatCount := flag.Uint("repeat", 1, "number of repetitions do when benchmarking")
maxCores := flag.Int("maxcores", 0, "sets the maximum number of cores to utilise while doing the benchmarks (0 = maximum number of available cores)")
sampleBenchmarks := flag.Bool("sample-benchmarks", false, "run sample benchmarks")
// Webserver
startWebserver := flag.Bool("webserver", false, "start webserver")
// todo: add option to choose which execution to use (synchronous vs asynchronous with polarities)
flag.Parse()
args := flag.Args()
if *maxCores <= 0 || *maxCores > runtime.NumCPU() {
// if maxCores is set beyond the number of available cores, reset it to the max
*maxCores = runtime.NumCPU()
}
if *sampleBenchmarks {
if len(args) >= 1 {
log.Fatal("To run pre-configured benchmarks, do not pass any filenames")
return
}
// Run benchmarks and terminate
benchmarks.SampleBenchmarks(*maxCores)
return
}
if *benchmark {
if len(args) < 1 {
log.Fatal("expected name of file to benchmark")
return
}
benchmarks.BenchmarkFile(args[0], *benchmarkRepeatCount, *maxCores)
return
}
typecheckRes := !*noTypecheck && *typecheck
executeRes := !*noExecute && *execute
if *logLevel < 1 {
*logLevel = 1
} else if *logLevel > 3 {
*logLevel = 3
}
if *logLevel > 1 {
fmt.Printf("Grits -- typecheck: %v, execute: %v, verbosity: %d, webserver: %v, benchmark: %v, ", typecheckRes, executeRes, *logLevel, *startWebserver, *benchmark)
if *syncSemantics {
fmt.Printf("execution version: v1 (sync)\n")
} else if *asyncSemantics {
fmt.Printf("execution version: v2 (async)\n")
}
}
if *startWebserver {
// Run via API
webserver.SetupAPI()
return
}
var processes []*process.Process
var assumedFreeNames []process.Name
var globalEnv *process.GlobalEnvironment
var err error
if len(args) < 1 {
err := fmt.Errorf("expected name of file to be executed (use -h for help)")
log.Fatal(err)
return
}
if len(args) > 1 {
err := fmt.Errorf("found extra arguments: %v", args[1:])
log.Fatal(err)
return
}
processes, assumedFreeNames, globalEnv, err = parser.ParseFile(args[0])
if err != nil {
log.Fatal(err)
return
}
globalEnv.LogLevels = generateLogLevel(*logLevel)
if typecheckRes {
err = process.Typecheck(processes, assumedFreeNames, globalEnv)
if err != nil {
log.Fatal(err)
return
}
}
if executeRes {
// Choose execution version
var executionVersion process.Execution_Version
if *syncSemantics {
executionVersion = process.NON_POLARIZED_SYNC
} else if *asyncSemantics {
executionVersion = process.NORMAL_ASYNC
} else {
fmt.Println("Choose either --sync or --async as the execution version")
return
}
re := &process.RuntimeEnvironment{
GlobalEnvironment: globalEnv,
UseMonitor: false,
Color: true,
ExecutionVersion: executionVersion,
Typechecked: typecheckRes,
Delay: 0 * time.Millisecond,
Quiet: false,
}
process.InitializeProcesses(processes, nil, nil, re)
}
}
// Generate log levels: 1 = least verbose, 3 = most verbose
// todo maybe add level 0 for quiet
func generateLogLevel(logLevel int) []process.LogLevel {
if logLevel < 1 {
logLevel = 1
}
switch logLevel {
case 1:
return []process.LogLevel{
process.LOGINFO,
}
case 2:
return []process.LogLevel{
process.LOGINFO,
process.LOGRULE,
}
default:
return []process.LogLevel{
process.LOGINFO,
process.LOGRULE,
process.LOGPROCESSING,
process.LOGRULEDETAILS,
process.LOGMONITOR,
}
}
}