forked from solijonovam/fp0406-language-sam-madina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini.py
358 lines (257 loc) · 8.62 KB
/
mini.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
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
"""
Author:
College: Berea College
Course: CSC420 Programming Languages (Summer 2023)
Purpose: A simple language interpreter
Notes:
History:
2023-04-05, DMW, added inline comment
2023-04-05, DMW, created
"""
import ply.lex as lex
import ply.yacc as yacc
from ASTNODE import ASTNODE
from MiniAST import MiniAST
# reserved words / keywords
reserved = {
"ayt": "AYT", #PRINT
"pass": "PASS",
"uchun": "UCHUN", #FOR
"agar": "AGAR", #IF
"boshqa": "BOSHQA", #Else
"funktsiyasi": "FUNKTSIYASI", #Function (subroutine)
"yugur": "YUGUR", #RUN
}
# tokens
tokens = [
'DQ_STRING', 'NAME', 'NUMBER', 'SEMICOLON', 'SQ_STRING', 'COMMA', # , 'LPAREN', 'RPAREN'
]
# all tokens, reserved words and keywords, combined
tokens += list(reserved.values())
# literal characters recognized by the lexer and parser
literals = ['=', '+', '-', '*', '/', '(', ')', ';', '{', '}' ]
# ---------------------------------------------------------- TOKEN DEFINITIONS
# a dictionary to save comments for tying to emitted code output
comments = {}
# TOKEN FOR NAME (or IDENTIFIER)
# noinspection PyPep8Naming
# noinspection PySingleQuotedDocstring
def t_NAME(t):
r'[a-zA-Z_][a-zA-Z0-9_]*'
t.type = reserved.get(t.value, 'NAME') # Check for reserved words
return t
# Helper function for NUMBER token
def str_to_num(s):
ans = 0
try:
ans = int(s)
except ValueError:
ans = float(s)
return ans
# noinspection PyPep8Naming
# noinspection PySingleQuotedDocstring
def t_NUMBER(t):
r'[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?'
# 2021-05-29, DMW, regular expression is from:
# https://www.regular-expressions.info/floatingpoint.html
# r'\d+' # original regular expression
t.value = str_to_num(t.value)
return t
# noinspection PyPep8Naming
# noinspection PySingleQuotedDocstring
def t_SEMICOLON(t):
r'\n+'
#""";"""
# r'(\;|\s)+'
return t
# 2021-11-11, DMW, added the ability to handle single quote strings
# based on: https://stackoverflow.com/questions/5695240/php-regex-to-ignore-escaped-quotes-within-quotes
# noinspection PyPep8Naming
# noinspection PySingleQuotedDocstring
def t_SQ_STRING(t):
r"'[^'\\]*(?:\\.[^'\\]*)*'"
return t
# 2021-11-11, DMW, added the ability to handle double quote strings
# based on: https://stackoverflow.com/questions/5695240/php-regex-to-ignore-escaped-quotes-within-quotes
# noinspection PyPep8Naming
def t_DQ_STRING(t):
# noinspection PySingleQuotedDocstring
r'"[^"\\]*(?:\\.[^"\\]*)*"'
return t
# noinspection PyPep8Naming
# noinspection PySingleQuotedDocstring
def t_INLINE_COMMENT(t):
r'//.*'
comments[t.lexer.lineno] = t.value
# return t
def t_COMMA(t):
r','
return t
# noinspection PyPep8Naming
# noinspection SpellCheckingInspection
#def t_LPAREN(t):
# """\("""
# return t
# noinspection PyPep8Naming
# noinspection SpellCheckingInspection
#def t_LPAREN(t):
# """\)"""
# return t
# Whitespace to ignore
t_ignore = " \f\t\v"
# noinspection PyPep8Naming
# noinspection PySingleQuotedDocstring
def t_EOL(t):
r'\n+'
t.lexer.lineno += t.value.count("\n")
# return t
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
# Build the lexer
lexer = lex.lex()
# ---------------------------------------------------------- DEFINE OPERATOR PRECEDENCE FOR MATH EXPRESSIONS
# noinspection SpellCheckingInspection
precedence = (
# ('nonassoc', '?', ':'), # 'EQUALS', 'LESSTHAN', 'GREATERTHAN', 'LTEQ', 'GTEQ',
# ('left', 'EQ', 'LT', 'GT', 'LTEQ', 'GTEQ'),
('left', '+', '-'),
('left', '*', '/'), # '%', 'INTDIV'),
('left', '^'), # add other similar precedence like factorial
('right', 'UMINUS', 'UPOSITIVE')
)
# ---------------------------------------------------------- PARSER DEFINITIONS (GRAMMAR, PRODUCTIONS, RULES)
# The following list of to do items aren't currently supported by the simple grammar provided
# However, they can be added if needed
# TODO: no support for block comments
# TODO: Other than recognizing strings as expressions, this grammar doesn't do anything with strings
# TODO: variables are implicitly (dynamically) typed
# TODO: no static typing support
# TODO: no support for constants
# TODO: no IF ELSE support
start = "program"
root = None
def p_program(p):
"""program : statement_list"""
global root
root = ASTNODE("program", children=[p[1]])
def p_statement_list2(p):
"""statement_list : statement_list statement"""
p[0] = ASTNODE("statement_list", children=[p[1], p[2]])
def p_statement_list(p):
"""statement_list : statement"""
p[0] = p[1]
# def p_statement(p):
# """statement : simple_statement SEMICOLON INLINE_COMMENT"""
# p[0] = p[1]
def p_statement_no_comment(p):
"""statement : simple_statement SEMICOLON"""
p[0] = p[1]
def p_statement_no_code_no_comment(p):
"""statement : SEMICOLON"""
p[0] = ASTNODE("pass")
# do nothing
def p_simple_statement_pass(p):
"""simple_statement : PASS"""
p[0] = ASTNODE("pass")
def p_simple_statement_assign(p):
"""simple_statement : NAME "=" expression"""
p[0] = ASTNODE("assign", value=p[1], children=[p[3]])
def p_simple_statement_ayt(p):
"""simple_statement : AYT '(' expression ')'"""
p[0] = ASTNODE("ayt", children=[p[3]])
# noinspection SpellCheckingInspection
def p_expression_binop(p):
"""expression : expression '+' expression
| expression '-' expression
| expression '*' expression
| expression '/' expression
| expression '^' expression"""
p[0] = ASTNODE("binop", value=p[2], children=[p[1], p[3]])
# noinspection SpellCheckingInspection
def p_expression_uminus(p):
"""expression : '-' expression %prec UMINUS"""
p[0] = ASTNODE("negate", children=[p[2]])
# noinspection SpellCheckingInspection
def p_expression_upositive(p):
"""expression : '+' expression %prec UPOSITIVE"""
p[0] = p[2]
def p_expression_group(p):
"""expression : '(' expression ')'"""
p[0] = p[2]
def p_expression_number(p):
"""expression : NUMBER"""
p[0] = ASTNODE("number", value=p[1])
def remove_outside_quotes(quoted_string: str) -> str:
if quoted_string[0] == '"':
return quoted_string.split('"')[1]
return quoted_string.split("'")[1]
def p_expression_dq_string(p):
"""expression : DQ_STRING"""
p[0] = ASTNODE("string", value=remove_outside_quotes(p[1]))
def p_expression_sq_string(p):
"""expression : SQ_STRING"""
p[0] = ASTNODE("string", value=remove_outside_quotes(p[1]))
def p_expression_name(p):
"""expression : NAME"""
p[0] = ASTNODE("name", value=p[1])
def p_statement_subroutine(p):
"""statement : FUNKTSIYASI NAME statement_block """
p[0] = ASTNODE("funktsiyasi", value=p[2], children=[p[3]])
def p_statement_call(p):
"""statement : YUGUR NAME """
p[0] = ASTNODE("yugur", value=p[2])
def p_statement_block(p):
"""statement_block : '{' statement_list '}' """
p[0] = ASTNODE('statement_list', children=[p[2]])
# p_error() is required
def p_error(p):
try:
print("{1}: Syntax error on '{0}'".format(p.value, lexer.lineno-1))
except AttributeError:
print("{0}: Syntax error on line".format(lexer.lineno-1))
parser = yacc.yacc()
# ---------------------------------------------------------- PROCESS INPUT
if __name__ == "__main__":
# a test program; the program can be read from a file, too;
program = """
ayt("")
ayt("--Variables--")
x = 5 + 3
y = 2
ayt(x)
a = 3 * y
ayt(a)
ayt("")
ayt("--Strings--")
ayt("Hello World!")
x = "This is example 1 "
ayt(x)
ayt("This is example 2 ")
y = "combined with example 3 "
ayt(x + y)
ayt("")
//FUNCTIONS
//aniqlash foo(x, y) = x * y
//output = foo(3, 4)
//qaytish output
//ayt(output)
ayt("")
ayt("--SUB ROUTINES--")
funktsiyasi hello {
ayt("hello world")
x = 2 * 5
ayt (x)
}
yugur hello
"""
root = None
yacc.parse(program) # computes root
mini_ast = MiniAST(root) # ---------- set up AST
# print(mini_ast) # ------------------- show the AST
mini_ast.reserved_names = tokens # -- tell the AST which words are reserved/keywords/tokens
mini_ast.process_ast() # ------------ Execute the program (or emit it if writing a compiler)
# diagnostic information
# print("AST stack: {}".format(mini_ast.stack.stack))
# print("Names:\n", mini_ast.names)
# print("comments:\n", comments)