Add unary expression parsing

Signed-off-by: CHatingPython <chatingpython@gmail.com>
This commit is contained in:
CHatingPython
2026-05-29 20:09:09 +02:00
committed by CHatingPython
parent 86988ffea5
commit f178b09f89
8 changed files with 286 additions and 69 deletions
+47
View File
@@ -133,4 +133,51 @@ TEST(Parser, OperatorPrecedence_Complex) {
EXPECT_INTLIT(div->rhs(), 2);
}
TEST(Parser, UnaryOperator_Simple) {
parser parser("<TEMP>", "func main() { return -5; }");
auto program = parser.parse();
EXPECT_TRUE(program.present());
EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0];
EXPECT_TRUE(func.present());
EXPECT_EQ(func->declaration_type(), declaration_node_t::FunctionDefinition);
function_definition_node_h funcDef = func;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
return_statement_node_h ret = funcDef->body()->statements[0];
auto retVal = ret->value();
EXPECT_TRUE(retVal.present());
EXPECT_EQ(retVal->expression_type(), expression_node_t::Unaryop);
unaryop_expression_node_h neg = retVal;
EXPECT_EQ(neg->type(), unaryop_expression_node_t::Negative);
EXPECT_INTLIT(neg->get_node(), 5);
}
TEST(Parser, UnaryOperator_PrePost) {
parser parser("<TEMP>", "func main() { return --5++; }");
auto program = parser.parse();
EXPECT_TRUE(program.present());
EXPECT_EQ(program->declarations().size(), 1);
auto func = program->declarations()[0];
EXPECT_TRUE(func.present());
EXPECT_EQ(func->declaration_type(), declaration_node_t::FunctionDefinition);
function_definition_node_h funcDef = func;
EXPECT_EQ(funcDef->name()->string, "main");
EXPECT_EQ(funcDef->body()->statements.size(), 1);
return_statement_node_h ret = funcDef->body()->statements[0];
auto retVal = ret->value();
EXPECT_TRUE(retVal.present());
EXPECT_EQ(retVal->expression_type(), expression_node_t::Unaryop);
unaryop_expression_node_h inc = retVal;
EXPECT_EQ(inc->type(), unaryop_expression_node_t::PostfixIncrement);
EXPECT_EQ(inc->get_node()->expression_type(), expression_node_t::Unaryop);
unaryop_expression_node_h dec = inc->get_node();
EXPECT_INTLIT(dec->get_node(), 5);
}
} // namespace