Skip to content

Commit b863619

Browse files
committed
Do not allow initialization of arrays of strings
1 parent 265ba33 commit b863619

5 files changed

Lines changed: 35 additions & 5 deletions

File tree

src/api/errmsg.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,12 @@ def syntax_error_cannot_pass_array_by_value(lineno: int, id_: str):
307307
def syntax_error_no_data_defined(lineno: int):
308308
error(lineno, "No DATA defined")
309309

310+
311+
# ----------------------------------------
312+
# Cannot pass an array by value
313+
# ----------------------------------------
314+
def syntax_error_cannot_initialize_array_of_type(lineno: int, type_name: str):
315+
error(lineno, f"Cannot initialize array of type {type_name}")
316+
317+
310318
# endregion

src/zxbc/zxbparser.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,12 +848,22 @@ def check_bound(boundlist, remaining):
848848
return True
849849

850850
p[0] = None
851-
if p[8] is None:
851+
if p[4] is None or p[6] is None or p[8] is None:
852852
return
853853

854-
if check_bound(p[4].children, p[8]):
855-
id_, lineno = p[2][0]
856-
SYMBOL_TABLE.declare_array(id_, lineno, p[6], p[4], default_value=p[8])
854+
if not check_bound(p[4].children, p[8]):
855+
return
856+
857+
id_, lineno = p[2][0]
858+
SYMBOL_TABLE.declare_array(id_, lineno, p[6], p[4], default_value=p[8])
859+
860+
entry = SYMBOL_TABLE.get_entry(id_)
861+
if entry is None:
862+
return
863+
864+
if p[6] == TYPE.string or entry.type_ == TYPE.string:
865+
src.api.errmsg.syntax_error_cannot_initialize_array_of_type(p.lineno(1), TYPE.string)
866+
return
857867

858868

859869
def p_bound_list(p):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
DIM a(2) as String => {0, 1, 2}
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
DIM a$(2) => {0, 1, 2}
4+

tests/functional/test_errmsg.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ poke5.bas:4: error: Syntax Error. Unexpected token ',' <COMMA>
5252
arrlabels10.bas:3: warning: Using default implicit type 'float' for 'a'
5353
arrlabels10.bas:3: error: Can't convert non-numeric value to float at compile time
5454
>>> process_file('arrlabels10c.bas')
55-
arrlabels10c.bas:3: error: Can't convert non-numeric value to string at compile time
55+
arrlabels10c.bas:3: error: Cannot initialize array of type string
5656
>>> process_file('arrlabels10d.bas')
5757
arrlabels10d.bas:3: error: Undeclared array "a"
5858
>>> process_file('arrlabels11.bas')
@@ -201,6 +201,10 @@ bad_pragma.bas:6: warning: Ignoring unknown pragma 'BAD_PRAGMA'
201201
>>> process_file('opt2_global_array2.bas')
202202
opt2_global_array2.bas:1: warning: Variable 'myArray' is never used
203203
>>> process_file('end.bas')
204+
>>> process_file('dim_str_error0.bas')
205+
dim_str_error0.bas:3: error: Cannot initialize array of type string
206+
>>> process_file('dim_str_error1.bas')
207+
dim_str_error1.bas:3: error: Cannot initialize array of type string
204208

205209
# Test warning silencing
206210
>>> process_file('mcleod3.bas', ['-S', '-q', '-O --expect-warnings=2'])

0 commit comments

Comments
 (0)