-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.py
246 lines (204 loc) · 4.36 KB
/
parser.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
from metaL import *
## lexer
import ply.lex as lex
tokens = ['symbol', 'string', 'integer', 'number',
'add', 'sub', 'mul', 'div', 'pow',
'colon', 'semicolon', 'tick',
'push', 'lshift', 'rshift', 'eq', 'dot',
'exit']
t_ignore = ' \t\r\n'
t_ignore_comment = r'\#.*'
states = (('str', 'exclusive'),)
states = (('str', 'exclusive'),)
t_str_ignore = ''
def t_str(t):
r"\'\n*"
t.lexer.push_state('str')
t.lexer.string = ''
def t_str_string(t):
r"\'"
t.lexer.pop_state()
t.value = String(t.lexer.string)
return t
def t_str_any(t):
r"."
t.lexer.string += t.value
def t_str_nl(t):
r"\n+"
t.lexer.lineno += len(t.value)
t.lexer.string += t.value
def t_exit(t):
r'exit\(\)'
return t
def t_nl(t):
r'\n'
t.lexer.lineno += 1
def t_semicolon(t):
r'\;'
return t
def t_push(t):
r'\/\/'
t.value = Op(t.value)
return t
def t_lshift(t):
r'\<\<'
t.value = Op(t.value)
return t
def t_rshift(t):
r'\>\>'
t.value = Op(t.value)
return t
def t_eq(t):
r'\='
t.value = Op(t.value)
return t
def t_dot(t):
r'\.'
t.value = Op(t.value)
return t
def t_colon(t):
r'\:'
t.value = Op(t.value)
return t
def t_add(t):
r'\+'
t.value = Op(t.value)
return t
def t_sub(t):
r'\-'
t.value = Op(t.value)
return t
def t_mul(t):
r'\*'
t.value = Op(t.value)
return t
def t_div(t):
r'\/'
t.value = Op(t.value)
return t
def t_pow(t):
r'\^'
t.value = Op(t.value)
return t
def t_tick(t):
r'\`'
t.value = Op(t.value)
return t
def t_number(t):
r'[0-9]+(\.[0-9]*)'
t.value = Number(t.value)
return t
def t_integer(t):
r'[0-9]+'
t.value = Integer(t.value)
return t
def t_symbol(t):
r'[^ \t\r\n\#\+\-\*\/\^\:\;\<\>\'\=\.]+'
t.value = Symbol(t.value)
return t
def t_ANY_error(t): raise SyntaxError(t)
lexer = lex.lex()
## parser
import ply.yacc as yacc
precedence = (
('left', 'eq',),
('left', 'push', 'lshift', 'rshift',),
('left', 'add', 'sub'),
('left', 'mul', 'div'),
('left', 'pow', ),
('left', 'pfx', ),
('left', 'dot',),
('nonassoc', 'colon',),
)
class AST(Vector):
pass
def p_REPL_exit(p):
' REPL : exit '
BYE()
def p_REPL_none(p):
' REPL : '
p[0] = AST('')
def p_REPL_recursion(p):
' REPL : REPL ex '
p[0] = p[1] // p[2]
def p_REPL_semicolon(p):
' REPL : REPL semicolon ex '
p[0] = p[1] // p[3]
def p_ex_symbol(p):
' ex : symbol '
p[0] = p[1]
def p_ex_string(p):
' ex : string '
p[0] = p[1]
def p_ex_number(p):
' ex : number '
p[0] = p[1]
def p_ex_integer(p):
' ex : integer '
p[0] = p[1]
def p_ex_plus(p):
' ex : add ex %prec pfx '
p[0] = p[1] // p[2]
def p_ex_minus(p):
' ex : sub ex %prec pfx '
p[0] = p[1] // p[2]
def p_ex_tick(p):
' ex : tick ex %prec pfx '
p[0] = p[1] // p[2]
def p_ex_add(p):
' ex : ex add ex '
p[0] = p[2] // p[1] // p[3]
def p_ex_sub(p):
' ex : ex sub ex '
p[0] = p[2] // p[1] // p[3]
def p_ex_mul(p):
' ex : ex mul ex '
p[0] = p[2] // p[1] // p[3]
def p_ex_div(p):
' ex : ex div ex '
p[0] = p[2] // p[1] // p[3]
def p_ex_pow(p):
' ex : ex pow ex '
p[0] = p[2] // p[1] // p[3]
def p_ex_colon(p):
' ex : ex colon ex '
p[0] = p[2] // p[1] // p[3]
def p_ex_push(p):
' ex : ex push ex '
p[0] = p[2] // p[1] // p[3]
def p_ex_lshift(p):
' ex : ex lshift ex '
p[0] = p[2] // p[1] // p[3]
def p_ex_rshift(p):
' ex : ex rshift ex '
p[0] = p[2] // p[1] // p[3]
def p_ex_eq(p):
' ex : ex eq ex '
p[0] = p[2] // p[1] // p[3]
def p_ex_dot(p):
' ex : ex dot ex '
p[0] = p[2] // p[1] // p[3]
def p_error(p): raise SyntaxError(p)
parser = yacc.yacc(debug=False, write_tables=False)
## interpreter
import traceback
def q(src, ctx=vm): # [q]uery
parser.ctx = ctx
for ast in parser.parse(src):
print(ast)
try:
res = ast.eval(ctx)
assert type(res) not in [Undef]
print(res)
except Exception as e:
traceback.print_exc()
print('-' * 66)
def repl(ctx=vm):
while True:
query = input("[sync:%i] %s " % (len(storage), ctx.head(test=True)))
q(query, ctx)
if __name__ == '__main__':
q(vm, """""")
q(vm, """ AUTHOR """)
q(vm, """ 2^10. """)
repl()