Skip to content

Commit

Permalink
parse body adds each individual statement into the body node. The bod…
Browse files Browse the repository at this point in the history
…y node continuously calculates body stack size
  • Loading branch information
august95 committed Sep 14, 2024
1 parent cf224f0 commit 3667a78
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 7 deletions.
6 changes: 6 additions & 0 deletions compiler_lib/datatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ void datatype::setDataType(std::string data_type)

}

void datatype::incrementPointerDepth()
{
m_pointer_depth++;
calcualteDatatypeSize();
}

void datatype::calcualteDatatypeSize()
{
m_datatype_size = getPrimitiveTypeSize(m_first);
Expand Down
2 changes: 1 addition & 1 deletion compiler_lib/datatype.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class datatype
bool isConst() { return m_const; }
bool isExtern() { return m_extern; }
bool secondaryPrimitiveType() { return m_has_secondary_primitive_type; }
void incrementPointerDepth() { m_pointer_depth++; }
void incrementPointerDepth();
bool isUnion() { return m_first == primitiveType::DATA_TYPE_UNION; }
bool isStruct() { return m_first == primitiveType::DATA_TYPE_STRUCT; }
primitiveType getPrimitiveType() { return m_first; }
Expand Down
6 changes: 6 additions & 0 deletions compiler_lib/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ void node::reorderExpression()

}

void node::addStatement(std::shared_ptr<node> statement)
{
m_statements.push_back(statement);
m_body_size += statement->getStackSize();
}

void node::shiftChildrenLeft()
{
std::string right_operator = getRightNode()->getStringValue();
Expand Down
3 changes: 2 additions & 1 deletion compiler_lib/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ class node

void setDatatype(std::shared_ptr < datatype > dtype) { m_datatype = dtype; }
std::shared_ptr < datatype > getDatatype() {return m_datatype; }
void setStatements(std::list < std::shared_ptr < node > > statements) { m_statements = statements; }
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; }

void setStringValue(std::string string_value) { m_string_value = string_value; }
Expand Down
5 changes: 3 additions & 2 deletions compiler_lib/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ void parser::parseVariableOrFunction()
_node->setNodeType(nodeType::NODE_TYPE_FUNCTION);
pushNode(_node); //_node is popped inside parseFunction
parseFunction();
return; //function node allready pushed
}
else
{
Expand Down Expand Up @@ -254,6 +255,7 @@ void parser::parseBody()
std::shared_ptr<token> token = nextToken(); // '{'
std::list < std::shared_ptr < node > > statements;
std::shared_ptr < node > body_node = std::make_shared < node >(nodeType::NODE_TYPE_BODY, token->getFilePosition());
int stack_size = 0;

if (!token->isTokenTypeSymbol() || token->getCharValue() != '{')
{
Expand All @@ -266,11 +268,10 @@ void parser::parseBody()
{
parseStatement();
std::shared_ptr < node > statement_node = popLastNode();
statements.push_back(statement_node);
body_node->addStatement(statement_node);
token = peekToken();

}
body_node->setStatements(statements);

token = peekToken(); // '}', parseGlobalKeyword will pop this symbol
if (!token->isTokenTypeSymbol() || token->getCharValue() != '}')
Expand Down
7 changes: 4 additions & 3 deletions unit_test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ TEST(parser, keyword) {
EXPECT_TRUE(dtype->secondaryPrimitiveType());
EXPECT_EQ(dtype->getPrimitiveType(), primitiveType::DATA_TYPE_LONG);
EXPECT_EQ(dtype->getSecondPrimitiveType(), primitiveType::DATA_TYPE_LONG);
EXPECT_EQ(dtype->getDatatypeSize(), 4);

EXPECT_EQ(_node->getStringValue(), "var_name");

Expand All @@ -639,10 +640,10 @@ TEST(parser, function) {
const int num_of_tokens = 5;

compileProcess process;
//process.initialize(file_path + file_name);
//process.startCompiler();
process.initialize(file_path + file_name);
process.startCompiler();

// std::list < std::shared_ptr < node > > ast = process.getAbstractSyntaxTree();
std::list < std::shared_ptr < node > > ast = process.getAbstractSyntaxTree();


}
Expand Down

0 comments on commit 3667a78

Please sign in to comment.