forked from OoTRandomizer/OoT-Randomizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuleParser.py
More file actions
260 lines (215 loc) · 9.34 KB
/
RuleParser.py
File metadata and controls
260 lines (215 loc) · 9.34 KB
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
import ast
from collections import defaultdict
import logging
import re
from Item import ItemFactory
from ItemList import item_table
from Location import Location
from State import State
escaped_items = {}
for item in item_table:
escaped_items[re.sub(r'[\'()[\]]', '', item.replace(' ', '_'))] = item
event_name = re.compile(r'\w+')
class Rule_AST_Transformer(ast.NodeTransformer):
def __init__(self, world):
self.world = world
self.events = set()
# map Region -> rule ast string -> item name
self.replaced_rules = defaultdict(dict)
# delayed rules need to keep: region name, ast node, event name
self.delayed_rules = []
def visit_Name(self, node):
if node.id in escaped_items:
return ast.Call(
func=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr='has',
ctx=ast.Load()),
args=[ast.Str(escaped_items[node.id])],
keywords=[])
elif node.id in self.world.__dict__:
return ast.Attribute(
value=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr='world',
ctx=ast.Load()),
attr=node.id,
ctx=ast.Load())
elif node.id in State.__dict__:
return ast.Call(
func=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr=node.id,
ctx=ast.Load()),
args=[],
keywords=[])
elif event_name.match(node.id):
self.events.add(node.id.replace('_', ' '))
return ast.Call(
func=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr='has',
ctx=ast.Load()),
args=[ast.Str(node.id.replace('_', ' '))],
keywords=[])
else:
raise Exception('Parse Error: invalid node name %s' % node.id, self.current_spot.name, ast.parse(node, False))
def visit_Str(self, node):
return ast.Call(
func=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr='has',
ctx=ast.Load()),
args=[ast.Str(node.s)],
keywords=[])
def visit_Tuple(self, node):
if len(node.elts) != 2:
raise Exception('Parse Error: Tuple must have 2 values', self.current_spot.name, ast.parse(node, False))
item, count = node.elts
if not isinstance(item, (ast.Name, ast.Str)):
raise Exception('Parse Error: first value must be an item. Got %s' % item.__class__.__name__, self.current_spot.name, ast.parse(node, False))
iname = item.id if isinstance(item, ast.Name) else item.s
if not (isinstance(count, ast.Name) or isinstance(count, ast.Num)):
raise Exception('Parse Error: second value must be a number. Got %s' % item.__class__.__name__, self.current_spot.name, ast.parse(node, False))
if isinstance(count, ast.Name):
count = ast.Attribute(
value=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr='world',
ctx=ast.Load()),
attr=count.id,
ctx=ast.Load())
if iname in escaped_items:
iname = escaped_items[iname]
if iname not in item_table:
self.events.add(iname)
return ast.Call(
func=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr='has',
ctx=ast.Load()),
args=[ast.Str(iname), count],
keywords=[])
def visit_Call(self, node):
if not isinstance(node.func, ast.Name):
return node
if node.func.id in dir(self):
return getattr(self, node.func.id)(node)
new_args = []
for child in node.args:
if isinstance(child, ast.Name):
if child.id in self.world.__dict__:
child = ast.Attribute(
value=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr='world',
ctx=ast.Load()),
attr=child.id,
ctx=ast.Load())
elif child.id in escaped_items:
child = ast.Str(escaped_items[child.id])
else:
child = ast.Str(child.id.replace('_', ' '))
elif not isinstance(child, ast.Str):
child = self.visit(child)
new_args.append(child)
return ast.Call(
func=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr=node.func.id,
ctx=ast.Load()),
args=new_args,
keywords=node.keywords)
def visit_Subscript(self, node):
if isinstance(node.value, ast.Name):
return ast.Subscript(
value=ast.Attribute(
value=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr='world',
ctx=ast.Load()),
attr=node.value.id,
ctx=ast.Load()),
slice=ast.Index(value=ast.Str(node.slice.value.id.replace('_', ' '))),
ctx=node.ctx)
else:
return node
def visit_Compare(self, node):
def escape_or_string(n):
if isinstance(n, ast.Name) and n.id in escaped_items:
return ast.Str(escaped_items[n])
elif not isinstance(n, ast.Str):
return self.visit(n)
return n
node.left = escape_or_string(node.left)
node.comparators = list(map(escape_or_string, node.comparators))
node.ops = list(map(self.visit, node.ops))
return node
def replace_subrule(self, target, node):
rule = ast.dump(node, False)
if rule in self.replaced_rules[target]:
return self.replaced_rules[target][rule]
subrule_name = target + ' Subrule %d' % (1 + len(self.replaced_rules[target]))
# Save the info to be made into a rule later
self.delayed_rules.append((target, node, subrule_name))
# Replace the call with a reference to that item
item_rule = ast.Call(
func=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr='has',
ctx=ast.Load()),
args=[ast.Str(subrule_name)],
keywords=[])
# Cache the subrule for any others in this region
# (and reserve the item name in the process)
self.replaced_rules[target][rule] = item_rule
return item_rule
# Requires the target regions have been defined in the world.
def create_delayed_rules(self):
for region_name, node, subrule_name in self.delayed_rules:
region = self.world.get_region(region_name)
event = Location(subrule_name, type='Event', parent=region)
event.world = self.world
self.current_spot = event
newrule = ast.fix_missing_locations(
ast.Expression(ast.Lambda(
args=ast.arguments(
args=[ast.arg(arg='state')],
defaults=[],
kwonlyargs=[],
kw_defaults=[]),
# This could, in theory, create further subrules.
body=self.visit(node))))
event.access_rule = eval(compile(newrule, '<string>', 'eval'))
region.locations.append(event)
item = ItemFactory(subrule_name, self.world, event=True)
self.world.push_item(event, item)
event.locked = True
self.world.event_items.add(subrule_name)
# Safeguard in case this is called multiple times per world
self.delayed_rules.clear()
## Handlers for specific internal functions used in the json logic.
# at(region_name, rule)
# Creates an internal event at the remote region and depends on it.
def at(self, node):
# Cache this under the target (region) name
if len(node.args) < 2 or not isinstance(node.args[0], ast.Str):
raise Exception('Parse Error: invalid at() arguments', self.current_spot.name, ast.dump(node, False))
return self.replace_subrule(node.args[0].s, node.args[1])
# here(rule)
# Creates an internal event in the same region and depends on it.
def here(self, node):
if not node.args:
raise Exception('Parse Error: missing here() argument', self.current_spot.name, ast.parse(node, False))
return self.replace_subrule(
self.current_spot.parent_region.name,
node.args[0])
def parse_spot_rule(self, spot):
if spot.rule_string is None:
return lambda state: True
rule = 'lambda state: ' + spot.rule_string
rule = rule.split('#')[0]
self.current_spot = spot
rule_ast = ast.parse(rule, mode='eval')
rule_ast = ast.fix_missing_locations(self.visit(rule_ast))
spot.access_rule = eval(compile(rule_ast, '<string>', 'eval'))