-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
211 lines (182 loc) · 4.08 KB
/
parse.py
File metadata and controls
211 lines (182 loc) · 4.08 KB
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
from utils import expect, accept, ReferenceError
def parser(tokens):
global t
t = tokens
ast = semi()
if len(t) > 0:
print()
print(t)
ReferenceError("セミコロンの付け忘れです")
return ast
def value():
if len(t) == 0:return
data = t.pop(0)
if data.isdigit():
return int(data)
elif data == "else":
return None
elif data == "end":
return None
elif data == "case":
t.insert(0,data)
return None
elif data == ")":
t.insert(0,data)
else:
return data
def paren():
if accept(t,"("):
expect(t,"(")
ast = semi()
expect(t,")")
return ast
elif accept(t, "()"):
expect(t,"()")
return None
return value()
def funccall():
if len(t) == 0:return
ast = paren()
while accept(t,"("):
expect(t,"(")
ast = [ast,semi()]
expect(t,")")
if accept(t, "()"):
expect(t,"()")
return ast
return ast
def using_module():
ast = funccall()
if ast == "using":
prg = t.pop(0)
ast = [[ast,prg],semi()]
return ast
def import_module():
ast = using_module()
if ast == "import":
prg = t.pop(0)
ast = [[ast,prg],semi()]
return ast
def func_module():
ast = import_module()
if ast == "func":
name = semi()
expect(t,":")
ast = [[ast,name[0],name[1:],semi()],semi()]
return ast
def while_module():
ast = func_module()
if ast == "while":
condition = semi()
expect(t,":")
positive = semi()
ast = [[ast,condition,positive],semi()]
return ast
def if_module():
ast = while_module()
if ast == "if":
condition = semi()
expect(t, ":")
#print(condition)
positive = semi()
#print(positive)
expect(t, ":")
negative = semi()
#print(negative)
ast = [[ast,condition,positive,negative],semi()]
return ast
def list_module():
ast = if_module()
if ast == "[":
ast = ["[",semi(),expect(t,"]")]
return ast
def mod():
ast = list_module()
while accept(t, "%"):
op = expect(t, "%")
ast = [ast, op, if_module()]
return ast
def surplus():
ast = mod()
while accept(t, "**"):
op = expect(t, "**")
ast = [ast, op, mod()]
return ast
def div():
ast = surplus()
while accept(t, "/"):
op = expect(t, "/")
ast = [ast, op, surplus()]
return ast
def mul():
ast = div()
while accept(t, "*"):
op = expect(t, "*")
ast = [ast, op, div()]
return ast
def equivalent():
ast = mul()
while accept(t, "=="):
op = expect(t, "==")
ast = [ast,op,mul()]
return ast
def conpare_left():
ast = equivalent()
while accept(t, ">"):
op = expect(t, ">")
ast = [ast,op,equivalent()]
return ast
def conpare_right():
ast = conpare_left()
while accept(t, "<"):
op = expect(t, "<")
ast = [ast,op,conpare_left()]
return ast
def sub():
ast = conpare_right()
while accept(t, "-"):
op = expect(t, "-")
ast = [ast,op,conpare_right()]
return ast
def plusplus():
ast = sub()
while accept(t, "++"):
op = expect(t,"++")
ast = [ast,op]
return ast
def plus():
ast = plusplus()
while accept(t, "+"):
op = expect(t, "+")
ast = [ast,op,sub()]
return ast
def assign():
ast = plus()
if accept(t, "="):
op = expect(t, "=")
ast = [ast,op,plus()]
return ast
def comment_out():
ast = assign()
if ast == "#":
t.pop(0)
ast = assign()
return ast
def comma():
ast = comment_out()
while accept(t, ","):
expect(t,",")
ast = [ast, ",",semi()]
return ast
def period():
ast = comma()
while accept(t,"."):
expect(t,".")
ast = [ast,".",semi()]
return ast
def semi():
ast = period()
if len(t) != 0:
while accept(t,";"):
ast = [ast,expect(t,";"),semi()]
return ast