-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
465 lines (425 loc) · 13.2 KB
/
Copy pathmain.go
File metadata and controls
465 lines (425 loc) · 13.2 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package main
import (
"fmt"
"strings"
"os"
"os/exec"
)
const triops_help_message = `A lower level assembly macro programming language
Usage:
$ triops <file.trs> [options] # Compile 'file.trs' to 'file'
$ triops <file.trs> <output> [options] # Compile 'file.trs' to 'ouput'
$ triops <file.trs> <output.nasm> [options] # Transpile 'file.trs' to 'ouput.nasm', doesn't generate an executable
Options:
-small # Package the executable to be small
-run # Run the executable after compiling
-check # Only check, don't try to build
`
func main() {
if len(os.Args) < 2 {
fmt.Print(triops_help_message)
os.Exit(1)
}
make_small_exe := false
generate_executable := true
output_provided := false
run_executable := false
only_check := false
input_filename := os.Args[1]
output_filename := input_filename
if len(os.Args) > 2 {
for _, arg := range os.Args[2:] {
if arg == "-small" {
make_small_exe = true
continue
} else if arg == "-run" {
run_executable = true
continue
} else if arg == "-check" {
only_check = true
continue
}
if output_provided {
fmt.Printf("Triops: An output file (`%v`) was already provided\n", output_filename)
fmt.Print(triops_help_message)
os.Exit(1)
}
output_filename = arg
output_provided = true
}
l := len(output_filename)
if l > 5 && output_filename[l - 5:] == ".nasm" {
generate_executable = false
}
}
if !output_provided {
clippage := len(input_filename)-1;
for ; clippage > 0; clippage -= 1 {
if output_filename[clippage] == '.' {
break
}
}
if clippage >= 1 {
output_filename = output_filename[:clippage]
}
//output_filename = fmt.Sprintf("%v.nasm", output_filename)
}
file, err := os.ReadFile(input_filename)
if err != nil {
fmt.Printf("Triops: Couldn't read file `%v`\n", input_filename)
fmt.Print(triops_help_message)
os.Exit(1)
}
full_text := string(file)
if len(full_text) == 0 {
fmt.Println("Triops: Empty file, expected at least `entry`")
os.Exit(1)
}
tokens := make([]Token, 0)
var token Token
for i := 0; i < len(full_text); i += 1 {
char := rune(full_text[i])
// fmt.Printf("%c", (char))
/* this skips text */
if is_white(char) {
if token.len != 0 {
token.tag = keywords[token_txt_str(token, full_text)]
tokens = append(tokens, token)
token = Token{0, 0, 0}
}
continue
}
if char == '#' && full_text[i+1] == '*' {
depth := 0
for ; i < len(full_text); i += 1 {
if full_text[i] == '#' && full_text[i+1] == '*' {
depth += 1; i += 1; continue
}
if full_text[i] == '*' && full_text[i+1] == '#' {
depth -= 1; i += 1;
if depth == 0 { break }
}
}
continue
}
if char == '#' && full_text[i+1] == '#' {
for ; i < len(full_text); i += 1 {
if full_text[i] == '\n' { break }
}
continue
}
/* time for real tokens */
if token.len == 0 {token.pos = uint32(i)}
token.len += 1
/* special cases */
if char == '#' && is_alphabetic(rune(full_text[i+1])) { continue }
if lang_operator(char) {
if token.len > 1 { token.len -= 1; i -= 1 }
token.tag = keywords[token_txt_str(token, full_text)] /* TODO: anytime keywords[] is checked, also check for # if it isn't a keyword, then error */
tokens = append(tokens, token)
token = Token{0, 0, 0}
continue
}
if token.len == 1 && is_digit(char) {/* TODO: this basically only really handles integers */
var is_float bool
for is_digit(rune(full_text[i])) {
token.len += 1; i += 1
if token.len == 2 && full_text[i] == 'x' { token.len += 1; i += 1 }
if full_text[i] == '.' { is_float = true; token.len += 1; i += 1 }
}
token.len -= 1; i -= 1
if is_float { token.tag = VALUE_FLOAT
} else { token.tag = VALUE_INTEGER }
tokens = append(tokens, token)
token = Token{0, 0, 0}
continue
}
if stumble(char) {
if token.len == 1{/* the same stumble character repeated will count as 1 token */
for full_text[i] == byte(char) { token.len += 1; i += 1 }
}
token.len -= 1; i -= 1
token.tag = keywords[token_txt_str(token, full_text)]
tokens = append(tokens, token)
token = Token{0, 0, 0}
continue
}
if char == '"' {
if token.len == 1 {
token.len += 1; i += 1
var prev_char rune
for {
if full_text[i] == '"' && prev_char != '\\' { break }
if full_text[i] == '\\' && prev_char == '\\' { prev_char = ' '
} else { prev_char = rune(full_text[i]) }
token.len += 1; i += 1
}
token.tag = VALUE_STRING
} else {
token.tag = keywords[token_txt_str(token, full_text)]
}
tokens = append(tokens, token)
token = Token{0, 0, 0}
continue
}
}
if len(tokens) == 0 {
fmt.Println("There is no code in this file (only comments), expected at least `entry`")
os.Exit(1)
}
var global_scope Scope
set := Token_Set {
index : 0,
text : full_text,
tokens : tokens,
}
// ggscope = &global_scope
error_count := 0
for ; !set.end ; inc(&set) {
token := set.tokens[set.index]
// fmt.Println(token_txt_str(token, full_text))
switch token.tag {
case KEYWORD_TYPE:
inc(&set)
if !parse_type_decl(&set, &global_scope) { error_count += 1 }
case KEYWORD_ENUM:
inc(&set)
if !parse_enum_decl(&set, &global_scope) { error_count += 1 }
case KEYWORD_SEMICOLON:
case KEYWORD_REGISTER, NONE:
old_index := set.index
if !parse_variable_decl(&set, &global_scope, SPEC_NONE) { error_count += 1 }
if set.index == old_index {
print_error_line(&set, "Unknown type for global declaration")
error_count += 1
skip_statement(&set)
// print_error_line("Runtime expressions are not allowed in the global scope (`entry` would be the place for that)", tokens[i], &global_scope)
}
case KEYWORD_OPEN_PAREN:
inc(&set)
error_count += parse_proc_decl(&set, &global_scope)
case KEYWORD_ASM: fallthrough
case KEYWORD_ENTRY:
inc(&set)
error_count += parse_asm(&set, &global_scope)
for _, lbl_token := range global_scope.label_uses {
this := where_is(&global_scope, token_txt_str(lbl_token, set.text))
if this.named_thing != NAME_LABEL {
print_error_line_token_txt(lbl_token, set.text, "Undefined name")
error_count += 1
}
}
default:
print_error_line(&set, "Unexpected toplevel token")
skip_statement(&set)
error_count += 1
}
}
/* fmt.Println()
for _, typ := range bare_types { fmt.Println(typ) }
for _, typ := range indirect_types { fmt.Println(typ) }
fmt.Println()/*
for name, enum := range global_scope.enums { fmt.Println(name, enum) }
for name, value := range enum_values { fmt.Println(name, value) }
fmt.Println()/*
for name, decl := range global_scope.decls { fmt.Println(name, decl) }/*
fmt.Println(all_procs)/*
fmt.Println(all_values)
*/
if error_count != 0 {
fmt.Printf("Amount of errors: %v\n", error_count)
os.Exit(1)
}
if only_check {
return
}
full_asm, _ := generate_assembly(&global_scope, &set, "entry", make_small_exe)
nasm_filename := output_filename
if generate_executable { nasm_filename = fmt.Sprintf("%v.nasm", nasm_filename) }
err = os.WriteFile(nasm_filename, []byte(full_asm), 0666)
if err != nil {
fmt.Printf("Triops: Could not write `%v` to disk:\n", nasm_filename)
fmt.Println(err)
os.Exit(1)
}
/* TODO: properly catch the outputs of these programs cus it does not appear to be happening rn */
var program_output strings.Builder
if generate_executable && make_small_exe {
cmd := exec.Command("nasm", "-f", "bin", "-o", output_filename, nasm_filename)
cmd.Stdout = &program_output
fmt.Print(program_output.String())
err := cmd.Run()
if err != nil {
fmt.Printf("Triops: nasm error `%v`:\n", err)
os.Exit(1)
}
os.Remove(nasm_filename)
cmd = exec.Command("chmod", "+x", output_filename)
fmt.Print(program_output.String())
cmd.Stdout = &program_output
err = cmd.Run()
if err != nil {
fmt.Printf("Triops: chmod error `%v`:\n", err)
os.Remove(output_filename)
os.Exit(1)
}
}
if generate_executable && !make_small_exe {
object_filename := fmt.Sprintf("%v.o", output_filename)
cmd := exec.Command("nasm", "-f", "elf64", "-o", object_filename, nasm_filename)
cmd.Stdout = &program_output
err := cmd.Run()
fmt.Print(program_output.String())
if err != nil {
fmt.Printf("Triops: nasm error `%v`:\n", err)
os.Exit(1)
}
os.Remove(nasm_filename)
cmd = exec.Command("ld", "-o", output_filename, object_filename)
cmd.Stdout = &program_output
err = cmd.Run()
fmt.Print(program_output.String())
os.Remove(object_filename)
if err != nil {
fmt.Printf("Triops: ld error `%v`:\n", err)
os.Exit(1)
}
}
if generate_executable && run_executable {
cmd := exec.Command(fmt.Sprintf("./%v", output_filename))
cmd.Stdout = &program_output
cmd.Run()
fmt.Print(program_output.String())
}
}
/* TODO: one-time explainers about specific errors */
func print_error_line(set *Token_Set, message string, args ...any) {
print_error_line_token_txt(curr(set), set.text, message, args...)
}
func print_error_line_token_txt(token Token, text string, message string, args ...any) {
var full_line Token
/* line_nr and start of the line */
line_nr := 1
for i := token.pos; i < token.pos + 1; i -= 1 {
char := text[i]
if line_nr == 1 && char == '\n' { full_line.pos = i + 1 }
if char == '\n' { line_nr += 1 }
}
/* end of the line */
for i := full_line.pos; i < uint32(len(text)); i += 1 {
full_line.len += 1
if text[i] == '\n' { break }
}
/* main print */
full_line_str := token_txt_str(full_line, text)
fmt.Printf("%s:\n", fmt.Sprintf(message, args...))
chars_written, _ := fmt.Printf("%d | ", line_nr)
fmt.Printf("%s", full_line_str)
if full_line_str[len(full_line_str)-1] != '\n' { fmt.Println() }
/* the c a r e t s */
for i := 0; i < chars_written; i += 1 { fmt.Print(" ") }
for i := uint32(0); i < token.pos - full_line.pos; i += 1 {
if is_white(rune(full_line_str[i])) { fmt.Printf("%c", rune(full_line_str[i]))
} else { fmt.Print(" ") }
}
for i := uint16(0); i < token.len; i += 1 { fmt.Print("^") }
fmt.Println()
}
func is_white(char rune) bool {
switch char {
case ' ', '\t', '\n', '\r', '\v', '\f': return true
default: return false
}
}
func stumble(char rune) bool {
switch char {
case '+', '-', '/', '\\', '*',
'%', '$', '@', '!', '|',
'~', '>', '<': return true
default: return false
}
}
func lang_operator(char rune) bool {
switch char {
case ';', ',', '^', '[', ']',
'(', ')', '{', '}', '=',
'\'', '#', '.': return true
default: return false
}
}
func is_digit(char rune) bool {
return char >= '0' && char <= '9'
}
func is_alphabetic(char rune) bool {
return (char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z') || (char == '_')
}
func prev(set *Token_Set) Token {
return set.tokens[set.index-1]
}
func curr(set *Token_Set) Token {
return set.tokens[set.index]
}
func inc(set *Token_Set) *Token_Set {
if set.index == len(set.tokens) - 1 { set.end = true; return set }
set.index += 1
if t := set.tokens[set.index].tag; t == KEYWORD_OPEN_BRACE { set.braces += 1
} else if t == KEYWORD_CLOSE_BRACE { set.braces -= 1 }
// fmt.Print("\"", token_str(set), "\"i", set.braces, ", ")
return set
}
func dec(set *Token_Set) *Token_Set {
if t := set.tokens[set.index].tag; t == KEYWORD_OPEN_BRACE { set.braces -= 1
} else if t == KEYWORD_CLOSE_BRACE { set.braces += 1 }
set.index = max(set.index - 1, 0)
// fmt.Print("\"", token_str(set), "\"d", set.braces, ", ")
return set
}
func append_node(node Node) int {
all_nodes = append(all_nodes, node)
return len(all_nodes)-1
}
func add_link(scope *Scope, link Link) {
scope.code = append(scope.code, link)
}
func where_is(scope *Scope, name string) What {
for _, what := range scope.names {
if what.name == name {
return what
}
}
prev_scope := scope.prev_scope
for prev_scope != nil {
/* TODO: Global variable management */
for _, what := range prev_scope.names {
if what.named_thing != NAME_DECL && what.named_thing != NAME_LABEL && what.name == name {
return what
}
}
prev_scope = prev_scope.prev_scope
}
return What{"", 0, NAME_NOT_HERE, SPEC_NONE}
}
func add_type_to_scope(scope *Scope, name string, ti Type_Index) {
all_types = append(all_types, ti)
scope.names = append(scope.names, What{name, len(all_types)-1, NAME_TYPE, SPEC_NONE})
}
func add_enum_to_scope(scope *Scope, name string, enum_des Enum_Des) {
all_enums = append(all_enums, enum_des)
scope.names = append(scope.names, What{name, len(all_enums)-1, NAME_ENUM, SPEC_NONE})
}
func add_decl_to_scope(scope *Scope, name string, decl_des Decl_Des, specialty Decl_Specialty) {
all_decls = append(all_decls, decl_des)
scope.names = append(scope.names, What{name, len(all_decls)-1, NAME_DECL, specialty})
}
func add_reg_to_scope(scope *Scope, name string, register_des Reg_Des, specialty Decl_Specialty) {
all_registers = append(all_registers, register_des)
scope.names = append(scope.names, What{name, len(all_registers)-1, NAME_REG, specialty})
}
func add_label_to_scope(scope *Scope, name string, place int) {
all_labels = append(all_labels, place)
scope.names = append(scope.names, What{name, len(all_labels)-1, NAME_LABEL, SPEC_NONE})
}
func add_proc_to_scope(scope *Scope, name string, proc Scope) {
all_procs = append(all_procs, proc)
scope.names = append(scope.names, What{name, len(all_procs)-1, NAME_PROC, SPEC_NONE})
}