diff options
| author | hachem <im@hachem.wtf> | 2026-09-19 14:49:36 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-09-19 14:49:36 +0200 |
| commit | e5cc80fb6a608aa1fbd0d6fabd1cf614a0fc004a (patch) | |
| tree | bf28325d56488de0cfe8cf4634b6307e229ea437 | |
| parent | b79902cd36b2c5b54bbf0f07078fc528754977b2 (diff) | |
fix: add a workaround for redeclaration
| -rw-r--r-- | ast.h | 1 | ||||
| -rw-r--r-- | eval.c | 14 | ||||
| -rw-r--r-- | examples/reassign.sfinae | 16 | ||||
| -rw-r--r-- | sfinae.l | 1 | ||||
| -rw-r--r-- | sfinae.y | 3 |
5 files changed, 35 insertions, 0 deletions
@@ -32,6 +32,7 @@ enum NodeKind COND_EQUAL, COND_LESS, STMT_DECLARE, + STMT_UNDECLARE, STMT_EMIT, STMT_STATIC_ASSERT }; @@ -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; @@ -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; @@ -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 |
