From 919e47a4188acc1da711ac05e5aa097a3b1349f7 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sat, 30 Nov 2024 15:54:21 -0500 Subject: [PATCH] Better error messages for `table[key] += ...` --- compile.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/compile.c b/compile.c index 8244992..ee06e95 100644 --- a/compile.c +++ b/compile.c @@ -618,6 +618,13 @@ CORD compile_statement(env_t *env, ast_t *ast) } else if (test->expr->tag == UpdateAssign) { type_t *lhs_t = get_type(env, Match(test->expr, UpdateAssign)->lhs); auto update = Match(test->expr, UpdateAssign); + + if (update->lhs->tag == Index) { + type_t *indexed = value_type(get_type(env, Match(update->lhs, Index)->indexed)); + if (indexed->tag == TableType) + code_err(update->lhs, "Update assignments are not currently supported for tables"); + } + ast_t *update_var = WrapAST(ast, UpdateAssign, .lhs=WrapAST(update->lhs, InlineCCode, .code="(*expr)", .type=lhs_t), .op=update->op, .rhs=update->rhs); @@ -692,6 +699,13 @@ CORD compile_statement(env_t *env, ast_t *ast) } case UpdateAssign: { auto update = Match(ast, UpdateAssign); + + if (update->lhs->tag == Index) { + type_t *indexed = value_type(get_type(env, Match(update->lhs, Index)->indexed)); + if (indexed->tag == TableType) + code_err(update->lhs, "Update assignments are not currently supported for tables"); + } + if (!is_idempotent(update->lhs)) { type_t *lhs_t = get_type(env, update->lhs); return CORD_all("{ ", compile_declaration(Type(PointerType, lhs_t), "update_lhs"), " = &",