Skip to content

Commit c67bb85

Browse files
committed
fix: Detect static expressions better.
Now all non-static expressions are also allowed as initializers. They are just a hiden lazy LET
1 parent 3310645 commit c67bb85

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

src/api/check.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def check_call_arguments(lineno: int, id_: str, args: symbols.ARGLIST, filename:
160160
return False
161161

162162
if param.byref:
163-
if not isinstance(arg.value, (symbols.ID, symbols.ARRAYLOAD)):
163+
if not isinstance(arg.value, symbols.ID | symbols.ARRAYLOAD):
164164
errmsg.error(
165165
lineno, "Expected a variable name, not an expression (parameter By Reference)", fname=arg.filename
166166
)
@@ -310,11 +310,31 @@ def is_CONST(*p: symbols.SYMBOL) -> bool:
310310
return is_SYMBOL("CONSTEXPR", *p)
311311

312312

313+
def _is_static_unary(x: symbols.SYMBOL) -> bool:
314+
if x.token != "UNARY":
315+
return False
316+
317+
if x.operator != "ADDRESS":
318+
return False
319+
320+
if x.operand.token == "LABEL":
321+
return True
322+
323+
if x.operand.token in ("ID", "VAR"):
324+
return x.operand.scope == SCOPE.global_
325+
326+
if x.operand.token == "ARRAYACCESS":
327+
return is_static(*(arg.value for arg in x.operand.args))
328+
329+
return False
330+
331+
313332
def is_static(*p: symbols.SYMBOL) -> bool:
314333
"""A static value (does not change at runtime)
315334
which is known at compile time
316335
"""
317-
return all(is_CONST(x) or is_number(x) or is_const(x) for x in p)
336+
assert all(isinstance(x, symbols.SYMBOL) for x in p)
337+
return all(is_CONST(x) or is_number(x) or is_const(x) or _is_static_unary(x) for x in p)
318338

319339

320340
def is_number(*p):

src/zxbc/zxbparser.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def make_builtin(
246246
type_: sym.TYPE | None = None,
247247
) -> sym.BUILTIN | sym.NUMBER:
248248
"""Wrapper: returns a Builtin function node.
249-
Can be a Symbol, tuple or list of Symbols
249+
Can be a Symbol, tuple, or list of Symbols
250250
If operand is an iterable, they will be expanded.
251251
"""
252252
if operands is None:
@@ -736,9 +736,8 @@ def p_var_decl_ini(p):
736736
if expr is None:
737737
return
738738

739-
if not is_static(expr):
740-
if isinstance(expr, sym.UNARY):
741-
expr = make_constexpr(p.lineno(4), expr) # Delayed constant evaluation
739+
if is_static(expr) and isinstance(expr, sym.UNARY):
740+
expr = make_constexpr(p.lineno(4), expr) # Delayed constant evaluation
742741

743742
if typedef.implicit:
744743
typedef = sym.TYPEREF(expr.type_, p.lexer.lineno, implicit=True)

0 commit comments

Comments
 (0)