diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b9eb4e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +labubu +a.out diff --git a/labubu.c b/labubu.c index 6f6fff5..4b885bb 100644 --- a/labubu.c +++ b/labubu.c @@ -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; - return reduce(res.value, env); - } - return ast; + + if (res.found && res.value != NULL) { + if (res.value->tag == AST_VARIABLE) + return reduce(res.value, env); + else return res.value; + } + + 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);