-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path인터프리트하는데정신나갈거같애.py
179 lines (148 loc) · 3.74 KB
/
인터프리트하는데정신나갈거같애.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
#/usr/bin/python3
import io
import sys
debug = False
input_stream = io.TextIOWrapper(sys.stdin.buffer, encoding='utf-8')
stack = []
queue = []
tmp = 0
nline = 0
ncol = 0
def parse_block(block):
fdata = {
"정신나갈것같애": [push(tmp)],
"정신나갈거같애": [read()],
"정신나가서먹을것같애": [pop()],
"정신나가서먹을거같애": [write(tmp)],
"점심나갈것같애": [enqueue(tmp)],
"점심나갈거같애": [tmp_dec()],
"점심나가서먹을것같애": [dequeue()],
"점심나가서먹을거같애": [tmp_inc()],
}
if debug:
fdata['<'] = [enqueue(tmp)]
fdata['>'] = [dequeue()]
fdata['v'] = [push(tmp)]
fdata['^'] = [pop()]
fdata['+'] = [tmp_inc()]
fdata['-'] = [tmp_dec()]
fdata['p'] = [write(tmp)]
fdata['r'] = [read()]
fdata['l'] = [move_line()]
fdata['.'] = [tmp_reset()]
for k, v in fdata.copy().items():
if "애" in k:
fdata[k.replace("애", "아")] = v + [move_line()]
for k, v in fdata.copy().items():
fdata[k + '.'] = v + [tmp_reset()]
if "정신" in k:
fdata[k + '?'] = v + [stack_cmp()]
elif "점심" in k:
fdata[k + '?'] = v + [queue_cmp()]
return fdata[block]
def interpret(lines):
global ncol
blocks = lines[:]
if debug:
blocks = list(map(lambda x: list(x.strip()), blocks))
else:
blocks = list(map(
lambda x: x.strip().split(),
map(
lambda x:
x.replace('같아', '같아 ').replace('같애', '같애 ')
.replace(' .', '. ').replace(' ?', '? '),
blocks
)
))
# print(blocks)
while nline < len(blocks):
fs = parse_block(blocks[nline][ncol])
if len(blocks[nline]) - 1 == ncol and "move_line" not in str(fs):
fs.append(next_line())
if debug:
print("l", nline, ncol)
for f in fs:
if debug:
print(f)
f()
if debug:
print("v", tmp, stack, queue)
ncol += 1
def next_line():
def inner():
global nline, ncol
nline += 1
ncol = -1
return inner
def move_line():
def inner():
global nline, ncol
nline += stack.pop()
ncol = -1
return inner
def enqueue(x):
def inner():
queue.insert(0, x)
return inner
def dequeue():
def inner():
global tmp
tmp = queue.pop()
return inner
def queue_cmp():
def inner():
global tmp
enqueue(int(queue[-1] == tmp))()
return inner
def push(x):
def inner():
stack.append(x)
return inner
def pop():
def inner():
global tmp
tmp = stack.pop()
return inner
def stack_cmp():
def inner():
global tmp
push(int(stack[-1] == tmp))()
return inner
def tmp_dec():
def inner():
global tmp
tmp -= 1
return inner
def tmp_inc():
def inner():
global tmp
tmp += 1
return inner
def tmp_reset():
def inner():
global tmp
tmp = 0
return inner
def read():
def inner():
global tmp
tmp = ord(input_stream.read(1))
return inner
def write(code):
def inner():
sys.stdout.write(chr(code))
return inner
def main():
if not sys.argv[1]:
print("파일안줘서정신나갈것같애")
exit(1)
f = None
try:
f = open(sys.argv[1], 'r')
except FileNotFoundError:
print("파일이없어정신나갈것같애")
exit(1)
interpret(f.readlines())
if __name__ == '__main__':
main()