Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed many bugs in Parser::Parse function. #126

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 84 additions & 64 deletions prototype/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1611,15 +1611,20 @@ CompoundNode* Parser::Parse(std::vector<Token> token) {
size_t length = token.size();
CompoundNode* ast = nullptr;
std::vector<StmtNode*> stmts;
if (IsDecl(token_ptr, length, index)) {
if (IsFuncDecl(token_ptr, length, index)) {
stmts.push_back(ParseFuncDecl(token_ptr, length, index));
while (index <= token.size()) {
if (IsDecl(token_ptr, length, index)) {
if (IsFuncDecl(token_ptr, length, index)) {
stmts.push_back(ParseFuncDecl(token_ptr, length, index));
} else {
stmts.push_back(
dynamic_cast<DeclNode*>(ParseVarDecl(token_ptr, length, index)));
if (token_ptr[index].type != Token::Type::OPERATOR ||
token_ptr[index].value._operator != Token::OperatorType::semi)
return nullptr;
index++;
}
} else {
stmts.push_back(
dynamic_cast<DeclNode*>(ParseVarDecl(token_ptr, length, index)));
if (token_ptr[index].type != Token::Type::OPERATOR ||
token_ptr[index].value._operator != Token::OperatorType::semi)
return nullptr;
// ERROR
index++;
}
}
Expand Down Expand Up @@ -1891,10 +1896,12 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
full_expr = amp_node;
preoper_expr = amp_node;
} else {
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
amp_node);
if (preoper_expr != nullptr)
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
amp_node);
}
index++;
break;
Expand All @@ -1909,10 +1916,11 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
full_expr = star_node;
preoper_expr = star_node;
} else {
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
star_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
star_node);
}
index++;
break;
Expand All @@ -1927,10 +1935,11 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
full_expr = plus_node;
preoper_expr = plus_node;
} else {
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
plus_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
plus_node);
}
index++;
break;
Expand All @@ -1945,10 +1954,11 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
full_expr = minus_node;
preoper_expr = minus_node;
} else {
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
minus_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
minus_node);
}
index++;
break;
Expand All @@ -1963,10 +1973,11 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
full_expr = not_node;
preoper_expr = not_node;
} else {
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
not_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
not_node);
}
index++;
break;
Expand All @@ -1982,10 +1993,11 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
full_expr = bitwisenot_node;
preoper_expr = bitwisenot_node;
} else {
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
bitwisenot_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
bitwisenot_node);
}
index++;
break;
Expand All @@ -1998,10 +2010,11 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
index++;
ArrayNode* array = new ArrayNode();
array->SetArrayNode(main_expr, ParseExpr(token, length, index));
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
array);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
array);
if (main_expr == full_expr) main_expr = array;
index++;
break;
Expand All @@ -2028,17 +2041,19 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
ConvertNode* convert_node = new ConvertNode();
convert_node->SetConvertNode(
Type::CreateType(token, length, index), nullptr);
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
convert_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
convert_node);
preoper_expr = convert_node;
} else {
ExprNode* full_expr_node = ParseExpr(token, length, index);
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
full_expr_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
full_expr_node);
state = State::kPostOper;
}
}
Expand Down Expand Up @@ -2068,10 +2083,11 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
}
FuncInvokeNode* func_invoke_node = new FuncInvokeNode();
func_invoke_node->SetFuncInvokeNode(main_expr, args);
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
func_invoke_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
func_invoke_node);
main_expr = func_invoke_node;
} else {
state = State::kEnd;
Expand All @@ -2088,10 +2104,11 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
if (full_expr == nullptr || preoper_expr == nullptr) {
preoper_expr = full_expr = preinc_node;
} else {
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
preinc_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
preinc_node);
}
} else {
preinc_node->SetUnaryNode(UnaryNode::Operator::kPostInc, full_expr);
Expand All @@ -2107,10 +2124,11 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
if (full_expr == nullptr || preoper_expr == nullptr) {
preoper_expr = full_expr = postinc_node;
} else {
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
postinc_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
postinc_node);
}
} else {
postinc_node->SetUnaryNode(UnaryNode::Operator::kPostInc,
Expand Down Expand Up @@ -2149,10 +2167,11 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
if (full_expr == nullptr || preoper_expr == nullptr) {
full_expr = preoper_expr = main_expr = identifier_node;
} else {
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
identifier_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
identifier_node);
main_expr = identifier_node;
}
index++;
Expand All @@ -2164,10 +2183,11 @@ ExprNode* Parser::ParsePrimaryExpr(Token* token, size_t length, size_t& index) {
if (full_expr == nullptr || preoper_expr == nullptr) {
full_expr = preoper_expr = main_expr = number_node;
} else {
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
number_node);
if (preoper_expr != nullptr)
dynamic_cast<UnaryNode*>(preoper_expr)
->SetUnaryNode(
dynamic_cast<UnaryNode*>(preoper_expr)->GetOperator(),
number_node);
main_expr = number_node;
}
index++;
Expand Down
Loading