Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: handle labels more readably #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions nml/actions/action0.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,11 +722,11 @@ def get_tracktypelist_action(table_prop_id, cond_tracktype_not_defined, tracktyp
id_table.append(tracktype)
offset+=4
continue
param, extra_actions = actionD.get_tmp_parameter(expression.ConstantNumeric(expression.parse_string_to_dword(tracktype[-1])))
param, extra_actions = actionD.get_tmp_parameter(expression.Label(tracktype[-1]))
action_list.extend(extra_actions)
for idx in range(len(tracktype)-2, -1, -1):
val = expression.ConstantNumeric(expression.parse_string_to_dword(tracktype[idx]))
action_list.append(action7.SkipAction(0x09, 0x00, 4, (cond_tracktype_not_defined, None), val.value, 1))
val = expression.Label(tracktype[idx])
action_list.append(action7.SkipAction(0x09, 0x00, 4, (cond_tracktype_not_defined, None), val, 1))
action_list.append(actionD.ActionD(expression.ConstantNumeric(param), expression.ConstantNumeric(0xFF), nmlop.ASSIGN, expression.ConstantNumeric(0xFF), val))
act6.modify_bytes(param, 4, offset)
id_table.append(expression.StringLiteral(r"\00\00\00\00", None))
Expand Down Expand Up @@ -958,8 +958,8 @@ def __init__(self, source, target):

def write(self, file):
file.print_bytex(0x11)
file.print_dwordx(self.source)
file.print_dwordx(self.target)
self.source.write(file, 4)
self.target.write(file, 4)
file.newline()

def get_size(self):
Expand Down
5 changes: 2 additions & 3 deletions nml/actions/action0properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import itertools
from nml import generic, nmlop
from nml.expression import (BinOp, ConstantNumeric, ConstantFloat, Array, StringLiteral,
Identifier, ProduceCargo, AcceptCargo, parse_string_to_dword)
Identifier, ProduceCargo, AcceptCargo, Label)

tilelayout_names = {}

Expand Down Expand Up @@ -1036,8 +1036,7 @@ def write(self, file):
file.print_bytex(self.prop_num)
file.print_byte(len(self.labels))
for label in self.labels:
parse_string_to_dword(label) # Error if the wrong length or not ASCII
label.write(file, 4)
Label(label).write(file, 4)
file.newline()

def get_size(self):
Expand Down
4 changes: 2 additions & 2 deletions nml/actions/action11.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ImportSound(base_action.BaseAction):
<sprite-number> * <length> FE 00 <grfid> <number>

@ivar grfid: ID of the other grf.
@type grfid: C{int}
@type grfid: L{Label}

@ivar number: Sound number to load.
@type number: C{int}
Expand All @@ -76,7 +76,7 @@ def write(self, file):
file.start_sprite(8)
file.print_bytex(0xfe)
file.print_bytex(0)
file.print_dwordx(self.grfid)
self.grfid.write(file, 4)
file.print_wordx(self.number)
file.end_sprite()
if self.last: file.newline()
Expand Down
2 changes: 1 addition & 1 deletion nml/actions/action2var.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def preprocess_storageop(self, expr):

if expr.info['perm'] and self.feature == 0x08:
# store grfid in register 0x100 for town persistent storage
grfid = expression.ConstantNumeric(0xFFFFFFFF if expr.grfid is None else expression.parse_string_to_dword(expr.grfid))
grfid = expression.ConstantNumeric(0xFFFFFFFF) if expr.grfid is None else expression.Label(expr.grfid)
store_op = nmlop.STO_TMP(grfid, 0x100, expr.pos)
ret = nmlop.VAL2(store_op, ret)
elif expr.grfid is not None:
Expand Down
22 changes: 11 additions & 11 deletions nml/actions/action7.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ def write(self, file):
file.print_bytex(self.condtype[0], self.condtype[1])
if self.varsize == 8:
#grfid + mask
file.print_dwordx(self.value & 0xFFFFFFFF)
file.print_dwordx(self.value >> 32)
file.print_dwordx(self.value.value & 0xFFFFFFFF)
file.print_dwordx(self.value.value >> 32)
else:
file.print_varx(self.value, self.varsize)
self.value.write(file, self.varsize)
file.print_bytex(self.label)
file.newline()
file.end_sprite()
Expand All @@ -62,7 +62,7 @@ def skip_action9(self):

class UnconditionalSkipAction(SkipAction):
def __init__(self, action_type, label):
SkipAction.__init__(self, action_type, 0x9A, 1, (0, r'\71'), 0, label)
SkipAction.__init__(self, action_type, 0x9A, 1, (0, r'\71'), expression.ConstantNumeric(0), label)

def op_to_cond_op(op):
#The operators are reversed as we want to skip if the expression is true
Expand All @@ -84,7 +84,7 @@ def parse_conditional(expr):
- The size of the value (as integer)
'''
if expr is None:
return (None, [], (2, r'\7='), 0, 4)
return (None, [], (2, r'\7='), expression.ConstantNumeric(0), 4)
if isinstance(expr, expression.BinOp):
if expr.op == nmlop.HASBIT or expr.op == nmlop.NOTHASBIT:
if isinstance(expr.expr1, expression.Parameter) and isinstance(expr.expr1.num, expression.ConstantNumeric):
Expand All @@ -93,7 +93,7 @@ def parse_conditional(expr):
else:
param, actions = actionD.get_tmp_parameter(expr.expr1)
if isinstance(expr.expr2, expression.ConstantNumeric):
bit_num = expr.expr2.value
bit_num = expr.expr2
else:
if isinstance(expr.expr2, expression.Parameter) and isinstance(expr.expr2.num, expression.ConstantNumeric):
param = expr.expr2.num.value
Expand All @@ -103,7 +103,7 @@ def parse_conditional(expr):
act6 = action6.Action6()
act6.modify_bytes(param, 1, 4)
actions.append(act6)
bit_num = 0
bit_num = expression.ConstantNumeric(0)
comp_type = (1, r'\70') if expr.op == nmlop.HASBIT else (0, r'\71')
return (param, actions, comp_type, bit_num , 1)
elif expr.op in (nmlop.CMP_EQ, nmlop.CMP_NEQ, nmlop.CMP_LE, nmlop.CMP_GE) \
Expand All @@ -114,17 +114,17 @@ def parse_conditional(expr):
else:
param, actions = actionD.get_tmp_parameter(expr.expr1)
op = op_to_cond_op(expr.op)
return (param, actions, op, expr.expr2.value, 4)
return (param, actions, op, expr.expr2, 4)

if isinstance(expr, expression.Boolean):
expr = expr.expr

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

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

def cond_skip_actions(action_list, param, condtype, value, value_size, pos):
if len(action_list) == 0: return []
Expand Down Expand Up @@ -239,7 +239,7 @@ def parse_conditional_block(cond_list):
param = block['param_dst']
if i == 0: action_list.extend(block['cond_actions'])
else:
action_list.extend(cond_skip_actions(block['cond_actions'], param_skip_all, (2, r'\7='), 0, 4, cond_list.pos))
action_list.extend(cond_skip_actions(block['cond_actions'], param_skip_all, (2, r'\7='), expression.ConstantNumeric(0), 4, cond_list.pos))
if param is None:
param = param_skip_all
else:
Expand Down
12 changes: 2 additions & 10 deletions nml/actions/actionD.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,7 @@ def parse_special_check(assignment):
check = assignment.value
assert isinstance(check, expression.SpecialCheck)
actions = parse_actionD(ParameterAssignment(assignment.param, expression.ConstantNumeric(check.results[0])))

value = check.value
if check.mask is not None:
value &= check.mask
value += check.mask << 32
assert check.varsize == 8
else:
assert check.varsize <= 4
actions.append(nml.actions.action7.SkipAction(9, check.varnum, check.varsize, check.op, value, 1))
actions.append(nml.actions.action7.SkipAction(9, check.varnum, check.varsize, check.op, check.value, 1))

actions.extend(parse_actionD(ParameterAssignment(assignment.param, expression.ConstantNumeric(check.results[1]))))
return actions
Expand Down Expand Up @@ -396,7 +388,7 @@ def parse_actionD(assignment):
action_list.extend(tmp_param_actions)
param1 = expression.ConstantNumeric(0)
param2 = expression.ConstantNumeric(0xFE)
data = expression.ConstantNumeric(expression.parse_string_to_dword(assignment.value.grfid))
data = expression.Label(assignment.value.grfid)
elif isinstance(assignment.value, expression.PatchVariable):
op = nmlop.ASSIGN
param1 = expression.ConstantNumeric(assignment.value.num)
Expand Down
2 changes: 1 addition & 1 deletion nml/actions/actionE.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def write(self, file):
file.print_byte(len(self.grfid_list))
for grfid in self.grfid_list:
file.newline()
file.print_dwordx(grfid)
grfid.write(file, 4)
file.newline()
file.end_sprite()

Expand Down
2 changes: 1 addition & 1 deletion nml/ast/cargotable.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def register_names(self):
for i, cargo in enumerate(self.cargo_list):
if isinstance(cargo, expression.Identifier):
self.cargo_list[i] = expression.StringLiteral(cargo.value, cargo.pos)
expression.parse_string_to_dword(self.cargo_list[i]) # we don't care about the result, only validate the input
expression.Label(self.cargo_list[i]) # we don't care about the result, only validate the input
if self.cargo_list[i].value in global_constants.cargo_numbers:
generic.print_warning("Duplicate entry in cargo table: {}".format(self.cargo_list[i].value), cargo.pos)
else:
Expand Down
3 changes: 1 addition & 2 deletions nml/ast/deactivate.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def __init__(self, grfid_list, pos):
self.grfid_list = grfid_list

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

def debug_print(self, indentation):
generic.print_dbg(indentation, 'Deactivate other newgrfs:')
Expand Down
6 changes: 5 additions & 1 deletion nml/ast/grf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"""
param_stats = [0, 0x40]

"""The grfid set in the `grf` block, if present"""
GRFID = None

def print_stats():
"""
Print statistics about used ids.
Expand Down Expand Up @@ -105,7 +108,8 @@ def pre_process(self):
raise generic.ScriptError("A GRF-block requires the 'name', 'desc', 'grfid', 'version' and 'min_compatible_version' properties to be set.", self.pos)

self.grfid = self.grfid.reduce()
global_constants.constant_numbers['GRFID'] = expression.parse_string_to_dword(self.grfid)
global GRFID
GRFID = expression.Label(self.grfid)
self.name = self.name.reduce()
if not isinstance(self.name, expression.String):
raise generic.ScriptError("GRF-name must be a string", self.name.pos)
Expand Down
12 changes: 5 additions & 7 deletions nml/ast/override.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA."""

from nml import generic, expression, global_constants
from nml.ast import base_statement
from nml.ast import base_statement, grf
from nml.actions import action0

class EngineOverride(base_statement.BaseStatement):
Expand All @@ -36,15 +36,13 @@ def pre_process(self):
raise generic.ScriptError("engine_override expects 1 or 2 parameters", self.pos)

if len(self.args) == 1:
try:
self.source_grfid = expression.Identifier('GRFID').reduce(global_constants.const_list).value
assert isinstance(self.source_grfid, int)
except generic.ScriptError:
self.source_grfid = grf.GRFID
if self.source_grfid is None:
raise generic.ScriptError("GRFID of this grf is required, but no grf-block is defined.", self.pos)
else:
self.source_grfid = expression.parse_string_to_dword(self.args[0].reduce(global_constants.const_list))
self.source_grfid = expression.Label(self.args[0].reduce(global_constants.const_list))

self.grfid = expression.parse_string_to_dword(self.args[-1].reduce(global_constants.const_list))
self.grfid = expression.Label(self.args[-1].reduce(global_constants.const_list))

def debug_print(self, indentation):
generic.print_dbg(indentation, 'Engine override')
Expand Down
4 changes: 2 additions & 2 deletions nml/ast/tracktypetable.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ def register_names(self):
val_list.append(expression.StringLiteral(rt.value, rt.pos))
else:
val_list.append(rt)
expression.parse_string_to_dword(val_list[-1]) # we don't care about the result, only validate the input
expression.Label(val_list[-1]) # we don't care about the result, only validate the input
self.tracktype_list[i] = val_list if len(val_list) > 1 else val_list[0]
else:
name = tracktype
if isinstance(tracktype, expression.Identifier):
self.tracktype_list[i] = expression.StringLiteral(tracktype.value, tracktype.pos)
expression.parse_string_to_dword(self.tracktype_list[i]) # we don't care about the result, only validate the input
expression.Label(self.tracktype_list[i]) # we don't care about the result, only validate the input
self.tracktype_table[name.value] = i

def pre_process(self):
Expand Down
3 changes: 2 additions & 1 deletion nml/expression/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
from .functioncall import FunctionCall, SpecialCheck, GRMOp
from .functionptr import FunctionPtr
from .identifier import Identifier
from .label import Label
from .patch_variable import PatchVariable
from .parameter import Parameter, OtherGRFParameter, parse_string_to_dword
from .parameter import Parameter, OtherGRFParameter
from .special_parameter import SpecialParameter
from .spritegroup_ref import SpriteGroupRef
from .storage_op import StorageOp
Expand Down
Loading