Skip to content

Commit

Permalink
Replace unnecessary ElseClauseASTNode
Browse files Browse the repository at this point in the history
  • Loading branch information
mifka01 committed Nov 17, 2023
1 parent d40b2b0 commit c022367
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 87 deletions.
13 changes: 2 additions & 11 deletions include/compiler/parser/ASTNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ enum ASTNodeType {
NODE_ARGUMENT_LIST,
NODE_FUNCTION_CALL,
NODE_IF_STATEMENT,
NODE_ELSE_CLAUSE,
NODE_PATTERN,
NODE_CONDITION,
NODE_OPTIONAL_BINDING_CONDITION,
Expand Down Expand Up @@ -205,18 +204,11 @@ typedef struct ConditionASTNode {
OptionalBindingConditionASTNode *optionalBindingCondition;
} ConditionASTNode;

typedef struct ElseClauseASTNode {
enum ASTNodeType _type;
struct IfStatementASTNode *ifStatement;
BlockASTNode *body;
bool isElseIf;
} ElseClauseASTNode;

typedef struct IfStatementASTNode {
enum ASTNodeType _type;
ConditionASTNode *condition;
BlockASTNode *body;
ElseClauseASTNode *elseClause;
ASTNode /* BlockASTNode | IfStatementASTNode | null */ *alternate;
} IfStatementASTNode;

typedef struct WhileStatementASTNode {
Expand Down Expand Up @@ -256,8 +248,7 @@ FunctionCallASTNode* new_FunctionCallASTNode(IdentifierASTNode *id, ArgumentList
PatternASTNode* new_PatternASTNode(IdentifierASTNode *id, TypeReferenceASTNode *type);
OptionalBindingConditionASTNode* new_OptionalBindingConditionASTNode(PatternASTNode *pattern, ExpressionASTNode *initializer, bool isConstant);
ConditionASTNode* new_ConditionASTNode(ExpressionASTNode *expression, OptionalBindingConditionASTNode *optionalBindingCondition);
ElseClauseASTNode* new_ElseClauseASTNode(IfStatementASTNode *ifStatement, BlockASTNode *body, bool isElseIf);
IfStatementASTNode* new_IfStatementASTNode(ConditionASTNode *condition, BlockASTNode *body, ElseClauseASTNode *elseClause);
IfStatementASTNode* new_IfStatementASTNode(ConditionASTNode *condition, BlockASTNode *body, ASTNode *alternate);
WhileStatementASTNode* new_WhileStatementASTNode(ConditionASTNode *condition, BlockASTNode *body);
AssignmentStatementASTNode* new_AssignmentStatementASTNode(IdentifierASTNode *id, ExpressionASTNode *assignment);

Expand Down
20 changes: 4 additions & 16 deletions src/compiler/parser/ASTNodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,27 +184,15 @@ ConditionASTNode * new_ConditionASTNode(
return node;
}

ElseClauseASTNode * new_ElseClauseASTNode(
IfStatementASTNode *ifStatement,
BlockASTNode *body,
bool isElseIf
) {
prepare_node_of(ElseClauseASTNode, NODE_ELSE_CLAUSE)
node->ifStatement = ifStatement;
node->body = body;
node->isElseIf = isElseIf;
return node;
}

IfStatementASTNode * new_IfStatementASTNode(
ConditionASTNode *condition,
BlockASTNode *body,
ElseClauseASTNode *elseClause
ASTNode *alternate
) {
prepare_node_of(IfStatementASTNode, NODE_IF_STATEMENT)
node->condition = condition;
node->body = body;
node->elseClause = elseClause;
node->alternate = alternate;
return node;
}

Expand Down Expand Up @@ -243,12 +231,12 @@ BinaryExpressionASTNode* new_BinaryExpressionASTNode(
UnaryExpressionASTNode* new_UnaryExpressionASTNode(
ExpressionASTNode *argument,
OperatorType operator
//bool IsPrefix
// bool IsPrefix
) {
prepare_node_of(UnaryExpressionASTNode, NODE_UNARY_EXPRESSION)
node->argument = argument;
node->operator = operator;
//node->isPrefix = isPrefix;
// node->isPrefix = isPrefix;
return node;
}

Expand Down
63 changes: 23 additions & 40 deletions src/compiler/parser/Parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ ParserResult __Parser_parseFuncStatement(Parser *parser);
ParserResult __Parser_parsePattern(Parser *parser);
ParserResult __Parser_parseOptionalBindingCondition(Parser *parser);
ParserResult __Parser_parseCondition(Parser *parser);
ParserResult __Parser_parseElseClause(Parser *parser);
ParserResult __Parser_parseIfStatement(Parser *parser);
ParserResult __Parser_parseWhileStatement(Parser *parser);
ParserResult __Parser_parseReturnStatement(Parser *parser);
Expand Down Expand Up @@ -506,40 +505,6 @@ ParserResult __Parser_parseCondition(Parser *parser) {
return ParserSuccess(condition);
}

ParserResult __Parser_parseElseClause(Parser *parser) {
assertf(parser != NULL);

// skip else keyword
LexerResult result = Lexer_nextToken(parser->lexer);
if(!result.success) return LexerToParserError(result);

LexerResult peek = Lexer_peekToken(parser->lexer, 1);
if(!peek.success) return LexerToParserError(peek);

IfStatementASTNode *ifStatement = NULL;
bool isElseIf = false;
BlockASTNode *body = NULL;

if(peek.token->kind == TOKEN_IF) {
// consume if keyword
result = Lexer_nextToken(parser->lexer);
if(!result.success) return LexerToParserError(result);

ParserResult ifStatementResult = __Parser_parseIfStatement(parser);
if(!ifStatementResult.success) return ifStatementResult;
ifStatement = (IfStatementASTNode*)ifStatementResult.node;
isElseIf = true;
} else {
ParserResult blockResult = __Parser_parseBlock(parser, true);
if(!blockResult.success) return blockResult;
body = (BlockASTNode*)blockResult.node;
}

ElseClauseASTNode *elseClause = new_ElseClauseASTNode(ifStatement, body, isElseIf);

return ParserSuccess(elseClause);
}

ParserResult __Parser_parseIfStatement(Parser *parser) {
assertf(parser != NULL);

Expand Down Expand Up @@ -569,15 +534,33 @@ ParserResult __Parser_parseIfStatement(Parser *parser) {
peek = Lexer_peekToken(parser->lexer, 1);
if(!peek.success) return LexerToParserError(peek);

ElseClauseASTNode *elseClause = NULL;
ASTNode *alternate = NULL;

if(peek.token->kind == TOKEN_ELSE) {
ParserResult elseClauseResult = __Parser_parseElseClause(parser);
if(!elseClauseResult.success) return elseClauseResult;
elseClause = (ElseClauseASTNode*)elseClauseResult.node;
// skip else keyword
LexerResult result = Lexer_nextToken(parser->lexer);
if(!result.success) return LexerToParserError(result);

LexerResult peek = Lexer_peekToken(parser->lexer, 1);
if(!peek.success) return LexerToParserError(peek);

if(peek.token->kind == TOKEN_IF) {
// consume if keyword
result = Lexer_nextToken(parser->lexer);
if(!result.success) return LexerToParserError(result);

ParserResult ifStatementResult = __Parser_parseIfStatement(parser);
if(!ifStatementResult.success) return ifStatementResult;
alternate = (ASTNode*)ifStatementResult.node;

} else {
ParserResult blockResult = __Parser_parseBlock(parser, true);
if(!blockResult.success) return blockResult;
alternate = (ASTNode*)blockResult.node;
}
}

IfStatementASTNode *ifStatement = new_IfStatementASTNode((ConditionASTNode*)conditionResult.node, (BlockASTNode*)blockResult.node, (ElseClauseASTNode*)elseClause);
IfStatementASTNode *ifStatement = new_IfStatementASTNode((ConditionASTNode*)conditionResult.node, (BlockASTNode*)blockResult.node, (ASTNode*)alternate);

return ParserSuccess(ifStatement);
}
Expand Down
30 changes: 10 additions & 20 deletions test/compiler/parser/Parser.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ DESCRIBE(if_statement, "If statement parsing") {
EXPECT_EQUAL_INT(arr->size, 0);

// else
EXPECT_NULL(if_statement->elseClause);
EXPECT_NULL(if_statement->alternate);

} TEST_END();

Expand Down Expand Up @@ -549,14 +549,9 @@ DESCRIBE(if_statement, "If statement parsing") {
EXPECT_NULL(arr->data);
EXPECT_EQUAL_INT(arr->size, 0);

// else
EXPECT_NOT_NULL(if_statement->elseClause);
EXPECT_FALSE(if_statement->elseClause->isElseIf);
EXPECT_NULL(if_statement->elseClause->ifStatement);
EXPECT_NOT_NULL(if_statement->elseClause->body);

// else body
body = if_statement->elseClause->body;
EXPECT_TRUE(if_statement->alternate->_type == NODE_BLOCK);
body = (BlockASTNode*)if_statement->alternate;
EXPECT_NOT_NULL(body->statements);
arr = body->statements;
EXPECT_NULL(arr->data);
Expand Down Expand Up @@ -602,11 +597,11 @@ DESCRIBE(if_statement, "If statement parsing") {
EXPECT_EQUAL_INT(arr->size, 0);

// else if
EXPECT_NOT_NULL(if_statement->elseClause);
EXPECT_TRUE(if_statement->elseClause->isElseIf);
EXPECT_NOT_NULL(if_statement->elseClause->ifStatement);
EXPECT_NOT_NULL(if_statement->alternate);

EXPECT_TRUE(if_statement->alternate->_type == NODE_IF_STATEMENT);

IfStatementASTNode *elseif = if_statement->elseClause->ifStatement;
IfStatementASTNode *elseif = (IfStatementASTNode*)if_statement->alternate;

EXPECT_NOT_NULL(elseif->condition);
EXPECT_TRUE(elseif->condition->_type == NODE_CONDITION);
Expand Down Expand Up @@ -635,14 +630,9 @@ DESCRIBE(if_statement, "If statement parsing") {
EXPECT_NULL(arr->data);
EXPECT_EQUAL_INT(arr->size, 0);

// else
EXPECT_NOT_NULL(elseif->elseClause);
EXPECT_FALSE(elseif->elseClause->isElseIf);
EXPECT_NULL(elseif->elseClause->ifStatement);
EXPECT_NOT_NULL(elseif->elseClause->body);

// else body
body = elseif->elseClause->body;
EXPECT_TRUE(elseif->alternate->_type == NODE_BLOCK);
body = (BlockASTNode*)elseif->alternate;
EXPECT_NOT_NULL(body->statements);
arr = body->statements;
EXPECT_NULL(arr->data);
Expand Down Expand Up @@ -705,7 +695,7 @@ DESCRIBE(if_statement, "If statement parsing") {
EXPECT_EQUAL_INT(arr->size, 0);

// else
EXPECT_NULL(if_statement->elseClause);
EXPECT_NULL(if_statement->alternate);

} TEST_END();
}
Expand Down

0 comments on commit c022367

Please sign in to comment.