-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpseudotoken.py
232 lines (196 loc) · 4.01 KB
/
pseudotoken.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
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
# ------------------------------------------------------------
# pseudotoken.py
#
# A WIP library for interpreting cambridge pseudocode
# ------------------------------------------------------------
import ply.lex as lex
# List of reserved words:
reserved = [
# Array Keywords
"ARRAY",
"OF",
# Arithmetic Operators
"DIV",
"MOD",
# Boolean Operators
"AND",
"OR",
"NOT",
# Data Flow
"DECLARE",
# Data Types
"DATE",
"REAL",
"INTEGER",
"CHAR",
"STRING",
"BOOLEAN",
"CONSTANT",
# IF Definition
"IF",
"THEN",
"ELSE",
"ENDIF",
# CASE Definition
"CASE",
"OTHERWISE",
"ENDCASE",
# FOR Definition
"FOR",
"TO",
"NEXT",
"ENDFOR",
# WHILE Definition
"WHILE",
"ENDWHILE",
# REPEAT-UNTIL Definition
"REPEAT",
"UNTIL",
# PROCEDURE Definition
"PROCEDURE",
"BYREF",
"BYVAL",
"ENDPROCEDURE",
"CALL",
# String Functions Definition
"LEFT",
"RIGHT",
"LENGTH",
"MID",
"LCASE",
"UCASE",
# Numeric Functions Definition
"INT",
"RAND",
# FUNCTION Definition
"FUNCTION",
"RETURNS",
"RETURN",
"ENDFUNCTION",
# File Operation Keywords
"OPENFILE",
"READ",
"WRITE",
"APPEND",
"READFILE",
"WRITEFILE",
"CLOSEFILE",
# I/O Keywords
"INPUT",
"OUTPUT",
]
# List of token names
# In order of priority - highest ones first
tokens = [
# Miscellaneous
"COMMENT",
"ASSIGN",
# Data Types
"DATETYPE",
"REALTYPE",
"INTEGERTYPE",
"CHARTYPE",
"STRINGTYPE",
"BOOLEANTYPE",
# Logical Operators
"EQUALTO",
"NOTEQUALTO",
"GREATEQUAL",
"LESSEQUAL",
"GREAT",
"LESS",
# Miscellaneous
"ID",
] + reserved
# Literal handling
literals = r"&+-*/(){}[],:."
# Logical Operators
t_EQUALTO = r"\="
t_NOTEQUALTO = r"\<\>"
t_GREATEQUAL = r"\>\="
t_LESSEQUAL = r"\<\="
t_LESS = r"\<"
t_GREAT = r"\>"
# Miscellaneous
t_COMMENT = r"\/\/.*"
# noinspection PyPep8Naming
def t_ASSIGN(t):
r"""\<\-|←"""
return t
# noinspection PyPep8Naming,PyUnusedLocal
def t_SPACE(t):
r"""\ """
# noinspection PyPep8Naming
def t_DATETYPE(t):
r"""\d{2}/\d{2}/\d{4}"""
temp = t.value
t.value = "date("
t.value += f"{int(temp[6:10])},"
t.value += f" {int(temp[3:5])},"
t.value += f" {int(temp[0:2])}"
t.value += ")"
return t
# noinspection PyPep8Naming
def t_REALTYPE(t):
r"""\d+\.\d+"""
t.value = float(t.value)
return t
# noinspection PyPep8Naming
def t_INTEGERTYPE(t):
r"""\d+"""
t.value = int(t.value)
return t
# noinspection PyPep8Naming
def t_CHARTYPE(t):
r"""\'.\'"""
t.value = str(t.value)
return t
# noinspection PyPep8Naming,PySingleQuotedDocstring
def t_STRINGTYPE(t):
r"\"[^\"]*[\\\"]*\"" ""
t.value = str(t.value)
return t
# noinspection PyPep8Naming
def t_BOOLEAN(t):
r"""TRUE|FALSE"""
if t.value == "TRUE":
t.value = bool(True)
elif t.value == "FALSE":
t.value = bool(False)
return t
# Reserved keywords + Identifiers
# noinspection PyPep8Naming
def t_ID(t):
r"""[a-zA-Z_][a-zA-Z_0-9]*"""
if t.value == "PRINT" or t.value == "OUTPUT":
t.type = "OUTPUT"
elif reserved.count(t.value) > 0:
t.type = t.value
return t
# Track line numbers
def t_newline(t):
r"""\n|\r|\r\n:"""
t.lexer.lineno += len(t.value)
# t.type = "NEWLINE"
# return t
# Ignore these characters
t_ignore = "\t"
# Error handler
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build lexer
lexer = lex.lex()
# Tokenize
def tokenize(input_text: str = "", filename: str = "") -> list[lex.LexToken]:
text = input_text
if input_text == "" and filename != "":
with open(filename, "r") as file:
text = file.read()
# Give the lexer some input
lexer.input(text)
# Tokenize
final_tokens = []
for tok in lexer:
final_tokens.append(tok)
return final_tokens