Skip to content

Commit

Permalink
fix: recursive lambda functions fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Apr 16, 2024
1 parent eafd9b1 commit 6b55493
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/bytecode/builtin_functions/DefineFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Bytecode::BuiltinFunctions {
std::vector<Instruction *> &instructions,
Segment *result) {
if (args[0]->type == SyntaxTreeNode::TokenNode) {
args[1]->compile(result, program, instructions);
const auto reg = program.declare_global(
((TokenNode *) args[0])->getName());
args[1]->compile(result, program, instructions);
instructions.push_back(new StoreGlobal(reg));
} else if (args[0]->type == SyntaxTreeNode::Expression) {
auto segment = new Segment({});
Expand Down
12 changes: 12 additions & 0 deletions tests/lambda_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,15 @@ TEST(lambda_test, ShouldPassLambdaAsArgument) {

EXPECT_EQ(actual_result, expected_result);
}

TEST(lambda_test, RecursiveLambdaFunction) {
const auto expression =
"(define fib (lambda (n)"
"(if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))))"
"(fib 10)";

const auto expected_result = StackObject((double) 55);
const auto actual_result = run_program(expression);

EXPECT_EQ(actual_result, expected_result);
}

0 comments on commit 6b55493

Please sign in to comment.