Fix 'when' expressions

This commit is contained in:
Bruce Hill 2025-03-17 18:34:37 -04:00
parent 3eae595c17
commit 748a3c0cf8

View File

@ -781,7 +781,7 @@ static CORD _compile_statement(env_t *env, ast_t *ast)
}
auto enum_t = Match(subject_t, EnumType);
CORD code = CORD_all("WHEN(", compile(env, when->subject), ", when, {\n");
CORD code = CORD_all("WHEN(", compile(env, when->subject), ", _when_subject, {\n");
for (when_clause_t *clause = when->clauses; clause; clause = clause->next) {
if (clause->pattern->tag == Var) {
const char *clause_tag_name = Match(clause->pattern, Var)->name;
@ -813,7 +813,7 @@ static CORD _compile_statement(env_t *env, ast_t *ast)
code_err(args->value, "This is not a valid variable to bind to");
const char *var_name = Match(args->value, Var)->name;
if (!streq(var_name, "_")) {
code = CORD_all(code, compile_declaration(tag_type, compile(env, args->value)), " = when.", clause_tag_name, ";\n");
code = CORD_all(code, compile_declaration(tag_type, compile(env, args->value)), " = _when_subject.", clause_tag_name, ";\n");
scope = fresh_scope(scope);
set_binding(scope, Match(args->value, Var)->name, tag_type, CORD_EMPTY);
}
@ -830,7 +830,7 @@ static CORD _compile_statement(env_t *env, ast_t *ast)
const char *var_name = Match(arg->value, Var)->name;
if (!streq(var_name, "_")) {
code = CORD_all(code, compile_declaration(field->type, compile(env, arg->value)), " = when.", clause_tag_name, ".", field->name, ";\n");
code = CORD_all(code, compile_declaration(field->type, compile(env, arg->value)), " = _when_subject.", clause_tag_name, ".", field->name, ";\n");
set_binding(scope, Match(arg->value, Var)->name, field->type, CORD_EMPTY);
}
field = field->next;