feat: error handling, better prelude, double linked lists usage etc

This commit is contained in:
2026-01-02 22:06:56 +01:00
parent a453603b9b
commit 6176d549c1
10 changed files with 148 additions and 65 deletions

View File

@@ -2,6 +2,7 @@
#include <cassert>
#include "parser/lexer.hpp"
#include "parser/nodes.hpp"
#include "prelude/error.hpp"
class AstParser
{
@@ -78,8 +79,8 @@ public:
return new VariableNode(name);
}
default:
fprintf(stderr, "%s:%d:%d: ERROR: unexpected token while parsing %ld\n", m_lexer->filename(), token->line_number, token->offset_start, token->token);
Exit(1);
// fprintf(stderr, "%s:%d:%d: ERROR: unexpected token while parsing %ld\n", m_lexer->filename(), token->line_number, token->offset_start, token->token);
ErrorLogger::Raise(Error::ParseError(m_lexer->filename(), StringView::FromFormat("unexpected token while parsing '%c'", token->token), token->line_number, token->offset_start));
break;
}
@@ -94,7 +95,7 @@ public:
{
m_lexer->NextToken();
ExpressionNode::Operator eop;
assert((int)ExpressionNode::Operator::COUNT_OPERATORS == 4 && "some operators may not be handled");
assert(static_cast<size_t>(ExpressionNode::Operator::COUNT_OPERATORS) == 4 && "some operators may not be handled");
switch((char)op->token)
{
case '/':
@@ -122,7 +123,7 @@ public:
{
m_lexer->NextToken();
ExpressionNode::Operator eop;
assert((int)ExpressionNode::Operator::COUNT_OPERATORS == 4 && "some operators may not be handled");
assert(static_cast<size_t>(ExpressionNode::Operator::COUNT_OPERATORS) == 4 && "some operators may not be handled");
switch((char)op->token)
{
case '+':
@@ -155,8 +156,7 @@ public:
Node* ParseStatement()
{
auto token = m_lexer->seek_token();
// TODO: proper error handling
assert(token != nullptr && "next token should be available");
assert(token != nullptr);
switch(token->token)
{
case TokenType::Local: return ParseVarDecl();
@@ -176,13 +176,13 @@ public:
auto token = m_lexer->token();
switch(token.token)
{
case TokenType::Extern: program->PushExtern(ParseExtern()); break;
case TokenType::Fn: program->PushFunction(ParseFnDecl()); break;
case TokenType::Extern: program->PushExtern(ParseExtern()); break;
default: {
fprintf(stderr, "%s:%d:%d: ERROR: unexpected token while parsing %ld\n", m_lexer->filename(), token.line_number, token.offset_start, token.token);
Exit(1);
break;
ErrorLogger::Raise(Error::ParseError(m_lexer->filename(), StringView::FromFormat("unexpected token while parsing '%c'", token.token), token.line_number, token.offset_start));
assert(false);
}
break;
}
}