Latest stuff, rewriting lexer

This commit is contained in:
Michael Fabian 'Xaymar' Dirks
2024-06-25 18:59:15 +02:00
parent 7f669f55e2
commit fa81c2a7fa
23 changed files with 1263 additions and 310 deletions
+29 -25
View File
@@ -1,3 +1,6 @@
/// AUTOGENERATED COPYRIGHT HEADER START
// Copyright (C) 2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
// AUTOGENERATED COPYRIGHT HEADER END
#include "parser.hpp"
#include "ast/function.hpp"
#include <iostream>
@@ -21,10 +24,10 @@ blitz::parser::~parser() {
}
}
std::unique_ptr<blitz::AST::Expression> blitz::parser::Parse() {
std::unique_ptr<AST::ScopeExpression> scope = std::make_unique<AST::ScopeExpression>();
std::unique_ptr<blitz::ast::expression> blitz::parser::parse() {
std::unique_ptr<ast::ScopeExpression> scope = std::make_unique<ast::ScopeExpression>();
std::unique_ptr<AST::Expression> expr;
std::unique_ptr<ast::expression> expr;
while ((expr = std::move(parse_expression())) != nullptr) {
scope->AddExpression(std::move(expr));
}
@@ -32,7 +35,7 @@ std::unique_ptr<blitz::AST::Expression> blitz::parser::Parse() {
return std::move(scope);
}
void blitz::parser::LogMessage(const char* msg, ...) {
void blitz::parser::log(const char* msg, ...) {
std::vector<char> buf(65535);
va_list val;
va_start(val, msg);
@@ -41,7 +44,7 @@ void blitz::parser::LogMessage(const char* msg, ...) {
std::cout << buf.data() << '\n';
}
void blitz::parser::LogError(const char* msg, ...) {
void blitz::parser::log_error(const char* msg, ...) {
std::vector<char> buf(65535);
va_list val;
va_start(val, msg);
@@ -50,60 +53,61 @@ void blitz::parser::LogError(const char* msg, ...) {
std::cerr << buf.data() << '\n';
}
std::pair<blitz::Lexer::Token, std::string> blitz::parser::GetNextToken() {
return m_lexer.GetNextToken(m_files.top().second);
std::pair<blitz::lexer::tokentype, std::string> blitz::parser::next() {
return m_lexer.next(m_files.top().second);
}
std::unique_ptr<blitz::AST::Expression> blitz::parser::parse_expression() {
std::unique_ptr<blitz::ast::expression> blitz::parser::parse_expression() {
while (true) {
auto tkn = GetNextToken();
auto tkn = next();
log("%s", tkn.second.c_str());
switch (tkn.first) {
case blitz::Lexer::Token::TokenNewLine:
case blitz::Lexer::Token::TokenComment:
case blitz::lexer::tokentype::TokenNewLine:
case blitz::lexer::tokentype::TokenComment:
// Skip Comments, since we don't really need them for the AST.
continue;
case blitz::Lexer::Token::TokenPlus:
case blitz::Lexer::Token::TokenMinus:
case blitz::lexer::tokentype::TokenPlus:
case blitz::lexer::tokentype::TokenMinus:
default: // End Of File / Unknown
case blitz::Lexer::Token::TokenUnknown:
case blitz::Lexer::Token::TokenEOF:
case blitz::lexer::tokentype::TokenUnknown:
case blitz::lexer::tokentype::TokenEOF:
return nullptr;
break;
}
}
}
std::unique_ptr<blitz::AST::NumberExpression> blitz::parser::parse_number(blitz::Lexer::Token token, std::string value) {
if (token != Lexer::Token::TokenNumber) {
LogError("Unexpected Token during parsing, expected number.");
std::unique_ptr<blitz::ast::NumberExpression> blitz::parser::parse_number(blitz::lexer::tokentype token, std::string value) {
if (token != lexer::tokentype::TokenNumber) {
log_error("Unexpected Token during parsing, expected number.");
return nullptr;
}
char* endptr = const_cast<char*>(value.c_str() + value.size());
int32_t parsed = strtol(value.c_str(), &endptr, 10);
if (errno == ERANGE) {
LogError("Number out of range.");
log_error("Number out of range.");
return nullptr;
}
return std::make_unique<blitz::AST::NumberExpression>(parsed);
return std::make_unique<blitz::ast::NumberExpression>(parsed);
}
std::unique_ptr<blitz::AST::DecimalExpression> blitz::parser::parse_decimal(blitz::Lexer::Token token, std::string value) {
if (token != Lexer::Token::TokenNumber) {
LogError("Unexpected Token during parsing, expected number.");
std::unique_ptr<blitz::ast::DecimalExpression> blitz::parser::parse_decimal(blitz::lexer::tokentype token, std::string value) {
if (token != lexer::tokentype::TokenNumber) {
log_error("Unexpected Token during parsing, expected number.");
return nullptr;
}
char* endptr = const_cast<char*>(value.c_str() + value.size());
float_t parsed = strtof(value.c_str(), &endptr);
if (errno == ERANGE) {
LogError("Number out of range.");
log_error("Number out of range.");
return nullptr;
}
return std::make_unique<blitz::AST::DecimalExpression>(parsed);
return std::make_unique<blitz::ast::DecimalExpression>(parsed);
}