-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_func.py
191 lines (137 loc) · 3.16 KB
/
scanner_func.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
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
#
# wang.tr@outlook.com
#
# newest input
character = ''
# read characters
token = ""
# buffer printer
ptr = 0
# reserve table
reserve_table = {"begin": 1, "end": 2, "integer": 3, "if": 4, "then": 5, "else": 6,
"function": 7,"read": 8, "write": 9, "=": 12, "<>": 13, "<=": 14,
"<": 15, ">=": 16, ">": 17, "-": 18, "*": 19, ":=": 20, "(": 21,
")": 22, ";": 23}
# symbol table
symbol_table = []
# constant table
constant_table = []
# output
dyd = []
# max length of var
length = 16
# read
def getchar(buf):
global ptr
global character
global eoln
character = buf[ptr]
ptr += 1
if ptr >= len(buf):
if character == "\n":
return -3
return -1
if character == "\n":
return -2
return 0
# read ignoring spaces
def getnbc(buf):
global ptr
global character
if ptr >= len(buf):
dyd_return("25", "EOF")
return -1
char = buf[ptr]
# remove spaces
while (char == " ") or (char == "\n") or (char == "\t") or (char == "\f"):
if char == "\n":
dyd_return("24", "EOLN")
ptr += 1
if ptr >= len(buf):
return -1
char = buf[ptr]
# write character
character = char
ptr += 1
if ptr >= len(buf):
return -1
return 0
# add to token
def concat(error = []):
global token
global character
token += character
if len(token) > length:
# out of range
error.append("***LINE:" + str(line()) +
" length of variable:" + token + "... out of range")
return -1
return 0
# is letter?
def letter():
global character
if (character >= "a") and (character <= "z"):
return 1
elif (character >= "A") and (character <= "Z"):
return 2
else:
return -1
# is digit?
def digit():
global character
if (character >= "0") and (character <= "9"):
return 1
else:
return -1
# roll back
def retract(reset = ''):
global character
global ptr
character = reset
ptr -= 1
# is reserve?
def reserve():
if token in reserve_table:
return reserve_table[token]
else:
return 0
# is symbol?
def symbol():
if token not in symbol_table:
symbol_table.append(token)
return symbol_table.index(token)
# is constant?
def constant():
const = float(token)
if const not in constant_table:
constant_table.append(const)
return constant_table.index(const)
# add to dyd
def dyd_return(num, val):
result = " " * (16 - len(val))
result += val + " "
if len(num) < 2:
result += " "
result += num
dyd.append(result)
# reset token
def reset_token():
global token
token = ""
# deal with single symbols
def single():
global character
if character in reserve_table:
dyd_return(str(reserve_table[character]), character)
return 0
return -1
# is double?
def double():
global character
if (character == ">") or (character == "<") or (character == ":"):
return 1
else:
return 0
# get line
def line():
return dyd.count(" EOLN 24") + 1