/
main.py
104 lines (87 loc) · 3.05 KB
/
main.py
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
#
# wang.tr@outlook.com
#
import io_file
import scanner_func
# error processor
error = []
if __name__ == '__main__':
#path = input()
path = "./tests/test.pas"
p_err = path[0:len(path)-3] + "err"
p_dyd = path[0:len(path)-3] + "dyd"
p_sc = path[0:len(path)-3] + "s_c"
buf = io_file.read_file(path)
end = 0
while end == 0:
single = 0
end = scanner_func.getnbc(buf)
# symbol or reserve
if scanner_func.letter() > 0:
single = 1
while (scanner_func.letter() > 0) or (scanner_func.digit() > 0):
if scanner_func.concat(error) < 0:
break
if end == -1:
break
end = scanner_func.getchar(buf)
if end == 0:
scanner_func.retract()
id = scanner_func.reserve()
if id > 0:
scanner_func.dyd_return(str(id), scanner_func.token)
else:
scanner_func.dyd_return("10", scanner_func.token)
if (end == -2) or (end == -3):
scanner_func.dyd_return("24", "EOLN")
scanner_func.reset_token()
# constant
elif scanner_func.digit() > 0:
single = 2
while scanner_func.digit() > 0:
scanner_func.concat()
if end == -1:
break
end = scanner_func.getchar(buf)
if end == 0:
scanner_func.retract()
scanner_func.dyd_return("11", scanner_func.token)
if (end == -2) or (end == -3):
scanner_func.dyd_return("24", "EOLN")
scanner_func.reset_token()
# double
elif scanner_func.double() > 0 and end != -1:
char = scanner_func.character
scanner_func.concat()
end = scanner_func.getchar(buf)
scanner_func.concat()
id = scanner_func.reserve()
if id > 0:
scanner_func.dyd_return(str(id), scanner_func.token)
single = 3
elif end == 0:
scanner_func.retract(char)
scanner_func.reset_token()
# single
if single == 0:
if scanner_func.single() < 0:
# illegal characters
if scanner_func.character == ":":
error.append("***LINE:" + str(scanner_func.line())
+ " expect '=' after ':' ")
else:
error.append("***LINE:" + str(scanner_func.line())
+ " illegal character: " + scanner_func.character)
# end of file
if end == -1:
scanner_func.dyd_return("25", "EOF")
else:
end = 0
if len(buf) != 0:
io_file.write_file(p_dyd, scanner_func.dyd)
io_file.write_file(p_sc, scanner_func.symbol_table)
io_file.add_file(p_sc, scanner_func.constant_table)
io_file.write_file(p_err, error)
print("Scanner finished")
else:
print("File not found.")