Skip to content
This repository has been archived by the owner on Jun 29, 2024. It is now read-only.

Commit

Permalink
Add return type to Function
Browse files Browse the repository at this point in the history
  • Loading branch information
Gashmob committed Aug 6, 2023
1 parent 9b19eaa commit c580b3c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/lib/ast/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,18 @@ namespace filc::ast {

class Function : public AbstractExpression {
public:
Function(Identifier *name, const std::vector<FunctionParameter *> &parameters);
Function(Identifier *name, const std::vector<FunctionParameter *> &parameters, AbstractType *return_type);

[[nodiscard]] auto getName() const -> Identifier *;

[[nodiscard]] auto getParameters() const -> const std::vector<FunctionParameter *> &;

[[nodiscard]] auto getReturnType() const -> AbstractType *;

private:
Identifier *_name;
std::vector<FunctionParameter *> _parameters;
AbstractType *_return_type;
};

class FunctionParameter {
Expand Down
8 changes: 6 additions & 2 deletions src/lib/ast/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#include "AST.h"

namespace filc::ast {
Function::Function(Identifier *name, const std::vector<FunctionParameter *> &parameters)
: AbstractExpression(), _name(name), _parameters(parameters) {}
Function::Function(Identifier *name, const std::vector<FunctionParameter *> &parameters, AbstractType *return_type)
: AbstractExpression(), _name(name), _parameters(parameters), _return_type(return_type) {}

auto Function::getName() const -> Identifier * {
return _name;
Expand All @@ -34,4 +34,8 @@ namespace filc::ast {
auto Function::getParameters() const -> const std::vector<FunctionParameter *> & {
return _parameters;
}

auto Function::getReturnType() const -> AbstractType * {
return _return_type;
}
}
14 changes: 9 additions & 5 deletions src/lib/grammar/FilParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -290,18 +290,20 @@ binary_operator returns[filc::ast::Operator *tree]

function returns[filc::ast::Function *tree]
: fd=function_declaration function_body {
$tree = new filc::ast::Function($fd.identifier, $fd.parameters);
$tree = new filc::ast::Function($fd.identifier, $fd.parameters, $fd.return_type);
};

function_declaration returns[filc::ast::Identifier *identifier, std::vector<filc::ast::FunctionParameter *> parameters]
function_declaration returns[filc::ast::Identifier *identifier, std::vector<filc::ast::FunctionParameter *> parameters, filc::ast::AbstractType *return_type]
@init {
$parameters = std::vector<filc::ast::FunctionParameter *>();
}
: FUN fi=function_identifier {
$identifier = $fi.tree;
} LPAREN (fp=function_parameters {
$parameters = $fp.tree;
})? RPAREN function_type;
})? RPAREN ft=function_type {
$return_type = $ft.tree;
};

function_identifier returns[filc::ast::Identifier *tree]
: OPERATOR fo=function_operator {
Expand Down Expand Up @@ -348,8 +350,10 @@ function_parameter returns[filc::ast::FunctionParameter *tree]
$tree = new filc::ast::FunctionParameter(new filc::ast::Identifier($i), $t.tree);
};

function_type
: COLON type;
function_type returns[filc::ast::AbstractType *tree]
: COLON t=type {
$tree = $t.tree;
};

function_body
: assignation | parenthesis_body | block_body;
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/ast/FunctionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
using namespace ::testing;

TEST(Function, constructor) {
filc::ast::Function fun1(new filc::ast::Identifier("fact"), {});
filc::ast::Function fun1(
new filc::ast::Identifier("fact"),
{},
new filc::ast::Type(new filc::ast::Identifier("int"))
);
ASSERT_IDENTIFIER("fact", fun1.getName());
ASSERT_THAT(fun1.getParameters(), IsEmpty());
ASSERT_TYPE("int", fun1.getReturnType());
}
2 changes: 2 additions & 0 deletions tests/unit/grammar/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ TEST(Parser, Function) {
auto *expression1 = static_cast<filc::ast::Function *>(program1->getExpressions()[0]);
ASSERT_NE(nullptr, expression1);
ASSERT_IDENTIFIER("minus", expression1->getName());
ASSERT_TYPE("int", expression1->getReturnType());
ASSERT_THAT(expression1->getParameters(), SizeIs(2));
auto *parameter1_1 = expression1->getParameters()[0];
auto *parameter1_2 = expression1->getParameters()[1];
Expand All @@ -324,6 +325,7 @@ TEST(Parser, Function) {
auto *expression2 = static_cast<filc::ast::Function *>(program2->getExpressions()[0]);
ASSERT_NE(nullptr, expression2);
ASSERT_IDENTIFIER("operator==", expression2->getName());
ASSERT_TYPE("bool", expression2->getReturnType());
ASSERT_THAT(expression1->getParameters(), SizeIs(2));
auto *parameter2_1 = expression2->getParameters()[0];
auto *parameter2_2 = expression2->getParameters()[1];
Expand Down

0 comments on commit c580b3c

Please sign in to comment.