From e5cc80fb6a608aa1fbd0d6fabd1cf614a0fc004a Mon Sep 17 00:00:00 2001 From: hachem Date: Sat, 19 Sep 2026 14:49:36 +0200 Subject: fix: add a workaround for redeclaration --- ast.h | 1 + eval.c | 14 ++++++++++++++ examples/reassign.sfinae | 16 ++++++++++++++++ sfinae.l | 1 + sfinae.y | 3 +++ 5 files changed, 35 insertions(+) create mode 100644 examples/reassign.sfinae diff --git a/ast.h b/ast.h index d17eaef..aaa5f7a 100644 --- a/ast.h +++ b/ast.h @@ -32,6 +32,7 @@ enum NodeKind COND_EQUAL, COND_LESS, STMT_DECLARE, + STMT_UNDECLARE, STMT_EMIT, STMT_STATIC_ASSERT }; diff --git a/eval.c b/eval.c index fc6e6d7..cb967c2 100644 --- a/eval.c +++ b/eval.c @@ -222,6 +222,19 @@ static enum RunStatus declare(struct Machine* machine, const struct Node* node) return RUN_DONE; } +static enum RunStatus undeclare(struct Machine* machine, const struct Node* node) +{ + struct Variable* variable = find_variable(machine, node->name); + if (variable == NULL) + { + (void)complain(node, "cannot undeclare what was never declared"); + return RUN_FAILED; + } + + variable->declared = false; + return RUN_DONE; +} + static enum RunStatus emit(struct Machine* machine, const struct Node* node) { struct Type type; @@ -261,6 +274,7 @@ static enum RunStatus execute_one(struct Machine* machine, const struct Node* no switch (node->kind) { case STMT_DECLARE: return declare(machine, node); + case STMT_UNDECLARE: return undeclare(machine, node); case STMT_EMIT: return emit(machine, node); case STMT_STATIC_ASSERT: return static_assert_statement(machine, node); default: break; diff --git a/examples/reassign.sfinae b/examples/reassign.sfinae new file mode 100644 index 0000000..9863cae --- /dev/null +++ b/examples/reassign.sfinae @@ -0,0 +1,16 @@ +// there is no assignment. to change x, undeclare it and declare it again +// if the new value depends on the old one, park it in a second variable first, +// because the old one is gone by the time you declare + +::declare<::name<'x'>, ::decltype(::difference<'F', 'A'>)>::commit; +::emit<::decltype(::value_of<::name<'x'>>)>::commit; + +::declare< + ::name<'n', 'e', 'x', 't'>, + ::decltype(::sum<::value_of<::name<'x'>>, ::difference<'B', 'A'>>) +>::commit; +::undeclare<::name<'x'>>::commit; +::declare<::name<'x'>, ::decltype(::value_of<::name<'n', 'e', 'x', 't'>>)>::commit; +::undeclare<::name<'n', 'e', 'x', 't'>>::commit; + +::emit<::decltype(::value_of<::name<'x'>>)>::commit; diff --git a/sfinae.l b/sfinae.l index 7c745ef..17bf28e 100644 --- a/sfinae.l +++ b/sfinae.l @@ -36,6 +36,7 @@ static int fail(const char* message) "static_assert" return TOK_STATIC_ASSERT; "commit" return TOK_COMMIT; "declare" return TOK_DECLARE; +"undeclare" return TOK_UNDECLARE; "name" return TOK_NAME; "value_of" return TOK_VALUE_OF; "void" return TOK_VOID; diff --git a/sfinae.y b/sfinae.y index e142aaa..1acb4d0 100644 --- a/sfinae.y +++ b/sfinae.y @@ -22,6 +22,7 @@ void yyerror(const char* message); %token TOK_STATIC_ASSERT %token TOK_COMMIT %token TOK_DECLARE +%token TOK_UNDECLARE %token TOK_NAME %token TOK_VALUE_OF %token TOK_VOID @@ -72,6 +73,8 @@ statement { $$ = node_unary(STMT_STATIC_ASSERT, $4); } | TOK_COLONCOLON TOK_DECLARE '<' name ',' type '>' TOK_COLONCOLON TOK_COMMIT { $$ = node_named(STMT_DECLARE, &$4, $6); } + | TOK_COLONCOLON TOK_UNDECLARE '<' name '>' TOK_COLONCOLON TOK_COMMIT + { $$ = node_named(STMT_UNDECLARE, &$4, NULL); } ; name -- cgit v1.3