Skip to content

Commit

Permalink
Enhance the debugging output of the parser, improve error handling lo…
Browse files Browse the repository at this point in the history
Merge pull request #194 from ax-6/main
  • Loading branch information
ax-6 authored Jan 11, 2025
2 parents bc44c77 + 536bdbd commit 6511a0e
Showing 1 changed file with 28 additions and 111 deletions.
139 changes: 28 additions & 111 deletions prototype/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ inline void EXIT_COMPILER(const char* func_name, const char* message) {
exit(-1);
}

inline void DEBUG_OUTPUT(const char* func_name, const char* message) {
std::cerr << func_name << ": " << message << std::endl;
}

namespace Aq {
namespace Compiler {
// A hash table for the lexer. Used to find special definitions such as compiler
Expand Down Expand Up @@ -2287,9 +2283,6 @@ class ReferenceType : public Type {
};

Type* Type::CreateType(Token* token, std::size_t length, std::size_t& index) {
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter the func.");

if (token == nullptr)
EXIT_COMPILER("Type::CreateType(Token*,std::size_t,std::size_t&)",
"token is nullptr.");
Expand All @@ -2299,13 +2292,7 @@ Type* Type::CreateType(Token* token, std::size_t length, std::size_t& index) {

Type* type = nullptr;
while (index < length) {
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter while loop.");

if (token[index].type == Token::Type::KEYWORD) {
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter if(token[index].type == Token::Type::KEYWORD).");

switch (token[index].value.keyword) {
case Token::KeywordType::Const: {
ConstType* const_type = new ConstType();
Expand All @@ -2315,69 +2302,44 @@ Type* Type::CreateType(Token* token, std::size_t length, std::size_t& index) {

if (index + 1 < length &&
token[index + 1].type == Token::Type::KEYWORD) {
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter if (index + 1 < length && token[index + "
"1].type == Token::Type::KEYWORD).");
index++;
switch (token[index].value.keyword) {
case Token::KeywordType::Void:
DEBUG_OUTPUT(
"Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Void.");
type->SetType(Type::BaseType::kVoid);
break;

case Token::KeywordType::Bool:
DEBUG_OUTPUT(
"Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Bool.");
type->SetType(Type::BaseType::kBool);
break;

case Token::KeywordType::Char:
DEBUG_OUTPUT(
"Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Char.");
type->SetType(Type::BaseType::kChar);
break;
case Token::KeywordType::Short:
DEBUG_OUTPUT(
"Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Short.");
type->SetType(Type::BaseType::kShort);
break;

case Token::KeywordType::Int:
DEBUG_OUTPUT(
"Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Int.");
type->SetType(Type::BaseType::kInt);
break;

case Token::KeywordType::Long:
DEBUG_OUTPUT(
"Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Long.");
type->SetType(Type::BaseType::kLong);
break;

case Token::KeywordType::Float:
DEBUG_OUTPUT(
"Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Float.");
type->SetType(Type::BaseType::kFloat);
break;

case Token::KeywordType::Double:
DEBUG_OUTPUT(
"Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Double.");
type->SetType(Type::BaseType::kDouble);
break;

case Token::KeywordType::Auto:
DEBUG_OUTPUT(
"Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Auto.");
type->SetType(Type::BaseType::kAuto);
break;

default:
DEBUG_OUTPUT(
"Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case default.");
break;
}
}
Expand All @@ -2391,126 +2353,99 @@ Type* Type::CreateType(Token* token, std::size_t length, std::size_t& index) {
break;
}
case Token::KeywordType::Void:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Void.");
type = new Type();
type->SetType(Type::BaseType::kVoid);
break;

case Token::KeywordType::Bool:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Bool.");
type = new Type();
type->SetType(Type::BaseType::kBool);
break;

case Token::KeywordType::Char:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Char.");
type = new Type();
type->SetType(Type::BaseType::kChar);
break;

case Token::KeywordType::Short:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Short.");
type = new Type();
type->SetType(Type::BaseType::kShort);
break;

case Token::KeywordType::Int:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Int.");
type = new Type();
type->SetType(Type::BaseType::kInt);
break;

case Token::KeywordType::Long:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Long.");
type = new Type();
type->SetType(Type::BaseType::kLong);
break;

case Token::KeywordType::Float:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Float.");
type = new Type();
type->SetType(Type::BaseType::kFloat);
break;

case Token::KeywordType::Double:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Double.");
type = new Type();
type->SetType(Type::BaseType::kDouble);
break;

case Token::KeywordType::Struct:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Struct.");
type = new Type();
type->SetType(Type::BaseType::kStruct);
return type;

case Token::KeywordType::Union:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Union.");
type = new Type();
type->SetType(Type::BaseType::kUnion);
return type;

case Token::KeywordType::Enum:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Enum.");
type = new Type();
type->SetType(Type::BaseType::kEnum);
return type;

case Token::KeywordType::Auto:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::KeywordType::Auto.");
type = new Type();
type->SetType(Type::BaseType::kAuto);
break;

default:
return type;
}
} else if (token[index].type == Token::Type::OPERATOR) {
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter if(token[index].type == Token::Type::OPERATOR).");

if (type == nullptr)
EXIT_COMPILER("Type::CreateType(Token*,std::size_t,std::size_t&)",
"type is nullptr.");

switch (token[index].value._operator) {
case Token::OperatorType::star: {
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::OperatorType::star.");
PointerType* pointer_type = new PointerType();
pointer_type->SetSubType(type);
type = pointer_type;
break;
}

case Token::OperatorType::amp: {
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::OperatorType::amp.");
ReferenceType* reference_type = new ReferenceType();
reference_type->SetSubType(type);
type = reference_type;
break;
}

default:
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter case Token::OperatorType::default.");
return type;
}
} else if (token[index].type == Token::Type::IDENTIFIER) {
DEBUG_OUTPUT("Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter if(token[index].type == Token::Type::IDENTIFIER).");

std::size_t index_temp = index;
ExprNode* temp_expr = Parser::ParsePrimaryExpr(token, length, index_temp);
if (temp_expr == nullptr)
EXIT_COMPILER("Type::CreateType(Token*,std::size_t,std::size_t&)",
"ParsePrimaryExpr return nullptr.");

if (temp_expr->GetType() == StmtNode::StmtType::kArray) {
DEBUG_OUTPUT(
"Type::CreateType(Token*,std::size_t,std::size_t&)",
"Enter if (temp_expr->GetType() == StmtNode::StmtType::kArray).");
ArrayType* array_type = new ArrayType();
array_type->SetSubType(type,
dynamic_cast<ArrayNode*>(temp_expr)->GetIndex());
Expand Down Expand Up @@ -2538,42 +2473,33 @@ CompoundNode* Parser::Parse(std::vector<Token> token) {
std::size_t length = token.size();
CompoundNode* ast = nullptr;
std::vector<StmtNode*> stmts;

while (index < token.size()) {
std::cout << "index: " << index << ", size: " << token.size() << " \n"
<< token[index] << std::endl
<< std::endl;
if (IsDecl(token_ptr, length, index)) {
if (IsFuncDecl(token_ptr, length, index)) {
stmts.push_back(ParseFuncDecl(token_ptr, length, index));
} else {
std::cout << "VarDecl" << " \n"
<< token[index] << std::endl
<< std::endl;
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) {
std::cout << "Error" << " \n"
<< token[index] << std::endl
<< std::endl;
EXIT_COMPILER("Parser::Parse(std::vector<Token>)", "not found semi.");
return nullptr;
}
index++;
}
} else {
// ERROR
index++;
std::cout << token[index] << std::endl;
EXIT_COMPILER("Parser::Parse(std::vector<Token>)", "Unexpected code.");
}
}

ast = new CompoundNode();
ast->SetCompoundNode(stmts);
return ast;
}

bool Parser::IsDecl(Token* token, std::size_t length, std::size_t index) {
std::cout << "RUN Parser::IsDecl" << " \n"
<< token[index] << std::endl
<< std::endl;
if (token[index].type == Token::Type::KEYWORD) {
if (token[index].value.keyword == Token::KeywordType::Auto ||
token[index].value.keyword == Token::KeywordType::Bool ||
Expand Down Expand Up @@ -2601,14 +2527,8 @@ bool Parser::IsDecl(Token* token, std::size_t length, std::size_t index) {
token[index].value.keyword == Token::KeywordType::Unsigned ||
token[index].value.keyword == Token::KeywordType::Virtual ||
token[index].value.keyword == Token::KeywordType::Wchar_t) {
std::cout << "RETURN TRUE Parser::IsDecl 1" << " \n"
<< token[index] << std::endl
<< std::endl;
return true;
} else {
std::cout << "RETURN FALSE Parser::IsDecl" << " \n"
<< token[index] << std::endl
<< std::endl;
return false;
}
} else if ((token[index].type == Token::Type::IDENTIFIER &&
Expand All @@ -2620,14 +2540,11 @@ bool Parser::IsDecl(Token* token, std::size_t length, std::size_t index) {
token[index + 1].value._operator ==
Token::OperatorType::ampamp) &&
token[index + 2].type == Token::Type::IDENTIFIER)) {
std::cout << "RETURN TRUE Parser::IsDecl 2" << " \n"
<< token[index] << std::endl
<< std::endl;
// TODO: Change the processing logic of custom types and add support of
// custom types.
return true;
}
std::cout << "RETURN FALSE Parser::IsDecl" << " \n"
<< token[index] << std::endl
<< std::endl;

return false;
}

Expand Down

0 comments on commit 6511a0e

Please sign in to comment.