-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.go
193 lines (152 loc) · 4.04 KB
/
configuration.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
package main
import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
"github.com/DanielNos/neco/errors"
"github.com/DanielNos/neco/logger"
)
type Action byte
const (
A_Build Action = iota
A_Run
A_Analyze
A_BuildAndRun
)
type Configuration struct {
PrintTokens bool
DrawTree bool
PrintInstructions bool
Optimize bool
Silent bool
PrintConstants bool
Action Action
TargetPath string
OutputPath string
}
func processArguments() *Configuration {
args := os.Args[1:]
// No action
if len(args) == 0 {
logger.Fatal(errors.INVALID_FLAGS, "No action specified. Use neco help for more info.")
}
argumentsStart := 2
// Collect target
configuration := &Configuration{Optimize: true}
switch args[0] {
case "build", "run", "analyze":
if len(args) == 1 {
logger.Fatal(errors.INVALID_FLAGS, "No target specified.")
}
configuration.TargetPath = args[1]
switch args[0] {
case "build":
configuration.Action = A_Build
case "run":
configuration.Action = A_Run
case "analyze":
configuration.Action = A_Analyze
}
case "help", "--help", "-h":
printHelp()
os.Exit(0)
default:
if strings.HasSuffix(args[0], ".neco") {
configuration.Action = A_BuildAndRun
logger.LoggingLevel = logger.LL_Warning
} else {
configuration.Action = A_Run
}
configuration.TargetPath = args[0]
argumentsStart = 1
}
// Collect flags
switch configuration.Action {
// Build flags
case A_Build, A_BuildAndRun:
for i := argumentsStart; i < len(args); i++ {
switch args[i] {
case "--tokens", "-to":
configuration.PrintTokens = true
case "--tree", "-tr":
configuration.DrawTree = true
case "--instructions", "-i":
configuration.PrintInstructions = true
case "--dont-optimize", "-d":
configuration.Optimize = false
case "--silent", "-s":
logger.LoggingLevel = logger.LL_Error
case "--no-log", "-n":
logger.LoggingLevel = logger.LL_NoLog
case "--log-level", "-l":
if i+1 == len(args) {
logger.Fatal(errors.INVALID_FLAGS, "No logging level provided after "+args[i]+" flag.")
}
i++
// Try to get logging level from name
level, isName := logger.StringToLogLevel[args[i]]
if isName {
logger.LoggingLevel = level
continue
}
// Get logging level from number
loggingLevel, err := strconv.Atoi(args[i])
if err != nil {
logger.Fatal(errors.INVALID_FLAGS, "Logging level has to be a number.")
}
if loggingLevel < 0 || loggingLevel > 5 {
logger.Fatal(errors.INVALID_FLAGS, "Invalid logging level "+fmt.Sprintf("%d.", loggingLevel))
}
logger.LoggingLevel = byte(loggingLevel)
case "--out", "-o":
if i+1 == len(args) {
logger.Fatal(errors.INVALID_FLAGS, "No output path provided after "+args[i]+" flag.")
}
i++
configuration.OutputPath = args[i]
case "--constants", "-c":
configuration.PrintConstants = true
default:
logger.Fatal(errors.INVALID_FLAGS, "Invalid flag \""+args[i]+"\" for action build.")
}
}
// Run flags
case A_Run:
for _, flag := range args[argumentsStart:] {
switch flag {
default:
logger.Fatal(errors.INVALID_FLAGS, "Invalid flag \""+flag+"\" for action run.")
}
}
// Analyze flags
case A_Analyze:
for _, flag := range args[2:] {
switch flag {
case "--tokens", "-to":
configuration.PrintTokens = true
case "--tree", "-tr":
configuration.DrawTree = true
case "--dont-optimize", "-d":
configuration.Optimize = false
default:
logger.Fatal(errors.INVALID_FLAGS, "Invalid flag \""+flag+"\" for action analyze.")
}
}
}
// Set output binary path
if configuration.OutputPath == "" {
if strings.HasSuffix(configuration.TargetPath, ".neco") && len(configuration.TargetPath) > 5 {
configuration.OutputPath = configuration.TargetPath[:len(configuration.TargetPath)-5]
if runtime.GOOS == "windows" {
configuration.OutputPath += ".nc"
}
} else if runtime.GOOS == "windows" {
configuration.OutputPath += ".nc"
} else {
configuration.OutputPath += "_bin"
}
}
return configuration
}