Files
BlitzLLVM/code_compiler/source/parser.cpp
T

116 lines
3.3 KiB
C++
Raw Normal View History

2024-06-25 18:59:15 +02:00
/// AUTOGENERATED COPYRIGHT HEADER START
// Copyright (C) 2024 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
// AUTOGENERATED COPYRIGHT HEADER END
2024-06-26 00:31:06 +02:00
/*
2024-06-06 13:37:13 +02:00
#include "parser.hpp"
#include "ast/function.hpp"
#include <iostream>
#include <vector>
#include <stdarg.h>
2024-06-06 14:04:34 +02:00
blitz::parser::parser(std::string file) {
2024-06-06 13:37:13 +02:00
// Try and load the file
std::shared_ptr<std::ifstream> instream = std::make_shared<std::ifstream>(file);
if (instream->bad() || !instream->good()) {
throw std::ios_base::failure("Failed to open file.");
}
m_files.push(std::make_pair(file, instream));
}
2024-06-06 14:04:34 +02:00
blitz::parser::~parser() {
2024-06-06 13:37:13 +02:00
while (m_files.size() > 0) {
std::shared_ptr<std::ifstream> file = std::dynamic_pointer_cast<std::ifstream>(m_files.top().second);
file->close();
m_files.pop();
}
}
2024-06-25 18:59:15 +02:00
std::unique_ptr<blitz::ast::expression> blitz::parser::parse() {
std::unique_ptr<ast::ScopeExpression> scope = std::make_unique<ast::ScopeExpression>();
2024-06-06 13:37:13 +02:00
2024-06-25 18:59:15 +02:00
std::unique_ptr<ast::expression> expr;
2024-06-06 14:04:34 +02:00
while ((expr = std::move(parse_expression())) != nullptr) {
2024-06-06 13:37:13 +02:00
scope->AddExpression(std::move(expr));
}
return std::move(scope);
}
2024-06-25 18:59:15 +02:00
void blitz::parser::log(const char* msg, ...) {
2024-06-06 13:37:13 +02:00
std::vector<char> buf(65535);
va_list val;
va_start(val, msg);
int rval = vsnprintf(buf.data(), buf.size(), msg, val);
va_end(val);
std::cout << buf.data() << '\n';
}
2024-06-25 18:59:15 +02:00
void blitz::parser::log_error(const char* msg, ...) {
2024-06-06 13:37:13 +02:00
std::vector<char> buf(65535);
va_list val;
va_start(val, msg);
int rval = vsnprintf(buf.data(), buf.size(), msg, val);
va_end(val);
std::cerr << buf.data() << '\n';
}
2024-06-25 18:59:15 +02:00
std::pair<blitz::lexer::tokentype, std::string> blitz::parser::next() {
return m_lexer.next(m_files.top().second);
2024-06-06 13:37:13 +02:00
}
2024-06-25 18:59:15 +02:00
std::unique_ptr<blitz::ast::expression> blitz::parser::parse_expression() {
2024-06-06 13:37:13 +02:00
while (true) {
2024-06-25 18:59:15 +02:00
auto tkn = next();
log("%s", tkn.second.c_str());
2024-06-06 13:37:13 +02:00
switch (tkn.first) {
2024-06-25 18:59:15 +02:00
case blitz::lexer::tokentype::TokenNewLine:
case blitz::lexer::tokentype::TokenComment:
2024-06-06 13:37:13 +02:00
// Skip Comments, since we don't really need them for the AST.
continue;
2024-06-25 18:59:15 +02:00
case blitz::lexer::tokentype::TokenPlus:
case blitz::lexer::tokentype::TokenMinus:
2024-06-06 13:37:13 +02:00
default: // End Of File / Unknown
2024-06-25 18:59:15 +02:00
case blitz::lexer::tokentype::TokenUnknown:
case blitz::lexer::tokentype::TokenEOF:
2024-06-06 13:37:13 +02:00
return nullptr;
break;
}
}
}
2024-06-25 18:59:15 +02:00
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.");
2024-06-06 13:37:13 +02:00
return nullptr;
}
char* endptr = const_cast<char*>(value.c_str() + value.size());
int32_t parsed = strtol(value.c_str(), &endptr, 10);
if (errno == ERANGE) {
2024-06-25 18:59:15 +02:00
log_error("Number out of range.");
2024-06-06 13:37:13 +02:00
return nullptr;
}
2024-06-25 18:59:15 +02:00
return std::make_unique<blitz::ast::NumberExpression>(parsed);
2024-06-06 13:37:13 +02:00
}
2024-06-25 18:59:15 +02:00
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.");
2024-06-06 13:37:13 +02:00
return nullptr;
}
char* endptr = const_cast<char*>(value.c_str() + value.size());
float_t parsed = strtof(value.c_str(), &endptr);
if (errno == ERANGE) {
2024-06-25 18:59:15 +02:00
log_error("Number out of range.");
2024-06-06 13:37:13 +02:00
return nullptr;
}
2024-06-25 18:59:15 +02:00
return std::make_unique<blitz::ast::DecimalExpression>(parsed);
2024-06-06 13:37:13 +02:00
}
2024-06-26 00:31:06 +02:00
*/