fixed bug related to variables

This commit is contained in:
2025-10-04 20:39:15 +01:00
parent caf0a6a9eb
commit 59dd756ba1
2 changed files with 15 additions and 8 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
labubu
a.out

View File

@@ -286,17 +286,22 @@ labubu_ast* reduce (labubu_ast* ast, labubu_variables* env) {
res = lookup(
env,
ast->data.AST_VARIABLE.name);
if (res.found) {
if (res.value == NULL) return ast;
if (res.found && res.value != NULL) {
if (res.value->tag == AST_VARIABLE)
return reduce(res.value, env);
else return res.value;
}
return ast;
labubu_ast* ast1 = malloc(sizeof (labubu_ast));
*ast1 = *ast;
return ast1;
case AST_APPLICATION:
f = reduce(ast->data.AST_APPLICATION.f, env);
v = reduce(ast->data.AST_APPLICATION.v, env);
f = reduce(ast->data.AST_APPLICATION.f, env);
if (f->tag == AST_FUNCTION) {
push(env, f->data.AST_FUNCTION.lambda, v);
@@ -322,7 +327,7 @@ labubu_ast* reduce (labubu_ast* ast, labubu_variables* env) {
int main (void) {
labubu_lexer lexer = {
.tokens = malloc(1024 * sizeof (labubu_token)),
.rest = "((\\a.\\b.((a b) \\x.\\y.y) \\x.\\y.x) \\x.\\y.x)"
.rest = "((\\a.\\b.((a b) \\x.\\y.y) \\x.\\y.y) \\x.\\y.x)"
};
printf("%s = ", lexer.rest);
tokenize(&lexer);