Skip to content

Commit

Permalink
Merge pull request #903 from boriel-basic/fix/READ_without_data_shoul…
Browse files Browse the repository at this point in the history
…d_fail_gracefully

fix: must show error when using READ with no DATA
  • Loading branch information
boriel authored Nov 19, 2024
2 parents 247e68e + 996bcde commit ea663ab
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 1,159 deletions.
4 changes: 4 additions & 0 deletions src/arch/z80/visitor/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ def visit_RESTORE(self, node):
self.runtime_call(RuntimeLabel.RESTORE, 0)

def visit_READ(self, node):
if not gl.DATAS:
src.api.errmsg.syntax_error_no_data_defined(node.lineno)
return

self.ic_fparam(TYPE.ubyte, "#" + str(self.DATA_TYPES[self.TSUFFIX(node.args[0].type_)]))
self.runtime_call(RuntimeLabel.READ, node.args[0].type_.size)

Expand Down
11 changes: 4 additions & 7 deletions src/arch/z80/visitor/translator_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def runtime_call(self, label: str, num: int):

# This function must be called before emit_strings
def emit_data_blocks(self):
if not gl.DATA_IS_USED:
if not gl.DATA_IS_USED or not gl.DATAS:
return # nothing to do

for label_, datas in gl.DATAS:
Expand All @@ -139,12 +139,9 @@ def emit_data_blocks(self):
else:
self.ic_data(d.value.type_, [self.traverse_const(d.value)])

if not gl.DATAS: # The above loop was not executed, because there's no data
self.ic_label("__DATA__0")
else:
missing_data_labels = set(gl.DATA_LABELS_REQUIRED).difference([x.label.name for x in gl.DATAS])
for data_label in missing_data_labels:
self.ic_label(data_label) # A label reference by a RESTORE beyond the last DATA line
missing_data_labels = set(gl.DATA_LABELS_REQUIRED).difference([x.label.name for x in gl.DATAS])
for data_label in missing_data_labels:
self.ic_label(data_label) # A label reference by a RESTORE beyond the last DATA line

self.ic_vard("__DATA__END", ["00"])

Expand Down
8 changes: 5 additions & 3 deletions tests/functional/arch/zx48k/arrbase1.asm
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ _c.__LBOUND__:
pop ix
ei
ret
__DATA__0:
.DATA.__DATA__0:
DEFB 3
DEFB 1
__DATA__END:
DEFB 00h
;; --- end of user code ---
Expand Down Expand Up @@ -247,7 +249,7 @@ __FNMUL2:
ret
ENDP
pop namespace
#line 46 "arch/zx48k/arrbase1.bas"
#line 48 "arch/zx48k/arrbase1.bas"
#line 1 "/zxbasic/src/lib/arch/zx48k/runtime/read_restore.asm"
;; This implements READ & RESTORE functions
;; Reads a new element from the DATA Address code
Expand Down Expand Up @@ -1344,5 +1346,5 @@ __DATA_ADDR: ;; Stores current DATA ptr
dw .DATA.__DATA__0
ENDP
pop namespace
#line 47 "arch/zx48k/arrbase1.bas"
#line 49 "arch/zx48k/arrbase1.bas"
END
2 changes: 2 additions & 0 deletions tests/functional/arch/zx48k/arrbase1.bas
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ DIM k as Uinteger
FOR k=1 TO 2
READ c(k, 1)
NEXT k

DATA 1
Loading

0 comments on commit ea663ab

Please sign in to comment.