Skip to content

Commit

Permalink
fix: recursive function calls fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Jan 29, 2024
1 parent c9df8c1 commit a2566d5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/bytecode/builtin_functions/DefineFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ namespace Bytecode::BuiltinFunctions {
result.push_back(new Store(reg));
} else {
auto segment = new Segment({});
compiler.program.declare_function(*args[0]->token->asString(), segment);
for (auto argument: args[0]->children)
compiler.program.declare_variable(*argument->token->asString());
compiler.compile(*args[1], segment->instructions);
compiler.program.declare_function(*args[0]->token->asString(), segment);

}
}
};
Expand Down
69 changes: 69 additions & 0 deletions tests/bytecode/compiler/functions_test.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#include "bytecode/compiler/Segment.h"
#include "bytecode/instructions/Add.h"
#include "bytecode/instructions/Call.h"
#include "bytecode/instructions/CondJumpIfNot.h"
#include "bytecode/instructions/Equals.h"
#include "bytecode/instructions/Jump.h"
#include "bytecode/instructions/Load.h"
#include "bytecode/instructions/LoadLiteral.h"
#include "bytecode/instructions/Multiply.h"
#include "bytecode/instructions/Subtract.h"
#include "parser/SyntaxTreeNode.h"
#include <gtest/gtest.h>

Expand Down Expand Up @@ -128,3 +132,68 @@ TEST(compiler_functions, SimpleFunctionCall) {

EXPECT_EQ(expected_result == compiler.program, true);
}

TEST(compiler_functions, RecursiveFunction) {
// (define (sum n) (if (= n 1) 1 (+ n (sum (- n 1)))))
const auto expression = SyntaxTreeNode(
new Token(Token::Symbol, "define"),
{
new SyntaxTreeNode(
new Token(Token::Symbol, "sum"),
{
new SyntaxTreeNode(new Token(Token::Symbol, "n")),
}),
new SyntaxTreeNode(
new Token(Token::Symbol, "if"),
{
new SyntaxTreeNode(
new Token(Token::Symbol, "="),
{
new SyntaxTreeNode(new Token(Token::Symbol, "n")),
new SyntaxTreeNode(new Token(1)),
}),
new SyntaxTreeNode(new Token(1)),
new SyntaxTreeNode(
new Token(Token::Symbol, "+"),
{
new SyntaxTreeNode(new Token(Token::Symbol, "n")),
new SyntaxTreeNode(
new Token(Token::Symbol, "sum"),
{
new SyntaxTreeNode(
new Token(Token::Symbol, "-"),
{
new SyntaxTreeNode(new Token(Token::Symbol, "n")),
new SyntaxTreeNode(new Token(1)),
}),
}),
}),
}),
});

auto expected_result = Program(
{{"sum", 1}},
{{"n", 0}},
{
new Segment({}),
new Segment({
new Load(0),
new LoadLiteral(1),
new Equals(),
new CondJumpIfNot(6),
new LoadLiteral(1),
new Jump(12),
new Load(0),
new Load(0),
new LoadLiteral(1),
new Subtract(),
new Call(1),
new Add(),
}),
});

Compiler compiler = Compiler();
compiler.compile(expression);

EXPECT_EQ(expected_result == compiler.program, true);
}

0 comments on commit a2566d5

Please sign in to comment.