-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
866 lines (769 loc) · 26.2 KB
/
main.swift
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
import Foundation
class Assembler {
private static let header = """
; constantes
SYS_EXIT equ 1
SYS_READ equ 3
SYS_WRITE equ 4
STDIN equ 0
STDOUT equ 1
True equ 1
False equ 0
segment .data
formatin: db "%d", 0
formatout: db "%d", 10, 0 ; newline, nul terminator
scanint: times 4 db 0 ; 32-bits integer = 4 bytes
segment .bss ; variaveis
res RESB 1
section .text
global main ; linux
;global _main ; windows
extern scanf ; linux
extern printf ; linux
;extern _scanf ; windows
;extern _printf; windows
extern fflush ; linux
;extern _fflush ; windows
extern stdout ; linux
;extern _stdout ; windows
; subrotinas if/while
binop_je:
JE binop_true
JMP binop_false
binop_jg:
JG binop_true
JMP binop_false
binop_jl:
JL binop_true
JMP binop_false
binop_false:
MOV EAX, False
JMP binop_exit
binop_true:
MOV EAX, True
binop_exit:
RET
main:
PUSH EBP ; guarda o base pointer
MOV EBP, ESP ; estabelece um novo base pointer
"""
private static let footer = """
; interrupcao de saida (default)
PUSH DWORD [stdout]
CALL fflush
ADD ESP, 4
MOV ESP, EBP
POP EBP
MOV EAX, 1
XOR EBX, EBX
INT 0x80
"""
private static var code: String = ""
static func addInstruction(_ instruction: String) {
code += instruction + "\n"
}
static func generate(filename: String) {
let content = header + "\n" + code + "\n" + footer
do {
try content.write(toFile: filename, atomically: true, encoding: .utf8)
} catch {
writeStderrAndExit("Failed to write file")
}
}
}
class PrePro {
static public func filter(code: String) -> String {
let splittedCode = code.replacingOccurrences(of: "\t", with: "").split(separator: "\n")
var processedCode = ""
for i in 0..<splittedCode.count {
let line = splittedCode[i]
if line.contains("--") {
if line.prefix(2) != "--" {
processedCode += String(line.split(separator: "--")[0]) + "\n"
}
} else {
processedCode += String(line) + "\n"
}
}
return processedCode
}
}
class SymbolTable {
private var table: [String: Any?] = [:]
private var variables: [String] = []
func initVar(_ variableName: String) {
if table.keys.contains(variableName) {
writeStderrAndExit("Variable already initialized: \(variableName)")
}
table[variableName] = nil as Any?
variables.append(variableName)
}
func setValue(_ variableName: String, _ variableValue: Any) {
if !table.keys.contains(variableName) {
writeStderrAndExit("Variable not initialized: \(variableName)")
}
table[variableName] = variableValue
}
func getValue(_ variableName: String) -> Any {
if !table.keys.contains(variableName) {
writeStderrAndExit("Variable not initialized: \(variableName)")
return 0
} else if let variableValue = table[variableName] {
return variableValue as Any
}
writeStderrAndExit("Variable \(variableName) is initialized, but has no value assigned")
return 0
}
func getOffset(for key: String) -> Int {
guard let index = variables.firstIndex(of: key) else {
writeStderrAndExit("Variable not declared!")
return 0
}
return (index+1) * 4
}
}
protocol Node {
var value: String { get set }
var children: [Node] { get set }
func evaluate(symbolTable: SymbolTable) -> Any
}
class Block: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
for node in self.children {
let _ = node.evaluate(symbolTable: symbolTable)
}
return 0
}
}
class BinOp: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
let rightValue = self.children[1].evaluate(symbolTable: symbolTable)
Assembler.addInstruction("PUSH EAX")
let leftValue = self.children[0].evaluate(symbolTable: symbolTable)
Assembler.addInstruction("POP EBX")
if ["EQ", "GT", "LT"].contains(self.value) {
Assembler.addInstruction("CMP EAX, EBX")
if self.value == "EQ" {
Assembler.addInstruction("CALL binop_je")
} else if self.value == "GT" {
Assembler.addInstruction("CALL binop_jg")
} else if self.value == "LT" {
Assembler.addInstruction("CALL binop_jl")
}
} else if ["PLUS", "MINUS", "MUL", "DIV", "AND", "OR"].contains(self.value) {
if self.value == "PLUS" {
Assembler.addInstruction("ADD EAX, EBX")
} else if self.value == "MINUS" {
Assembler.addInstruction("SUB EAX, EBX")
} else if self.value == "MUL" {
Assembler.addInstruction("IMUL EBX")
} else if self.value == "DIV" {
Assembler.addInstruction("CDQ")
Assembler.addInstruction("IDIV EBX")
} else if self.value == "AND" {
Assembler.addInstruction("AND EAX, EBX")
} else if self.value == "OR" {
Assembler.addInstruction("OR EAX, EBX")
}
}
if let firstInt = leftValue as? Int, let secondInt = rightValue as? Int {
if self.value == "PLUS" { return firstInt + secondInt }
if self.value == "MINUS" { return firstInt - secondInt }
if self.value == "MUL" { return firstInt * secondInt }
if self.value == "DIV" { return firstInt / secondInt }
if self.value == "GT" { return firstInt > secondInt ? 1 : 0 }
if self.value == "LT" { return firstInt < secondInt ? 1 : 0 }
if self.value == "EQ" { return firstInt == secondInt ? 1 : 0 }
if self.value == "AND" { return firstInt == 1 && secondInt == 1 ? 1 : 0 }
if self.value == "OR" { return firstInt == 1 || secondInt == 1 ? 1 : 0 }
if self.value == "CONCAT" { return String(firstInt) + String(secondInt) }
} else if let firstString = leftValue as? String, let secondString = rightValue as? String {
if self.value == "GT" { return firstString > secondString ? 1 : 0 }
if self.value == "LT" { return firstString < secondString ? 1 : 0 }
if self.value == "EQ" { return firstString == secondString ? 1 : 0 }
if self.value == "CONCAT" { return firstString + secondString }
} else if let firstInt = leftValue as? Int, let secondString = rightValue as? String {
if self.value == "CONCAT" { return String(firstInt) + secondString }
} else if let firstString = leftValue as? String, let secondInt = rightValue as? Int {
if self.value == "CONCAT" { return firstString + String(secondInt) }
}
writeStderrAndExit("Unsupported types for comparison: \(type(of: leftValue)) and \(type(of: rightValue))")
return 0
}
}
class UnOp: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
let result = self.children[0].evaluate(symbolTable: symbolTable) as! Int
if self.value == "NOT" {
Assembler.addInstruction("NOT EAX")
return (result == 0) ? 1 : 0
} else if self.value == "MINUS" {
Assembler.addInstruction("NEG EAX")
return -result
} else if self.value == "PLUS" {
return result
}
writeStderrAndExit("Unsupported unary operation on integers")
return 0
}
}
class IntVal: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
if let intValue = Int(self.value) {
Assembler.addInstruction("MOV EAX, \(intValue)")
return intValue
}
writeStderrAndExit("Invalid integer value")
return 0
}
}
class StringVal: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
return String(self.value)
}
}
class NoOp: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
return 0
}
}
class VarDec: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
Assembler.addInstruction("PUSH DWORD 0")
symbolTable.initVar(self.value)
if self.children.count == 1 {
let variableValue = self.children[0].evaluate(symbolTable: symbolTable)
let offset = symbolTable.getOffset(for: self.value)
Assembler.addInstruction("MOV [EBP-\(offset)], EAX")
symbolTable.setValue(self.value, variableValue)
}
return 0
}
}
class VarAssign: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
let variableValue = self.children[0].evaluate(symbolTable: symbolTable)
let offset = symbolTable.getOffset(for: self.value)
Assembler.addInstruction("MOV [EBP-\(offset)], EAX")
symbolTable.setValue(self.value, variableValue)
return 0
}
}
class VarAccess: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
let offset = symbolTable.getOffset(for: self.value)
Assembler.addInstruction("MOV EAX, [EBP-\(offset)]")
return symbolTable.getValue(self.value)
}
}
class Statements: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
for node in self.children {
let _ = node.evaluate(symbolTable: symbolTable)
}
return 0
}
}
class WhileOp: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
let idWhile = generateUniqueIdentifier(length: 10)
Assembler.addInstruction("while_\(idWhile):")
// Evaluate the condition
_ = self.children[0].evaluate(symbolTable: symbolTable) as! Int
Assembler.addInstruction("CMP EAX, False")
Assembler.addInstruction("JE while_end_\(idWhile)")
// Evaluate the statements
_ = self.children[1].evaluate(symbolTable: symbolTable)
Assembler.addInstruction("JMP while_\(idWhile)")
Assembler.addInstruction("while_end_\(idWhile):")
return 0
}
}
class IfOp: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
let idIf = generateUniqueIdentifier(length: 10)
Assembler.addInstruction("if_\(idIf):")
// Evaluate the condition
_ = self.children[0].evaluate(symbolTable: symbolTable) as! Int
Assembler.addInstruction("CMP EAX, False")
Assembler.addInstruction("JE if_else_\(idIf)")
// Evaluate the if statements
_ = self.children[1].evaluate(symbolTable: symbolTable)
Assembler.addInstruction("JMP if_end_\(idIf)")
Assembler.addInstruction("if_else_\(idIf):")
// Evaluate the else statements
_ = self.children[2].evaluate(symbolTable: symbolTable)
Assembler.addInstruction("if_end_\(idIf):")
return 0
}
}
class ReadOp: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
let readValue = readLine()
if let intValue = Int(readValue!) {
Assembler.addInstruction("PUSH scanint")
Assembler.addInstruction("PUSH formatin")
Assembler.addInstruction("call scanf")
Assembler.addInstruction("ADD ESP, 8")
Assembler.addInstruction("MOV EAX, DWORD [scanint]")
return intValue as Any
}
writeStderrAndExit("Invalid integer value read from input")
return 0
}
}
class PrintOp: Node {
var value: String
var children: [Node]
init(value: String, children: [Node]) {
self.value = value
self.children = children
}
func evaluate(symbolTable: SymbolTable) -> Any {
let printValue = self.children[0].evaluate(symbolTable: symbolTable)
Assembler.addInstruction("PUSH EAX")
Assembler.addInstruction("PUSH formatout")
Assembler.addInstruction("CALL printf")
Assembler.addInstruction("ADD ESP, 8")
if let printInt = printValue as? Int {
print(printInt)
} else if let printString = printValue as? String {
print(printString)
} else {
writeStderrAndExit("Unsupported type for print operation")
}
return 0
}
}
class Token {
var type: String
var value: String
init(type: String, value: String) {
self.type = type
self.value = value
}
}
class Tokenizer {
var source: String
var position: Int
var next: Token
init(source: String) {
self.source = source
self.position = 0
self.next = Token(type: "", value: "0")
}
func selectNext() -> Void {
if position < source.count {
var char = source[source.index(source.startIndex, offsetBy: position)]
var tokenWord = ""
while char == " " && position < source.count {
position += 1
char = source[source.index(source.startIndex, offsetBy: position)]
}
if char == "+" {
self.next = Token(type: "PLUS", value: "0")
} else if char == "-" {
self.next = Token(type: "MINUS", value: "0")
} else if char == "*" {
self.next = Token(type: "MUL", value: "0")
} else if char == "/" {
self.next = Token(type: "DIV", value: "0")
} else if char == "(" {
self.next = Token(type: "LPAREN", value: "0")
} else if char == ")" {
self.next = Token(type: "RPAREN", value: "0")
} else if char == ">" {
self.next = Token(type: "GT", value: "0")
} else if char == "<" {
self.next = Token(type: "LT", value: "0")
} else if char == "=" {
if source[source.index(source.startIndex, offsetBy: position+1)] == "=" {
self.next = Token(type: "EQ", value: "0")
position += 1
} else {
self.next = Token(type: "ASSIGN", value: "0")
}
} else if char == "." {
if source[source.index(source.startIndex, offsetBy: position+1)] == "." {
self.next = Token(type: "CONCAT", value: "0")
position += 1
} else {
writeStderrAndExit("Invalid character \(tokenWord)")
}
} else if char == "\n" {
self.next = Token(type: "EOL", value: "0")
} else if char == "\"" {
position += 1
while position < source.count {
let nextChar = source[source.index(source.startIndex, offsetBy: position)]
if nextChar == "\"" {
break
} else if nextChar == "\n"{
writeStderrAndExit("Forgot to close string with \"")
} else {
tokenWord += String(nextChar)
}
position += 1
}
if source[source.index(source.startIndex, offsetBy: position)] != "\"" {
writeStderrAndExit("Forgot to close string with \"")
}
self.next = Token(type: "STRING", value: tokenWord)
} else if char.isNumber {
while position < source.count {
let nextChar = source[source.index(source.startIndex, offsetBy: position)]
if nextChar.isNumber {
tokenWord += String(nextChar)
} else {
break
}
position += 1
}
position -= 1
self.next = Token(type: "NUMBER", value: tokenWord)
} else if char.isLetter {
while position < source.count {
let nextChar = source[source.index(source.startIndex, offsetBy: position)]
if nextChar.isLetter || nextChar.isNumber || nextChar == "_" {
tokenWord += String(nextChar)
} else {
break
}
position += 1
}
position -= 1
if ["print", "if", "else", "while", "do", "then", "end", "and", "or", "not", "read", "local"].contains(tokenWord) {
self.next = Token(type: tokenWord.uppercased(), value: "0")
} else {
self.next = Token(type: "IDENTIFIER", value: tokenWord)
}
} else {
writeStderrAndExit("Invalid character \(tokenWord)")
}
position += 1
} else {
self.next = Token(type: "EOF", value: "0")
}
}
}
class Parser {
var tokenizer: Tokenizer
init() {
self.tokenizer = Tokenizer(source: "")
}
private func parseFactor(symbolTable: SymbolTable) -> Node {
if tokenizer.next.type == "NUMBER" {
let factorValue = tokenizer.next.value
tokenizer.selectNext()
return IntVal(value: factorValue, children: [])
} else if tokenizer.next.type == "STRING" {
let factorValue = tokenizer.next.value
tokenizer.selectNext()
return StringVal(value: factorValue, children: [])
} else if tokenizer.next.type == "IDENTIFIER" {
let name = tokenizer.next.value
tokenizer.selectNext()
return VarAccess(value: name, children: [])
} else if tokenizer.next.type == "PLUS" || tokenizer.next.type == "MINUS" || tokenizer.next.type == "NOT" {
let operatorType = tokenizer.next.type
tokenizer.selectNext()
return UnOp(value: operatorType, children: [parseFactor(symbolTable: symbolTable)])
} else if tokenizer.next.type == "LPAREN" {
tokenizer.selectNext()
let result = parseBoolExpression(symbolTable: symbolTable)
if tokenizer.next.type != "RPAREN" {
writeStderrAndExit("Missing closing parenthesis")
}
tokenizer.selectNext()
return result
} else if tokenizer.next.type == "READ" {
tokenizer.selectNext()
if tokenizer.next.type != "LPAREN" {
writeStderrAndExit("Missing opening parenthesis for read statement")
}
tokenizer.selectNext()
if tokenizer.next.type != "RPAREN" {
writeStderrAndExit("Missing closing parenthesis for read statement")
}
tokenizer.selectNext()
return ReadOp(value: "READ", children: [])
} else {
writeStderrAndExit("Invalid factor: (\(tokenizer.next.type), \(tokenizer.next.value))")
}
return NoOp(value: "", children: [])
}
private func parseTerm(symbolTable: SymbolTable) -> Node {
var result = parseFactor(symbolTable: symbolTable)
while tokenizer.next.type == "MUL" || tokenizer.next.type == "DIV" {
let operatorType = tokenizer.next.type
tokenizer.selectNext()
result = BinOp(value: operatorType, children: [result, parseFactor(symbolTable: symbolTable)])
}
return result
}
private func parseExpression(symbolTable: SymbolTable) -> Node {
var result = parseTerm(symbolTable: symbolTable)
while tokenizer.next.type == "PLUS" || tokenizer.next.type == "MINUS" || tokenizer.next.type == "CONCAT" {
let operatorType = tokenizer.next.type
tokenizer.selectNext()
result = BinOp(value: operatorType, children: [result, parseTerm(symbolTable: symbolTable)])
}
return result
}
private func parseRelationalExpression(symbolTable: SymbolTable) -> Node {
var result = parseExpression(symbolTable: symbolTable)
while tokenizer.next.type == "GT" || tokenizer.next.type == "LT" || tokenizer.next.type == "EQ" {
let operatorType = tokenizer.next.type
tokenizer.selectNext()
result = BinOp(value: operatorType, children: [result, parseExpression(symbolTable: symbolTable)])
}
return result
}
private func parseBooleanTerm(symbolTable: SymbolTable) -> Node {
var result = parseRelationalExpression(symbolTable: symbolTable)
while tokenizer.next.type == "AND" {
let operatorType = tokenizer.next.type
tokenizer.selectNext()
result = BinOp(value: operatorType, children: [result, parseRelationalExpression(symbolTable: symbolTable)])
}
return result
}
private func parseBoolExpression(symbolTable: SymbolTable) -> Node {
var result = parseBooleanTerm(symbolTable: symbolTable)
while tokenizer.next.type == "OR" {
let operatorType = tokenizer.next.type
tokenizer.selectNext()
result = BinOp(value: operatorType, children: [result, parseBooleanTerm(symbolTable: symbolTable)])
}
return result
}
private func parseStatement(symbolTable: SymbolTable) -> Node {
if tokenizer.next.type == "EOL" {
tokenizer.selectNext()
return NoOp(value: "", children: [])
} else if tokenizer.next.type == "IDENTIFIER" {
let name = tokenizer.next.value
tokenizer.selectNext()
if tokenizer.next.type == "ASSIGN" {
tokenizer.selectNext()
let expression = parseBoolExpression(symbolTable: symbolTable)
return VarAssign(value: name, children: [expression])
} else {
writeStderrAndExit("Invalid statement")
}
} else if tokenizer.next.type == "PRINT" {
tokenizer.selectNext()
if tokenizer.next.type != "LPAREN" {
writeStderrAndExit("Missing opening parenthesis for print statement")
}
tokenizer.selectNext()
let expression = parseBoolExpression(symbolTable: symbolTable)
if tokenizer.next.type != "RPAREN" {
writeStderrAndExit("Missing closing parenthesis for print statement")
}
tokenizer.selectNext()
return PrintOp(value: "PRINT", children: [expression])
} else if tokenizer.next.type == "WHILE" {
tokenizer.selectNext()
let condition = parseBoolExpression(symbolTable: symbolTable)
if tokenizer.next.type != "DO" {
writeStderrAndExit("Missing DO after WHILE condition")
}
tokenizer.selectNext()
if tokenizer.next.type != "EOL" {
writeStderrAndExit("Missing EOL after DO")
}
tokenizer.selectNext()
var statements: [Node] = []
while tokenizer.next.type != "END" {
let statement = parseStatement(symbolTable: symbolTable)
statements.append(statement)
}
tokenizer.selectNext()
if tokenizer.next.type != "EOL" {
writeStderrAndExit("Missing EOL after END")
}
return WhileOp(value: "WHILE", children: [condition, Statements(value: "", children: statements)])
} else if tokenizer.next.type == "IF" {
tokenizer.selectNext()
let condition = parseBoolExpression(symbolTable: symbolTable)
if tokenizer.next.type != "THEN" {
writeStderrAndExit("Missing THEN after IF condition")
}
tokenizer.selectNext()
if tokenizer.next.type != "EOL" {
writeStderrAndExit("Missing EOL after THEN")
}
tokenizer.selectNext()
var ifStatements: [Node] = []
while tokenizer.next.type != "END" && tokenizer.next.type != "ELSE" {
let statement = parseStatement(symbolTable: symbolTable)
ifStatements.append(statement)
}
var elseStatements: [Node] = []
if tokenizer.next.type == "ELSE" {
tokenizer.selectNext()
while tokenizer.next.type != "END" {
let statement = parseStatement(symbolTable: symbolTable)
elseStatements.append(statement)
}
}
tokenizer.selectNext()
if tokenizer.next.type != "EOL" {
writeStderrAndExit("Missing EOL after END")
}
return IfOp(value: "IF", children: [condition, Statements(value: "", children: ifStatements), Statements(value: "", children: elseStatements)])
} else if tokenizer.next.type == "LOCAL" {
tokenizer.selectNext()
if tokenizer.next.type != "IDENTIFIER" {
writeStderrAndExit("Invalid variable name in declaration")
}
let variableName = tokenizer.next.value
tokenizer.selectNext()
if tokenizer.next.type == "EOL" {
return VarDec(value: variableName, children: [])
} else if tokenizer.next.type == "ASSIGN" {
tokenizer.selectNext()
let expression = parseBoolExpression(symbolTable: symbolTable)
return VarDec(value: variableName, children: [expression])
}
}
writeStderrAndExit("Invalid statement")
return NoOp(value: "", children: [])
}
private func parseBlock(symbolTable: SymbolTable) -> Node {
var statements: [Node] = []
while tokenizer.next.type != "EOF" {
let statement = parseStatement(symbolTable: symbolTable)
statements.append(statement)
}
return Block(value: "", children: statements)
}
public func run(code: String, symbolTable: SymbolTable) -> Node {
let filteredCode = PrePro.filter(code: code)
self.tokenizer = Tokenizer(source: filteredCode)
tokenizer.selectNext() // Position the tokenizer to the first token
return parseBlock(symbolTable: symbolTable)
}
}
func writeStderrAndExit(_ message: String) {
// function that writes to stderr a received string and exits with error
fputs("ERROR: \(message)\n", stderr) // write to stderr
exit(1) // exit with error
}
func readFile(_ path: String) -> String {
do {
let contents = try String(contentsOfFile: path, encoding: .utf8)
return contents
} catch {
writeStderrAndExit("Failed to read file")
return ""
}
}
func generateUniqueIdentifier(length: Int) -> String {
let characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var identifier = ""
for _ in 0..<length {
if let randomCharacter = characters.randomElement() {
identifier.append(randomCharacter)
}
}
return identifier
}
func main() {
// Ensure there is at least one command line argument for the file path.
guard CommandLine.arguments.count > 1 else {
writeStderrAndExit("Please provide a file path.")
return
}
let filename = CommandLine.arguments[1]
if !filename.hasSuffix(".lua") {
writeStderrAndExit("Please provide a .lua file.")
}
let filenameParts = filename.split(separator: ".")
let fileContent = readFile(filename)
let symbolTable = SymbolTable()
let myParser = Parser()
let ast = myParser.run(code: fileContent, symbolTable: symbolTable)
let _ = ast.evaluate(symbolTable: symbolTable)
Assembler.generate(filename: String(filenameParts[0]) + ".asm")
}
main()