Skip to content

Commit 1bfe134

Browse files
committed
Chase variable support added
1 parent 27767a4 commit 1bfe134

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

OWScript/AST.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ class EventPlayer(WorkshopType):
154154
_extends = []
155155

156156
class Variable(WorkshopType):
157-
_values = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
157+
_values = ['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
158158
_extends = []
159159

160160
def __init__(self, value, index):

OWScript/Transpiler.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import re
22
from collections import defaultdict
33
from itertools import chain, count
4-
from string import capwords
4+
from string import capwords, ascii_uppercase as letters
55

66
try:
77
from . import Errors
@@ -86,11 +86,14 @@ def __init__(self, tree, indent_size=3):
8686
self.tree = tree
8787
self.indent_size = indent_size
8888
self.indent_level = 0
89-
# Reserved:
89+
# Reserved Global Indices
9090
# 0: Map ID
9191
self.global_reserved = 1
9292
self.global_index = count(self.global_reserved)
9393
self.player_index = count()
94+
self.global_varconst = iter(letters[1:])
95+
self.player_varconst = iter(letters[1:])
96+
self.varconsts = {}
9497
self.curblock = []
9598

9699
@property
@@ -181,10 +184,31 @@ def visitOWID(self, node, scope):
181184
raise Errors.SyntaxError('\'{}\' expected {} arguments ({}), received {}'.format(
182185
name, len(node.args), ', '.join(map(lambda arg: arg.__name__, node.args)), len(node.children)),
183186
pos=node._pos)
184-
for index, types in enumerate(zip(node.args, node.children)):
187+
else:
188+
pass #wait shorthand
189+
for index, types in enumerate(zip(node.args, node.children[:])):
185190
arg, child = types
186191
if arg is None:
187192
continue
193+
elif arg == Variable:
194+
try:
195+
assert type(child) in (GlobalVar, PlayerVar)
196+
if child.name not in self.varconsts:
197+
constgen = self.global_varconst if type(child) == GlobalVar else self.player_varconst
198+
try:
199+
varconst = next(constgen)
200+
self.varconsts[child.name] = Raw(code=varconst)
201+
if type(child) == GlobalVar:
202+
code = 'Set Global Variable({}, {});\n'.format(varconst, self.visit(child, scope)) + code
203+
else:
204+
code = 'Set Player Variable({}, {}, {});\n'.format(self.visit(child.player, varconst, self.visit(child, scope))) + code
205+
except StopIteration:
206+
raise Errors.InvalidParameter('Exceeded maximum number of chase variables (25) for this type.', pos=child._pos)
207+
node.children[index] = self.varconsts[child.name]
208+
except AssertionError:
209+
raise Errors.InvalidParameter('Expected variable in chase variable expression, received {}'.format(
210+
child.__class__.__name__), pos=child._pos)
211+
continue
188212
extends = arg._extends if hasattr(arg, '_extends') else []
189213
values = list(flatten(arg.get_values()))
190214
if 'ANY' in values:

0 commit comments

Comments
 (0)