Skip to content

Commit

Permalink
support for parsing '{' and '}' symbol, and nested scopes within func…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
august95 committed Sep 14, 2024
1 parent 3667a78 commit 566df86
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
5 changes: 5 additions & 0 deletions compiler_lib/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ int node::getStackSize()
}
}

else if (m_node_type == NODE_TYPE_BODY)
{
return m_body_size;
}

return 0;
}

Expand Down
1 change: 1 addition & 0 deletions compiler_lib/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class node
std::shared_ptr < node > getValueNode() { return m_value_node; }
void setBodyNode(std::shared_ptr < node > body_node) { m_body_node = body_node; }
std::shared_ptr < node > getBodyNode() { return m_body_node; }
int getBodySize() { return m_body_size; }

void setDatatype(std::shared_ptr < datatype > dtype) { m_datatype = dtype; }
std::shared_ptr < datatype > getDatatype() {return m_datatype; }
Expand Down
27 changes: 23 additions & 4 deletions compiler_lib/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,27 @@ void parser::parseStatement()
return;
}

parseExpression();
parseExpression(); //if symbol, no tokens are popped

//parse symbol ('{' new scope or ':' label)
//no other token types is expected as the first token in the statement
token = peekToken();

if (token->isTokenTypeSymbol() && token->getCharValue() == '{')
{
parseSymbol();
nextToken(); //pop off '}'
/*
* Nested scope, return because ';' is not expected
*
* {
* {
* }
* }
*/
return;
}

token = nextToken();

if ((!token->isTokenTypeSymbol()) || token->getCharValue() != ';')
{
cerror("expected ';' at ending of statement");
Expand All @@ -309,7 +324,11 @@ void parser::parseStatement()
void parser::parseSymbol()
{
//parse '{' new scope

std::shared_ptr<token> token = peekToken();
if (token->isTokenTypeSymbol() && token->getCharValue() == '{')
{
parseBody();
}
//parse ':' label
}

Expand Down
43 changes: 42 additions & 1 deletion unit_test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,7 @@ TEST(parser, function) {
/*
* File Content
*
* a = 50 * 30 + 20
* int main(){ int var_val; var_val + 50;}
*/
const int num_of_tokens = 5;

Expand All @@ -644,7 +644,48 @@ TEST(parser, function) {
process.startCompiler();

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

EXPECT_EQ(_node->getStringValue(), "main");
EXPECT_EQ(_node->getBodyNode()->getBodySize(), 4 );
std::list < std::shared_ptr < node > > statements = _node->getBodyNode()->getStatements();
EXPECT_EQ(statements.size(), 2 );
statements.pop_back();
EXPECT_EQ(statements.back()->getStackSize(), 4); //int var_val;

}



TEST(parser, functionWithSecondScope) {

std::string file_name = "test_parser_function_2.c";
/*
* File Content
*
* int main() { int var_a; { int var_val; var_val + 50; int var_b; } }
*/
const int num_of_tokens = 5;

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

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

EXPECT_EQ(_node->getStringValue(), "main");
EXPECT_EQ(_node->getBodyNode()->getBodySize(), 12);
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();
EXPECT_EQ(statements.back()->getStackSize(), 4); //int var_val;

EXPECT_EQ(nested_body_node->getBodySize(), 8);
std::list < std::shared_ptr < node > > nested_statements = nested_body_node->getStatements();
EXPECT_EQ(nested_statements.size(), 3);

}

1 change: 1 addition & 0 deletions unit_test/test_files/test_parser_function_2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
int main() { int var_a; { int var_val; var_val + 50; int var_b; } }

0 comments on commit 566df86

Please sign in to comment.