-
Notifications
You must be signed in to change notification settings - Fork 0
/
onscr_interpreter.py
328 lines (216 loc) · 8.67 KB
/
onscr_interpreter.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# onscr_interpreter.py
#
# Copyright 2011 Mark Kolloros <uvthenfuv@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
from __future__ import division, print_function, unicode_literals
import sys
import collections
import functools
import onscr_parse
class InterpreterBase(object):
# This is supposed to be subclassed.
def __init__(self, filename):
self.parser = onscr_parse.CmdReader(filename)
self.waiting = False
def run_until_wait(self):
while not self.waiting:
self.step()
def step(self):
statement = self.parser.read_next()
#~ self.error( "DEBUG:"+str(statement) )
self.run_stmt(statement)
def run_stmt(self, statement):
assert len(statement) in (1, 2)
if len(statement) == 2:
self._run_cmd(*statement)
else:
assert statement[0][0] == b"*"
# It's a label.
pass
def _run_cmd(self, cmd, args):
try:
func = getattr(self, 'do_' + cmd)
except AttributeError:
self.error("**** Command '" + cmd + "' is not supported yet.")
return
try:
func(*args)
except TypeError:
self.error( "Func error with: " + str(cmd) + " " + str(args) )
raise
def error(self, s):
print(s)
# basic functions
def do_game(self):
self.do_goto("*start")
def do_click(self):
# the NScripter command for waiting until a click
self.waiting = True
def do_EOP(self):
# EOP (end-of-page) is how the parser returns the
# backslash special command
self.waiting = True
def do_goto(self, label):
self.parser.goto(label)
def do_skip(self, n):
self.parser.skip(n)
def do_gosub(self, label):
self.parser.gosub(label)
def do_return(self):
self.parser.cmd_return()
def do_jumpf(self):
self.parser.jumpf()
def clear(self):
pass
def is_name(self, v):
return type(v) in (str, unicode) and not self.is_str(v)
def is_str(self, v):
return type(v) in (str, unicode) and v[0] in '"`' and v[0] == v[-1]
def unstr(self, s):
# Remove string markers if it is a string.
if self.is_str(s):
s = s[1:-1]
return s
def is_color(self, s):
return type(s) in (str, unicode) and s[0] == "#"
class VarKeeper(InterpreterBase):
def __init__(self, filename):
super(VarKeeper, self).__init__(filename)
self.numeric_vars = collections.defaultdict(int)
self.char_vars = collections.defaultdict(str)
self.numalias = dict()
self.stralias = dict()
def load_var(self, var):
"""If the passed argument is a variable, a stralias,
a numalias, or any combination of these, return the
value it denotes. Otherwise return it."""
if self.is_name(var):
return self.unalias(var)
elif type(var) == list and len(var) == 2 and \
var[0] in "%$":
# The right part of a variable has to be
# evaluated first.
# e.g. '%%%0' is valid ONScripter syntax,
# meaning "the value of the variable
# denoted by the value of the variable
# denoted by the value of the variable
# denoted by 0". I know, I was shocked too.
var[1] = self.load_var( var[1] )
if var[0] == "%":
return self.numeric_vars[ var[1] ]
else:
return self.char_vars[ var[1] ]
else:
return var
def abs_var(self, var):
if type(var[1]) == int:
return var
else:
return [var[0], self.load_var(var[1])]
def unalias(self, var):
if var in self.numalias:
return self.numalias[var]
elif var in self.stralias:
return self.stralias[var]
return var
def do_numalias(self, name, value):
self.numalias[name] = self.load_var(value)
def do_stralias(self, name, value):
self.numalias[name] = self.load_var(value)
def do_notif(self, conds, stmts):
self.do_if(conds, stmts, False)
def do_if(self, conds, stmts, expecting = True):
if self.eval_conds(conds) == expecting:
for stmt in stmts:
self.run_stmt(stmt)
def eval_conds(self, conds):
for cond in conds:
cmd = cond[0]
args = [ self.load_var(arg) for arg in cond[1:] ]
if cmd in (b"=", b"==") and args[0] != args[1]:
return False
elif cmd in (b"!=", b"<>") and args[0] == args[1]:
return False
elif cmd == b">" and args[0] <= args[1]:
return False
elif cmd == b"<" and args[0] >= args[1]:
return False
elif cmd == b">=" and args[0] < args[1]:
return False
elif cmd == b"<=" and args[0] > args[1]:
return False
return True
def do_cmp(self, var, s, other):
s = self.unstr( self.load_var(s) )
other = self.unstr( self.load_var(other) )
if s == other:
result = 0
elif s > other:
result = 1
else:
result = -1
self.do_mov(var, result)
def do_inc(self, var):
self.do_add(var, 1)
def do_dec(self, var):
self.do_add(var, -1)
def do_sub(self, var, num):
self.do_add(var, -num)
def do_add(self, var, num_or_str):
num_or_str = self.load_var(num_or_str)
if self.is_str(num_or_str):
self.error("**String ADD isn't supported yet!")
num = num_or_str
#~ print(var)
var = self.abs_var(var)
#~ print("after:", var)
#~ print(self.numeric_vars)
self.numeric_vars[ var[1] ] += num
def do_mov(self, var, value):
var = self.abs_var(var)
value = self.load_var(value)
if self.is_str(value):
self.char_vars[ var[1] ] = value
else:
self.numeric_vars[ var[1] ] = value
def variable_loader(f):
@functools.wraps(f)
def varloader(self, *args, **kwargs):
args = map(self.load_var, args)
# I'm running Python 2.6 right now and dict
# comprehension is introduced in 2.7.
# Also, note to future me: Be optimistic.
# Why, you ask? 'Cause it's easier that way.
kwargs = dict((k, self.load_var(v)) for k, v in kwargs.items())
f(self, *args, **kwargs)
return varloader
#~ def main():
#~ if len(sys.argv) != 2:
#~ print("Usage: onscr_interpreter.py FILENAME")
#~ exit(1)
#~
#~ interpreter = SimpleInterpreter( sys.argv[1] )
#~ interpreter.run()
#~
#~
#~ if __name__ == '__main__':
#~ main()