Skip to content
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
24 changes: 22 additions & 2 deletions src/api/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def check_call_arguments(lineno: int, id_: str, args: symbols.ARGLIST, filename:
return False

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


def _is_static_unary(x: symbols.SYMBOL) -> bool:
if x.token != "UNARY":
return False

if x.operator != "ADDRESS":
return False

if x.operand.token == "LABEL":
return True

if x.operand.token in ("ID", "VAR"):
return x.operand.scope == SCOPE.global_

if x.operand.token == "ARRAYACCESS":
return is_static(*(arg.value for arg in x.operand.args))

return False


def is_static(*p: symbols.SYMBOL) -> bool:
"""A static value (does not change at runtime)
which is known at compile time
"""
return all(is_CONST(x) or is_number(x) or is_const(x) for x in p)
assert all(isinstance(x, symbols.SYMBOL) for x in p)
return all(is_CONST(x) or is_number(x) or is_const(x) or _is_static_unary(x) for x in p)


def is_number(*p):
Expand Down
7 changes: 3 additions & 4 deletions src/zxbc/zxbparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def make_builtin(
type_: sym.TYPE | None = None,
) -> sym.BUILTIN | sym.NUMBER:
"""Wrapper: returns a Builtin function node.
Can be a Symbol, tuple or list of Symbols
Can be a Symbol, tuple, or list of Symbols
If operand is an iterable, they will be expanded.
"""
if operands is None:
Expand Down Expand Up @@ -736,9 +736,8 @@ def p_var_decl_ini(p):
if expr is None:
return

if not is_static(expr):
if isinstance(expr, sym.UNARY):
expr = make_constexpr(p.lineno(4), expr) # Delayed constant evaluation
if is_static(expr) and isinstance(expr, sym.UNARY):
expr = make_constexpr(p.lineno(4), expr) # Delayed constant evaluation

if typedef.implicit:
typedef = sym.TYPEREF(expr.type_, p.lexer.lineno, implicit=True)
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/arch/zx48k/dim_at0.bas
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
declare sub test2

sub test1()
DIM dummy as Uinteger = @test2
end sub
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/arch/zx48k/dim_at1.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sub test1()
DIM dummy as Uinteger = @test2
end sub

sub test2
end sub
3 changes: 1 addition & 2 deletions tests/functional/arch/zx48k/dimconst2.bas
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Map:

Function q
DIM q as Uinteger = @Map
End Function

Map:
62 changes: 62 additions & 0 deletions tests/functional/arch/zx48k/dimconst2b.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
org 32768
.core.__START_PROGRAM:
di
push ix
push iy
exx
push hl
exx
ld (.core.__CALL_BACK__), sp
ei
jp .core.__MAIN_PROGRAM__
.core.__CALL_BACK__:
DEFW 0
.core.ZXBASIC_USER_DATA:
; Defines USER DATA Length in bytes
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
_Map:
DEFB 00
.core.ZXBASIC_USER_DATA_END:
.core.__MAIN_PROGRAM__:
ld a, 2
ld (_Map), a
ld hl, 0
ld b, h
ld c, l
.core.__END_PROGRAM:
di
ld hl, (.core.__CALL_BACK__)
ld sp, hl
exx
pop hl
exx
pop iy
pop ix
ei
ret
_q:
push ix
ld ix, 0
add ix, sp
ld hl, 0
push hl
push ix
pop hl
ld bc, -2
add hl, bc
ex de, hl
ld hl, .LABEL.__LABEL0
ld bc, 2
ldir
ld a, 1
ld (_Map), a
_q__leave:
ld sp, ix
pop ix
ret
;; --- end of user code ---
.LABEL.__LABEL0:
DEFW _Map
END
4 changes: 2 additions & 2 deletions tests/functional/arch/zx48k/dimconst2b.bas
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Map = 2

Function q
DIM q as Uinteger = @Map
Map = 1
End Function

Map = 2
4 changes: 2 additions & 2 deletions tests/functional/arch/zx48k/dimconst2c.bas
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Let Map = 1

Function q
DIM q as Uinteger = @Map
End Function

Let Map = 1
4 changes: 2 additions & 2 deletions tests/functional/arch/zx48k/dimconst2d.bas
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Dim Map = 1

Function q
DIM q as Uinteger = @Map
End Function

Dim Map = 1
50 changes: 50 additions & 0 deletions tests/functional/arch/zx48k/dimconst2f.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
org 32768
.core.__START_PROGRAM:
di
push ix
push iy
exx
push hl
exx
ld (.core.__CALL_BACK__), sp
ei
jp .core.__MAIN_PROGRAM__
.core.__CALL_BACK__:
DEFW 0
.core.ZXBASIC_USER_DATA:
; Defines USER DATA Length in bytes
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
.core.ZXBASIC_USER_DATA_END:
.core.__MAIN_PROGRAM__:
.LABEL._Map:
ld hl, 0
ld b, h
ld c, l
.core.__END_PROGRAM:
di
ld hl, (.core.__CALL_BACK__)
ld sp, hl
exx
pop hl
exx
pop iy
pop ix
ei
ret
_q:
push ix
ld ix, 0
add ix, sp
ld hl, 0
push hl
ld hl, .LABEL._Map
ld (ix-2), l
ld (ix-1), h
_q__leave:
ld sp, ix
pop ix
ret
;; --- end of user code ---
END
6 changes: 6 additions & 0 deletions tests/functional/arch/zx48k/dimconst2f.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

Function q
DIM q as Uinteger = @Map
End Function

Map:
59 changes: 59 additions & 0 deletions tests/functional/arch/zx48k/dimconst2g.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
org 32768
.core.__START_PROGRAM:
di
push ix
push iy
exx
push hl
exx
ld (.core.__CALL_BACK__), sp
ei
jp .core.__MAIN_PROGRAM__
.core.__CALL_BACK__:
DEFW 0
.core.ZXBASIC_USER_DATA:
; Defines USER DATA Length in bytes
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
_Map:
DEFB 00
.core.ZXBASIC_USER_DATA_END:
.core.__MAIN_PROGRAM__:
ld a, 2
ld (_Map), a
ld hl, 0
ld b, h
ld c, l
.core.__END_PROGRAM:
di
ld hl, (.core.__CALL_BACK__)
ld sp, hl
exx
pop hl
exx
pop iy
pop ix
ei
ret
_q:
push ix
ld ix, 0
add ix, sp
ld hl, 0
push hl
push hl
inc sp
push ix
pop hl
ld de, -1
add hl, de
ld (ix-3), l
ld (ix-2), h
ld (ix-1), 1
_q__leave:
ld sp, ix
pop ix
ret
;; --- end of user code ---
END
7 changes: 7 additions & 0 deletions tests/functional/arch/zx48k/dimconst2g.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

Function q
DIM q as Uinteger = @Map
Map = 1
End Function

Map = 2
51 changes: 51 additions & 0 deletions tests/functional/arch/zx48k/dimconst2h.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
org 32768
.core.__START_PROGRAM:
di
push ix
push iy
exx
push hl
exx
ld (.core.__CALL_BACK__), sp
ei
jp .core.__MAIN_PROGRAM__
.core.__CALL_BACK__:
DEFW 0
.core.ZXBASIC_USER_DATA:
; Defines USER DATA Length in bytes
.core.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_END - .core.ZXBASIC_USER_DATA
.core.__LABEL__.ZXBASIC_USER_DATA_LEN EQU .core.ZXBASIC_USER_DATA_LEN
.core.__LABEL__.ZXBASIC_USER_DATA EQU .core.ZXBASIC_USER_DATA
_Map:
DEFB 01h
.core.ZXBASIC_USER_DATA_END:
.core.__MAIN_PROGRAM__:
ld hl, 0
ld b, h
ld c, l
.core.__END_PROGRAM:
di
ld hl, (.core.__CALL_BACK__)
ld sp, hl
exx
pop hl
exx
pop iy
pop ix
ei
ret
_q:
push ix
ld ix, 0
add ix, sp
ld hl, 0
push hl
ld hl, _Map
ld (ix-2), l
ld (ix-1), h
_q__leave:
ld sp, ix
pop ix
ret
;; --- end of user code ---
END
6 changes: 6 additions & 0 deletions tests/functional/arch/zx48k/dimconst2h.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

Function q
DIM q as Uinteger = @Map
End Function

Dim Map = 1
Loading
Loading