Skip to content

Commit f29f3b4

Browse files
committed
WIP: handle labels more readably
1 parent aede745 commit f29f3b4

25 files changed

Lines changed: 149 additions & 120 deletions

nml/actions/action0.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -722,11 +722,11 @@ def get_tracktypelist_action(table_prop_id, cond_tracktype_not_defined, tracktyp
722722
id_table.append(tracktype)
723723
offset+=4
724724
continue
725-
param, extra_actions = actionD.get_tmp_parameter(expression.ConstantNumeric(expression.parse_string_to_dword(tracktype[-1])))
725+
param, extra_actions = actionD.get_tmp_parameter(expression.Label(tracktype[-1]))
726726
action_list.extend(extra_actions)
727727
for idx in range(len(tracktype)-2, -1, -1):
728-
val = expression.ConstantNumeric(expression.parse_string_to_dword(tracktype[idx]))
729-
action_list.append(action7.SkipAction(0x09, 0x00, 4, (cond_tracktype_not_defined, None), val.value, 1))
728+
val = expression.Label(tracktype[idx])
729+
action_list.append(action7.SkipAction(0x09, 0x00, 4, (cond_tracktype_not_defined, None), val, 1))
730730
action_list.append(actionD.ActionD(expression.ConstantNumeric(param), expression.ConstantNumeric(0xFF), nmlop.ASSIGN, expression.ConstantNumeric(0xFF), val))
731731
act6.modify_bytes(param, 4, offset)
732732
id_table.append(expression.StringLiteral(r"\00\00\00\00", None))
@@ -958,8 +958,8 @@ def __init__(self, source, target):
958958

959959
def write(self, file):
960960
file.print_bytex(0x11)
961-
file.print_dwordx(self.source)
962-
file.print_dwordx(self.target)
961+
self.source.write(file, 4)
962+
self.target.write(file, 4)
963963
file.newline()
964964

965965
def get_size(self):

nml/actions/action0properties.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import itertools
1717
from nml import generic, nmlop
1818
from nml.expression import (BinOp, ConstantNumeric, ConstantFloat, Array, StringLiteral,
19-
Identifier, ProduceCargo, AcceptCargo, parse_string_to_dword)
19+
Identifier, ProduceCargo, AcceptCargo, Label)
2020

2121
tilelayout_names = {}
2222

@@ -1036,8 +1036,7 @@ def write(self, file):
10361036
file.print_bytex(self.prop_num)
10371037
file.print_byte(len(self.labels))
10381038
for label in self.labels:
1039-
parse_string_to_dword(label) # Error if the wrong length or not ASCII
1040-
label.write(file, 4)
1039+
Label(label).write(file, 4)
10411040
file.newline()
10421041

10431042
def get_size(self):

nml/actions/action11.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ImportSound(base_action.BaseAction):
5858
<sprite-number> * <length> FE 00 <grfid> <number>
5959
6060
@ivar grfid: ID of the other grf.
61-
@type grfid: C{int}
61+
@type grfid: L{Label}
6262
6363
@ivar number: Sound number to load.
6464
@type number: C{int}
@@ -76,7 +76,7 @@ def write(self, file):
7676
file.start_sprite(8)
7777
file.print_bytex(0xfe)
7878
file.print_bytex(0)
79-
file.print_dwordx(self.grfid)
79+
self.grfid.write(file, 4)
8080
file.print_wordx(self.number)
8181
file.end_sprite()
8282
if self.last: file.newline()

nml/actions/action2var.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ def preprocess_storageop(self, expr):
432432

433433
if expr.info['perm'] and self.feature == 0x08:
434434
# store grfid in register 0x100 for town persistent storage
435-
grfid = expression.ConstantNumeric(0xFFFFFFFF if expr.grfid is None else expression.parse_string_to_dword(expr.grfid))
435+
grfid = expression.ConstantNumeric(0xFFFFFFFF) if expr.grfid is None else expression.Label(expr.grfid)
436436
store_op = nmlop.STO_TMP(grfid, 0x100, expr.pos)
437437
ret = nmlop.VAL2(store_op, ret)
438438
elif expr.grfid is not None:

nml/actions/action7.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ def write(self, file):
4646
file.print_bytex(self.condtype[0], self.condtype[1])
4747
if self.varsize == 8:
4848
#grfid + mask
49-
file.print_dwordx(self.value & 0xFFFFFFFF)
50-
file.print_dwordx(self.value >> 32)
49+
file.print_dwordx(self.value.value & 0xFFFFFFFF)
50+
file.print_dwordx(self.value.value >> 32)
5151
else:
52-
file.print_varx(self.value, self.varsize)
52+
self.value.write(file, self.varsize)
5353
file.print_bytex(self.label)
5454
file.newline()
5555
file.end_sprite()
@@ -62,7 +62,7 @@ def skip_action9(self):
6262

6363
class UnconditionalSkipAction(SkipAction):
6464
def __init__(self, action_type, label):
65-
SkipAction.__init__(self, action_type, 0x9A, 1, (0, r'\71'), 0, label)
65+
SkipAction.__init__(self, action_type, 0x9A, 1, (0, r'\71'), expression.ConstantNumeric(0), label)
6666

6767
def op_to_cond_op(op):
6868
#The operators are reversed as we want to skip if the expression is true
@@ -84,7 +84,7 @@ def parse_conditional(expr):
8484
- The size of the value (as integer)
8585
'''
8686
if expr is None:
87-
return (None, [], (2, r'\7='), 0, 4)
87+
return (None, [], (2, r'\7='), expression.ConstantNumeric(0), 4)
8888
if isinstance(expr, expression.BinOp):
8989
if expr.op == nmlop.HASBIT or expr.op == nmlop.NOTHASBIT:
9090
if isinstance(expr.expr1, expression.Parameter) and isinstance(expr.expr1.num, expression.ConstantNumeric):
@@ -93,7 +93,7 @@ def parse_conditional(expr):
9393
else:
9494
param, actions = actionD.get_tmp_parameter(expr.expr1)
9595
if isinstance(expr.expr2, expression.ConstantNumeric):
96-
bit_num = expr.expr2.value
96+
bit_num = expr.expr2
9797
else:
9898
if isinstance(expr.expr2, expression.Parameter) and isinstance(expr.expr2.num, expression.ConstantNumeric):
9999
param = expr.expr2.num.value
@@ -103,7 +103,7 @@ def parse_conditional(expr):
103103
act6 = action6.Action6()
104104
act6.modify_bytes(param, 1, 4)
105105
actions.append(act6)
106-
bit_num = 0
106+
bit_num = expression.ConstantNumeric(0)
107107
comp_type = (1, r'\70') if expr.op == nmlop.HASBIT else (0, r'\71')
108108
return (param, actions, comp_type, bit_num , 1)
109109
elif expr.op in (nmlop.CMP_EQ, nmlop.CMP_NEQ, nmlop.CMP_LE, nmlop.CMP_GE) \
@@ -114,17 +114,17 @@ def parse_conditional(expr):
114114
else:
115115
param, actions = actionD.get_tmp_parameter(expr.expr1)
116116
op = op_to_cond_op(expr.op)
117-
return (param, actions, op, expr.expr2.value, 4)
117+
return (param, actions, op, expr.expr2, 4)
118118

119119
if isinstance(expr, expression.Boolean):
120120
expr = expr.expr
121121

122122
if isinstance(expr, expression.Not):
123123
param, actions = actionD.get_tmp_parameter(expr.expr)
124-
return (param, actions, (3, r'\7!'), 0, 4)
124+
return (param, actions, (3, r'\7!'), expression.ConstantNumeric(0), 4)
125125

126126
param, actions = actionD.get_tmp_parameter(expr)
127-
return (param, actions, (2, r'\7='), 0, 4)
127+
return (param, actions, (2, r'\7='), expression.ConstantNumeric(0), 4)
128128

129129
def cond_skip_actions(action_list, param, condtype, value, value_size, pos):
130130
if len(action_list) == 0: return []
@@ -239,7 +239,7 @@ def parse_conditional_block(cond_list):
239239
param = block['param_dst']
240240
if i == 0: action_list.extend(block['cond_actions'])
241241
else:
242-
action_list.extend(cond_skip_actions(block['cond_actions'], param_skip_all, (2, r'\7='), 0, 4, cond_list.pos))
242+
action_list.extend(cond_skip_actions(block['cond_actions'], param_skip_all, (2, r'\7='), expression.ConstantNumeric(0), 4, cond_list.pos))
243243
if param is None:
244244
param = param_skip_all
245245
else:

nml/actions/actionD.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,7 @@ def parse_special_check(assignment):
177177
check = assignment.value
178178
assert isinstance(check, expression.SpecialCheck)
179179
actions = parse_actionD(ParameterAssignment(assignment.param, expression.ConstantNumeric(check.results[0])))
180-
181-
value = check.value
182-
if check.mask is not None:
183-
value &= check.mask
184-
value += check.mask << 32
185-
assert check.varsize == 8
186-
else:
187-
assert check.varsize <= 4
188-
actions.append(nml.actions.action7.SkipAction(9, check.varnum, check.varsize, check.op, value, 1))
180+
actions.append(nml.actions.action7.SkipAction(9, check.varnum, check.varsize, check.op, check.value, 1))
189181

190182
actions.extend(parse_actionD(ParameterAssignment(assignment.param, expression.ConstantNumeric(check.results[1]))))
191183
return actions
@@ -396,7 +388,7 @@ def parse_actionD(assignment):
396388
action_list.extend(tmp_param_actions)
397389
param1 = expression.ConstantNumeric(0)
398390
param2 = expression.ConstantNumeric(0xFE)
399-
data = expression.ConstantNumeric(expression.parse_string_to_dword(assignment.value.grfid))
391+
data = expression.Label(assignment.value.grfid)
400392
elif isinstance(assignment.value, expression.PatchVariable):
401393
op = nmlop.ASSIGN
402394
param1 = expression.ConstantNumeric(assignment.value.num)

nml/actions/actionE.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def write(self, file):
2626
file.print_byte(len(self.grfid_list))
2727
for grfid in self.grfid_list:
2828
file.newline()
29-
file.print_dwordx(grfid)
29+
grfid.write(file, 4)
3030
file.newline()
3131
file.end_sprite()
3232

nml/ast/cargotable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def register_names(self):
2727
for i, cargo in enumerate(self.cargo_list):
2828
if isinstance(cargo, expression.Identifier):
2929
self.cargo_list[i] = expression.StringLiteral(cargo.value, cargo.pos)
30-
expression.parse_string_to_dword(self.cargo_list[i]) # we don't care about the result, only validate the input
30+
expression.Label(self.cargo_list[i]) # we don't care about the result, only validate the input
3131
if self.cargo_list[i].value in global_constants.cargo_numbers:
3232
generic.print_warning("Duplicate entry in cargo table: {}".format(self.cargo_list[i].value), cargo.pos)
3333
else:

nml/ast/deactivate.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ def __init__(self, grfid_list, pos):
2323
self.grfid_list = grfid_list
2424

2525
def pre_process(self):
26-
# Parse (string-)expressions to integers
27-
self.grfid_list = [expression.parse_string_to_dword(grfid.reduce()) for grfid in self.grfid_list]
26+
self.grfid_list = [expression.Label(grfid.reduce()) for grfid in self.grfid_list]
2827

2928
def debug_print(self, indentation):
3029
generic.print_dbg(indentation, 'Deactivate other newgrfs:')

nml/ast/grf.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
"""
2828
param_stats = [0, 0x40]
2929

30+
"""The grfid set in the `grf` block, if present"""
31+
GRFID = None
32+
3033
def print_stats():
3134
"""
3235
Print statistics about used ids.
@@ -105,7 +108,8 @@ def pre_process(self):
105108
raise generic.ScriptError("A GRF-block requires the 'name', 'desc', 'grfid', 'version' and 'min_compatible_version' properties to be set.", self.pos)
106109

107110
self.grfid = self.grfid.reduce()
108-
global_constants.constant_numbers['GRFID'] = expression.parse_string_to_dword(self.grfid)
111+
global GRFID
112+
GRFID = expression.Label(self.grfid)
109113
self.name = self.name.reduce()
110114
if not isinstance(self.name, expression.String):
111115
raise generic.ScriptError("GRF-name must be a string", self.name.pos)

0 commit comments

Comments
 (0)