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
171 lines (138 loc) · 5.6 KB
/
RuleParser.py
File metadata and controls
171 lines (138 loc) · 5.6 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
import ast
from ItemList import item_table
from State import State
import re
escaped_items = {}
for item in item_table:
escaped_items[re.sub(r'[\'()[\]]', '', item.replace(' ', '_'))] = item
lambda_methods = ['as_either', 'as_both', 'as_adult', 'as_child',
'as_either_here', 'as_both_here', 'as_adult_here', 'as_child_here']
class Rule_AST_Transformer(ast.NodeTransformer):
def __init__(self, world):
self.world = world
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=[])
else:
raise Exception('Parse Error: invalid node name %s', node.id)
def visit_Tuple(self, node):
if len(node.elts) != 2:
raise Exception('Parse Error: Tuple must has 2 values')
item, count = node.elts
if isinstance(item, ast.Str):
item = ast.Name(id=item.s, ctx=ast.Load())
if not isinstance(item, ast.Name):
raise Exception('Parse Error: first value must be an item. Got %s' % item.__class__.__name__)
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__)
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 item.id in escaped_items:
item.id = escaped_items[item.id]
if not item.id in item_table:
raise Exception('Parse Error: invalid item name')
return ast.Call(
func=ast.Attribute(
value=ast.Name(id='state', ctx=ast.Load()),
attr='has',
ctx=ast.Load()),
args=[ast.Str(item.id), count],
keywords=[])
def visit_Call(self, node):
if node.func.id in lambda_methods and len(node.args) > 0:
node.args = [ast.Lambda(
args=ast.arguments(
args=[ast.arg(arg='state')],
defaults=[],
kwonlyargs=[],
kw_defaults=[]),
body=node.args[0])]
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('_', ' '))
else:
self.visit(child)
new_args.append(child)
if isinstance(node.func, ast.Name):
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)
else:
return node
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):
if isinstance(node.left, ast.Name):
if node.left.id in escaped_items:
node.left = ast.Str(escaped_items[node.left.id])
if isinstance(node.comparators[0], ast.Name):
if node.comparators[0].id in escaped_items:
node.comparators[0] = ast.Str(escaped_items[node.comparators[0].id])
self.generic_visit(node)
return node
def parse_rule_string(rule, world):
if rule is None:
return lambda state: True
else:
rule = 'lambda state: ' + rule
rule = rule.split('#')[0]
rule_ast = ast.parse(rule, mode='eval')
rule_ast = ast.fix_missing_locations(Rule_AST_Transformer(world).visit(rule_ast))
rule_lambda = eval(compile(rule_ast, '<string>', 'eval'))
return rule_lambda