-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
218 lines (204 loc) · 4.68 KB
/
main.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
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
)
var (
F_DBG bool
Commands instructionset
Labels map[string]int
BuildinInstructionSets map[string]instructionset = map[string]instructionset{
"standard": instructionset{ //Day9 standard
"add": command{
Instruction: 1,
ArgCount: 3,
},
"multi": command{
Instruction: 2,
ArgCount: 3,
},
"input": command{
Instruction: 3,
ArgCount: 1,
},
"output": command{
Instruction: 4,
ArgCount: 1,
},
"jmpit": command{
Instruction: 5,
ArgCount: 2,
},
"jmpif": command{
Instruction: 6,
ArgCount: 2,
},
"less": command{
Instruction: 7,
ArgCount: 3,
},
"equal": command{
Instruction: 8,
ArgCount: 3,
},
"srelbase": command{
Instruction: 9,
ArgCount: 1,
},
},
"standard5": instructionset{ //Day5 standard
"add": command{
Instruction: 1,
ArgCount: 3,
},
"multi": command{
Instruction: 2,
ArgCount: 3,
},
"input": command{
Instruction: 3,
ArgCount: 1,
},
"output": command{
Instruction: 4,
ArgCount: 1,
},
"jmpit": command{
Instruction: 5,
ArgCount: 2,
},
"jmpif": command{
Instruction: 6,
ArgCount: 2,
},
"less": command{
Instruction: 7,
ArgCount: 3,
},
"equal": command{
Instruction: 8,
ArgCount: 3,
},
},
"standard2": instructionset{ //Day2 standard
"add": command{
Instruction: 1,
ArgCount: 3,
},
"multi": command{
Instruction: 2,
ArgCount: 3,
},
},
}
)
type command struct {
Instruction int
ArgCount int
}
type instructionset map[string]command
func main() {
InputFileName := ""
OutputFileName := "out.int"
_ = OutputFileName
Args := os.Args
for i := 1; i < len(Args); i++ {
v := Args[i]
if v[0] == '-' {
flag := v[1:]
switch flag {
case "dbg":
F_DBG = true
case "o":
i++
if len(Args) > i {
OutputFileName = Args[i]
} else {
fmt.Printf("-o needs a filename.\n")
os.Exit(1)
}
default:
CloseMessage(1, "Flag: \"%s\" is not reconized.", flag)
}
} else {
InputFileName = v
}
}
if InputFileName == "" {
CloseMessage(1, "Please enter a Input file.")
}
f, err := os.Open(InputFileName)
if err != nil {
CloseMessage(1, "Could not open input file \"%s\"", InputFileName)
}
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanLines)
lines := make([][]string, 0, 0)
WordPattern := regexp.MustCompile("\\S+")
for scanner.Scan() {
tmp := make([]string, 0)
for _, v := range WordPattern.FindAllString(scanner.Text(), -1) {
tmp = append(tmp, v)
}
lines = append(lines, tmp)
}
f.Close()
loadInstructionset("standard")
Output := make([]*int, 0)
for LineID, _ := range lines {
Output = append(Output, CompileCommand(lines[LineID], Output, LineID)...)
}
}
func CompileCommand(args []string, output []*int, line int) []*int {
out := make([]*int, 0)
for i := 0; i < len(args); i++ {
Type := GetWordType(args[i])
fmt.Print(args[i], ":", Type, ", ")
switch Type {
case T_TEXT:
if _, ok := Commands[args[i]]; ok {
//cmdID := i
cmdArgs := make([]*int, 0)
ArgMode := make([]bool, Commands[args[i]].ArgCount) //true = value, false = pointer/reference
if len(args[i+1:]) < Commands[args[i]].ArgCount {
CloseMessage(1, "Line %d, Word %d: ERROR: command %s needs %d arguments.", line, i, args[i], Commands[args[i]].ArgCount)
}
for i2 := 0; i2 < Commands[args[i]].ArgCount; i2++ {
fmt.Println(Commands[args[i]].ArgCount)
i++
Type := GetWordType(args[i])
fmt.Print(args[i], ";:", Type, ", ")
switch Type {
case T_POINTER:
DebugPrint("Line %d, Word %d: DEBUG: found Pointer", line, i)
ArgMode[i2] = false
addr, err := strconv.Atoi(args[i][1:])
if err != nil {
CloseMessage(1, "Line %d, Word %d: ERROR: Value of pointer needs to be a number.", line, i)
}
cmdArgs = append(cmdArgs, GetSharedIntPointer(addr))
case T_LITTERAL:
DebugPrint("Line %d, Word %d: DEBUG: found Litteral", line, i)
ArgMode[i2] = true
val, err := strconv.Atoi(args[i][1:])
if err != nil {
CloseMessage(1, "Line %d, Word %d: ERROR: Value of litteral needs to be a number.", line, i)
}
cmdArgs = append(cmdArgs, GetSharedIntPointer(val))
}
}
out = append(out, GetSharedIntPointer(int(Commands[args[i]].Instruction)))
} else {
//sCloseMessage(1, "Command \"%s\" not found.", args[i])
}
}
}
fmt.Println()
return out
}
func loadInstructionset(name string) {
Commands = BuildinInstructionSets[name]
}