-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathowoi.py
308 lines (255 loc) · 8.41 KB
/
owoi.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
from OwOScriptGrammar.OwOScriptVisitor import OwOScriptVisitor
from OwOScriptGrammar.OwOScriptParser import OwOScriptParser
from OwOScriptGrammar.OwOScriptLexer import OwOScriptLexer
from antlr4 import *
import sys
import string
class OwOScriptExecutor(OwOScriptVisitor):
def __init__(self, token_stream, debug=False, wait=False):
self.functions = {}
self.wait = wait
self.debug = debug
self.token_stream = token_stream
self.out = ''
self.code = ''
self.stack = []
self.output = sys.stdout
self.input = sys.stdin
self.vars = {}
def pop(self):
try:
return self.stack.pop()
except:
return 0
def push(self, var):
return self.stack.append(var)
@staticmethod
def indent(code):
indented_string = ''
indent = 1
for line in code.split('\n'):
if '}' in line:
indent -= 1
indented_string += '\t' * indent + line + '\n'
if '{' in line:
indent += 1
return indented_string
def visitBignumber(self, ctx:OwOScriptParser.BignumberContext):
try:
self.push(int(float(ctx.getChild(1).getText())))
except:
pass
def visitDefinition(self, ctx:OwOScriptParser.DefinitionContext):
func_name = ctx.getChild(1).getText()
func_body = ctx.getChild(3)
if func_name not in self.functions:
self.functions[func_name] = func_body
def visitFunctioncall(self, ctx:OwOScriptParser.FunctioncallContext):
func_name = ctx.getChild(0).getText()
if func_name in self.functions:
self.visitChildren(self.functions[func_name])
else:
sys.stderr.write('Unknown function %s' % func_name)
sys.exit()
def visitScript(self, ctx: OwOScriptParser.ScriptContext):
self.code = self.indent(
ctx.getText()
.replace(';', ';\n')
.replace('{', ' {\n')
.replace('}', '}\n')
)
return self.visitChildren(ctx)
def visitNumber(self, ctx: OwOScriptParser.NumberContext):
try:
self.push(int(ctx.getChild(1).getText(), 16))
except:
pass
self.print_info(ctx)
return self.visitChildren(ctx)
def visitWhileloop(self, ctx: OwOScriptParser.WhileloopContext):
while self.stack[-1]:
self.visitChildren(ctx.getChild(2))
self.print_info(ctx)
def visitTernary(self, ctx: OwOScriptParser.TernaryContext):
if self.pop():
self.visitChildren(ctx.getChild(2))
else:
self.visitChildren(ctx.getChild(6))
self.print_info(ctx)
def visitCommand(self, ctx: OwOScriptParser.CommandContext):
self.exec(ctx.getText().lower())
self.print_info(ctx)
def exec(self, command):
if command == 'add':
b = self.pop()
a = self.pop()
self.push(a + b)
elif command == 'sub':
b = self.pop()
a = self.pop()
self.push(a - b)
elif command == 'mult':
b = self.pop()
a = self.pop()
self.push(a * b)
elif command == 'div':
b = self.pop()
a = self.pop()
if b != 0:
self.push(a // b)
elif command == 'mod':
b = self.pop()
a = self.pop()
self.push(a % b)
elif command == 'exp':
b = self.pop()
a = self.pop()
if a < 30 and b < 30:
self.push(a ** b)
else:
raise ValueError('Too high of an exponent!')
elif command == 'print':
a = self.pop()
if self.debug:
self.out += chr(a)
else:
self.output.write(chr(a))
elif command == 'printnum':
a = self.pop()
if self.debug:
self.out += str(a)
else:
self.output.write(str(a))
elif command == 'printstack':
self.output.write(str(self.stack))
elif command == 'input':
self.push(ord(self.input.read(1)))
elif command == 'inputnum':
num = 0
data = self.input.read(1)
while data in '0123456789':
num *= 10
num += int(data)
data = self.input.read(1)
self.push(num)
elif command == 'lt':
b = self.pop()
a = self.pop()
self.push(1 if a < b else 0)
elif command == 'gt':
b = self.pop()
a = self.pop()
self.push(1 if a > b else 0)
elif command == 'eq':
b = self.pop()
a = self.pop()
self.push(1 if a == b else 0)
elif command == 'neq':
b = self.pop()
a = self.pop()
self.push(1 if a != b else 0)
elif command == 'cmp':
b = self.pop()
a = self.pop()
if a == b:
self.push(0)
if a > b:
self.push(1)
else:
self.push(-1)
elif command == 'dupe':
a = self.pop()
self.push(a)
self.push(a)
elif command == 'dupedeep':
a = self.pop()
if a >= 0:
self.stack.extend(self.stack[-a:])
elif command == 'stacklength':
self.push(len(self.stack))
elif command == 'discard':
self.pop()
elif command == 'swap':
b = self.pop()
a = self.pop()
self.push(b)
self.push(a)
elif command == 'push':
b = self.pop()
a = self.pop()
if 0 < b < len(self.stack):
self.stack.insert(len(self.stack) - b, a)
elif b > len(self.stack):
self.stack.insert(0, a)
else:
self.push(a)
elif command == 'fetch':
a = self.pop()
if 0 < a < len(self.stack):
val = self.stack.pop(len(self.stack) - a - 1)
self.push(val)
elif a >= len(self.stack):
val = self.stack.pop(0)
self.push(val)
elif command == 'store':
b = self.pop()
a = self.pop()
self.vars[a] = b
elif command == 'get':
a = self.pop()
self.push(self.vars.get(a, 0))
elif command == 'stop':
sys.exit(self.pop())
elif command == 'fetchdupe':
a = self.pop()
if 0 < a < len(self.stack):
self.push(self.stack[len(self.stack) - a - 1])
elif a >= len(self.stack):
self.push(self.stack[0])
elif command == 'pushdupe':
b = self.pop()
a = self.pop()
if 0 < b < len(self.stack):
self.stack.insert(len(self.stack) - b, a)
elif b > len(self.stack):
self.stack.insert(0, a)
self.push(a)
elif command == 'hexmult':
b = self.pop()
a = self.pop()
self.push(a * 16 + b)
elif command == 'printhash':
self.output.write('%s' % self.vars)
elif command == 'nop':
pass
else:
raise ValueError('Unknown command %s' % command)
def print_info(self, ctx):
if not self.debug:
return
print('\n' * 50)
if len(self.code) < 8000:
print('Code:')
print(self.code)
print('\n\nStack:')
print(' | '.join([str(item) for item in self.stack]))
print(' | '.join([chr(item) if 32 <= item <= 127 else ' ' for item in self.stack]))
print('\nHashmap:')
print(', '.join(['%s: %s' % (key, value) for key, value in self.vars.items()]))
print('Output:')
print(self.out)
if self.wait:
input()
def run_owo_pseudocode(code, debug):
lexer = OwOScriptLexer(InputStream(code))
stream = CommonTokenStream(lexer)
parser = OwOScriptParser(stream)
parser.removeErrorListeners()
tree = parser.script()
visitor = OwOScriptExecutor(stream, debug)
try:
visitor.visit(tree)
if debug:
print('Completed')
except Exception as e:
raise e