Skip to content

Commit

Permalink
return datatype added to function node
Browse files Browse the repository at this point in the history
  • Loading branch information
august95 committed Sep 15, 2024
1 parent 566df86 commit 94e2e02
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions compiler_lib/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class node

void setDatatype(std::shared_ptr < datatype > dtype) { m_datatype = dtype; }
std::shared_ptr < datatype > getDatatype() {return m_datatype; }
void setReturnDatatype(std::shared_ptr < datatype > dtype) { m_return_datatype = dtype; }
std::shared_ptr < datatype > getReturnDatatype() {return m_return_datatype; }
void addStatement(std::shared_ptr<node> statement);
// void setStatements(std::list < std::shared_ptr < node > > statements);
std::list < std::shared_ptr < node > > getStatements() { return m_statements; }
Expand Down Expand Up @@ -100,6 +102,7 @@ class node

//used by: function nodes
std::shared_ptr < node > m_body_node;
std::shared_ptr < datatype > m_return_datatype;

nodeType m_node_type;
filePosition m_file_position;
Expand Down
1 change: 1 addition & 0 deletions compiler_lib/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ void parser::parseVariableOrFunction()
{
//parsing function int a(){}
_node->setNodeType(nodeType::NODE_TYPE_FUNCTION);
_node->setReturnDatatype(datatype);
pushNode(_node); //_node is popped inside parseFunction
parseFunction();
return; //function node allready pushed
Expand Down
5 changes: 5 additions & 0 deletions unit_test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,12 @@ TEST(parser, functionWithSecondScope) {

EXPECT_EQ(_node->getStringValue(), "main");
EXPECT_EQ(_node->getBodyNode()->getBodySize(), 12);
EXPECT_EQ(_node->getReturnDatatype()->getDatatypeSize(), 4);
EXPECT_EQ(_node->getReturnDatatype()->getPrimitiveType(), primitiveType::DATA_TYPE_INTEGER);

std::list < std::shared_ptr < node > > statements = _node->getBodyNode()->getStatements();
EXPECT_EQ(statements.size(), 2);


std::shared_ptr < node > nested_body_node = statements.back(); // { int var_val; var_val + 50; int var_b; }
statements.pop_back();
Expand All @@ -687,5 +691,6 @@ TEST(parser, functionWithSecondScope) {
std::list < std::shared_ptr < node > > nested_statements = nested_body_node->getStatements();
EXPECT_EQ(nested_statements.size(), 3);


}

0 comments on commit 94e2e02

Please sign in to comment.