From 13a8895fca82f982229247c668a1e95084715666 Mon Sep 17 00:00:00 2001 From: Sylvie <35663410+Rangi42@users.noreply.github.com> Date: Thu, 25 Jul 2024 17:36:02 -0400 Subject: [PATCH] Improve the error messages for interpolating undefined or invalid symbols (#1423) --- src/asm/lexer.cpp | 4 ++-- test/asm/interpolation.asm | 11 ++++++++++- test/asm/interpolation.err | 10 +++++++--- test/asm/interpolation.out | 3 +++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 2d04f78d..37fa3ace 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -1208,7 +1208,7 @@ static std::shared_ptr readInterpolation(size_t depth) { Symbol const *sym = sym_FindScopedValidSymbol(fmtBuf); - if (!sym) { + if (!sym || !sym->isDefined()) { error("Interpolated symbol \"%s\" does not exist\n", fmtBuf.c_str()); } else if (sym->type == SYM_EQUS) { auto buf = std::make_shared(); @@ -1219,7 +1219,7 @@ static std::shared_ptr readInterpolation(size_t depth) { fmt.appendNumber(*buf, sym->getConstantValue()); return buf; } else { - error("Only numerical and string symbols can be interpolated\n"); + error("Interpolated symbol \"%s\" is not a numeric or string symbol\n", fmtBuf.c_str()); } return nullptr; } diff --git a/test/asm/interpolation.asm b/test/asm/interpolation.asm index 7dc9a321..90cda992 100644 --- a/test/asm/interpolation.asm +++ b/test/asm/interpolation.asm @@ -1,4 +1,4 @@ -SECTION "Test", ROM0 +SECTION "Test", ROM0[123] def NAME equs "ITEM" def FMT equs "d" @@ -14,6 +14,15 @@ PRINTLN STRCAT("{NAME}_{d:INDEX}", " is ", {NAME}_{d:INDEX}) PURGE {NAME}_{d:INDEX} ASSERT !DEF({NAME}_{d:INDEX}) +; undefined +PRINTLN "undef {undef}" + +; referenced but undefined +ld hl, label +PRINTLN "label {label}" +label:: +PRINTLN "label {label}" + ; not string or number MACRO foo ENDM diff --git a/test/asm/interpolation.err b/test/asm/interpolation.err index 03a5a4ee..6622a426 100644 --- a/test/asm/interpolation.err +++ b/test/asm/interpolation.err @@ -1,3 +1,7 @@ -error: interpolation.asm(20): - Only numerical and string symbols can be interpolated -error: Assembly aborted (1 error)! +error: interpolation.asm(18): + Interpolated symbol "undef" does not exist +error: interpolation.asm(22): + Interpolated symbol "label" does not exist +error: interpolation.asm(29): + Interpolated symbol "foo" is not a numeric or string symbol +error: Assembly aborted (3 errors)! diff --git a/test/asm/interpolation.out b/test/asm/interpolation.out index a764c4f2..32890c61 100644 --- a/test/asm/interpolation.out +++ b/test/asm/interpolation.out @@ -1,2 +1,5 @@ ITEM_100 is hundredth +undef +label +label $7E foo