-
Notifications
You must be signed in to change notification settings - Fork 1
/
compiler.py
383 lines (338 loc) · 12.5 KB
/
compiler.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from keywords import Keyword
from operators import Operator
from variable import Variable, TYPE, FunVariable
from utility import Utility
class Compiler:
def __init__(self):
self.codes = list()
self.stack = list()
self.out = list()
self.util = Utility()
self.var = Variable()
self.funVar = FunVariable()
self.valid = False
self.indent = 0
self.currentLine = 0
def getNewLine(self, elseFlag = False):
return "\t"*(self.indent - int(elseFlag))
def save(self, path = "out.py"):
with open(path, "w") as file:
for code in self.out:
file.write(code+"\n")
def checkComment(self, code):
ix = code.find("잠만")
if ix>=0:
code = code[:ix]
return code
def getType(self, code):
if "오라고" in code:
return Keyword.CONTINUE
if "가라고" in code:
return Keyword.BREAK
if "스왑좀" in code:
return Keyword.SWAP
if "캐리좀" in code:
return Keyword.VAR_ASSIGN
if "갱좀" in code:
return Keyword.VAR_PRINT
if "리쉬좀" in code:
return Keyword.VAR_INPUT
if "근데저기" in code:
return Keyword.ELIF
if "저기" in code:
return Keyword.IF
if "아니" in code:
return Keyword.FUNCTION
if "님아" in code:
return Keyword.FOR
if "뭐함?" in code:
return Keyword.CLOSE
if "잠만" in code:
return Keyword.COMMENT
if "님" in code:
return Keyword.VAR_DECLARE
if "뭐하냐고" in code:
return Keyword.NEWLINE
if "근데" in code:
return Keyword.ELSE
if "그니까" in code:
return Keyword.WHILE
if "계속오네" in code:
return Keyword.FUN_DECLARE
if "나가라 그냥" in code:
return Keyword.RETURN
if "진짜" in code:
return Keyword.FUN_CALL
def varCheck(self, elements):
for element in elements:
if element[-1] == "님":
self.var.insert(element[:-1])
elif not self.var.get(element):
raise KeyError
return True
def varDeclare(self, code):
out = self.getNewLine()
code = code.replace(" ", "")
name = code[:code.find("님")]
self.var.insert(name)
self.out.append(out + f"{self.var.get(name)} = 0")
def varInput(self, code):
out = self.getNewLine()
elements = code.split()
strFlag = elements[-1] == "리쉬좀요"
elements = elements[:-1]
self.varCheck(elements)
elements = self.util.removeDeclare(elements)
for element in elements:
if self.var.getType(element) == TYPE.INT and strFlag:
self.var.setType(element, TYPE.STR)
if self.var.getType(element) == TYPE.STR and not strFlag:
self.var.setType(element, TYPE.INT)
if len(elements) == 1:
if strFlag:
out += f"{self.var.get(elements[0])} = input()"
else:
out += f"{self.var.get(elements[0])} = int(input())"
else:
out += f"{self.var.get(elements[0])}"
for element in elements[1:]:
out += f", {self.var.get(element)}"
if strFlag:
out+=" = input().split()"
else:
out+=" = map(int, input().split())"
self.out.append(out)
def varPrint(self, code):
out = self.getNewLine()
out += "print("
elements = code.split()
strFlag = elements[-1] == "갱좀요"
elements = elements[:-1]
if strFlag:
out+=f"chr({self.makeAssignStmt(elements[0])})"
else:
out+=f"{self.makeAssignStmt(elements[0])}"
if len(elements) == 1:
out+=",end='')"
else:
for element in elements[1:]:
if strFlag:
out+=f", chr({self.makeAssignStmt(element)})"
else:
out+=f", {self.makeAssignStmt(element)}"
out+=",end='')"
self.out.append(out)
def varSwap(self, code):
elements = code.split()[:-1]
self.varCheck(elements)
for element in elements:
out = self.getNewLine()
var_type = self.var.getType(element)
if var_type == TYPE.INT:
out += f"{self.var.get(element)} = chr({self.var.get(element)})"
self.var.setType(element, TYPE.STR)
elif var_type == TYPE.STR:
out += f"{self.var.get(element)} = ord({self.var.get(element)})"
self.var.setType(element, TYPE.INT)
else:
raise TypeError
self.out.append(out)
def makeAssignStmt(self, code, ix = 0):
stmt = ""
op = Operator.getOp()
if code[:2] == "진짜":
name = code[2:]
stmt+=f"{self.funVar.get(name)}("
return stmt
if ix == len(op):
element = code
l = len(element)
numLeftParenthesis, numRightParenthesis = self.util.getNumLeftParenthesis(element), self.util.getNumRightParenthesis(element)
stmt+="("*numLeftParenthesis
if element[0] == Operator.ONE:
if element.count(Operator.ONE) != l:
# 컴파일에러
raise KeyError
else:
stmt+=f"{l}"
else:
stmt+=f"{self.var.get(element)}"
stmt+=")"*numRightParenthesis
return stmt
elements = code.split(op[ix])
l = len(elements)
for i, element in enumerate(elements):
flag = element[-1] == "."
if flag:
element = element[:-1]
numLeftParenthesis, numRightParenthesis = self.util.getNumLeftParenthesis(element), self.util.getNumRightParenthesis(element)
stmt += "(" * numLeftParenthesis
stmt += self.makeAssignStmt(element[numLeftParenthesis:len(element)-numRightParenthesis], ix+1)
stmt += ")" * numRightParenthesis
if flag:
stmt+=")"
if i < l-1:
stmt += Operator.op[ix]
stmt = stmt.replace("(,", "(")
return stmt
def varAssign(self, code):
out = self.getNewLine()
elements = code.split(" ")
elements = [element for element in elements if element != ""]
self.varCheck([elements[0]])
variable = self.util.removeDeclare([elements[0]])[0]
out += f"{self.var.get(variable)} = "
out += self.makeAssignStmt(elements[-1])
self.out.append(out)
def forStmt(self, code):
out = self.getNewLine()
elements = code.split(" ")[:-1]
out+=f"for {self.var.get(elements[0])} in range({self.makeAssignStmt(elements[1])}, {self.makeAssignStmt(elements[2])}):"
self.indent+=1
self.out.append(out)
def ifStmt(self, code, elifFlag = False):
out = self.getNewLine(elifFlag)
elements = code.split(" ")[1:]
if elifFlag:
out+=f"elif {self.makeAssignStmt(elements[0])}:"
else:
out+=f"if {self.makeAssignStmt(elements[0])}:"
self.indent+=1
self.out.append(out)
def closeStmt(self):
self.indent-=1
def newLine(self):
out = self.getNewLine()
out += "print()"
self.out.append(out)
def elseStmt(self):
out = self.getNewLine(True)
out+="else:"
self.out.append(out)
def whileStmt(self, code):
out = self.getNewLine()
elements = code.split(" ")[1:]
out+=f"while {self.makeAssignStmt(elements[0])}:"
self.indent+=1
self.out.append(out)
def continueStmt(self):
out = self.getNewLine()
out += "continue"
self.out.append(out)
def breakStmt(self):
out = self.getNewLine()
out += "break"
self.out.append(out)
def funDeclare(self, code):
out = self.getNewLine()
elements = code.split(" ")[:-1]
name, args = elements[0], elements[1:]
self.funVar.insert(name)
out += f"def {self.funVar.get(name)}("
for i, arg in enumerate(args):
self.var.insert(arg)
if i:
out+=","
out+=self.var.get(arg)
out+="):"
self.indent += 1
self.out.append(out)
def funCall(self, code): # ret변수 : True -> 함수 호출이 한줄임, False -> 다른 구문 사이에 껴있음
out = self.getNewLine()
elements = code[2:].split(",")
name, args = elements[0], elements[1:]
out+=f"{self.funVar.get(name)}("
for i, arg in enumerate(args):
if i:
out+=","
out+=self.makeAssignStmt(arg)
out = out.replace("(,", "(")
self.out.append(out)
def returnStmt(self, code):
out = self.getNewLine()
out += "return"
code = code[:code.find("나가라 그냥")].strip()
elements = code.split(" ")
if len(elements) > 1: # 반환값은 1개 이하여야 함
raise SyntaxError
if len(elements) == 1:
out+=f" {self.makeAssignStmt(elements[0])}"
self.out.append(out)
def compileLine(self, code):
code = self.checkComment(code)
if self.util.isEmptyLine(code):
return
TYPE = self.getType(code)
if TYPE == Keyword.CONTINUE:
self.continueStmt()
if TYPE == Keyword.BREAK:
self.breakStmt()
if TYPE == Keyword.VAR_DECLARE:
self.varDeclare(code)
if TYPE == Keyword.VAR_ASSIGN:
self.varAssign(code)
if TYPE == Keyword.VAR_PRINT:
self.varPrint(code)
if TYPE == Keyword.VAR_INPUT:
self.varInput(code)
if TYPE == Keyword.SWAP: # 아스키를 숫자로 변환
self.varSwap(code)
if TYPE == Keyword.FOR:
self.forStmt(code)
if TYPE == Keyword.CLOSE:
self.closeStmt()
if TYPE == Keyword.NEWLINE:
self.newLine()
if TYPE == Keyword.IF:
self.ifStmt(code)
if TYPE == Keyword.ELSE:
self.elseStmt()
if TYPE == Keyword.ELIF:
self.ifStmt(code, True)
if TYPE == Keyword.WHILE:
self.whileStmt(code)
if TYPE == Keyword.FUN_DECLARE:
self.funDeclare(code)
if TYPE == Keyword.FUN_CALL:
self.funCall(code)
if TYPE == Keyword.RETURN:
self.returnStmt(code)
def compile(self, codes):
for ix, code in enumerate(codes):
self.currentLine = ix + 1
if ix > 0 and ix < len(codes) - 1:
self.compileLine(code)
elif ix == 0 and code != "우리 잘해보죠" or ix == len(codes) - 1 and code != "팀차이 ㅈㅈ":
raise SyntaxError
if self.indent:
raise SyntaxError
def compileFile(self, path, outPath = "out.py"):
try:
with open(path, "r", encoding="utf-8") as file:
codelines = [i.strip() for i in file.readlines()]
self.compile(codelines)
except TypeError:
print(f"{self.currentLine}번째 적이 학살중입니다!!")
except SyntaxError:
print(f"{self.currentLine}번째 적은 전설적입니다!!")
except ValueError:
print(f"{self.currentLine}번째 적을 도저히 막을 수 없습니다!!")
except KeyError:
print(f"{self.currentLine}번째 적이 전장을 지배하고 있습니다!!")
except FileNotFoundError:
print("서버에 연결할 수 없습니다.")
else:
self.save(outPath)
self.run(outPath)
def run(self, path = "out.py"):
try:
exec(open(path).read())
except ZeroDivisionError:
print(f"적이 전장의 화신입니다!!")
except Exception:
print("소환사 한명이 게임을 종료했습니다.")
# if __name__ == "__main__":
# compiler = Compiler()
# compiler.compileFile("example/while.lo")
# compiler.save()
# compiler.run()