Files
pl/src/main.cpp

66 lines
1.6 KiB
C++

#include <iostream>
#include <fstream>
#include <print>
#include "parser/lexer.hpp"
#include "parser/ast.hpp"
#include "ir/ir.hpp"
#include "codegen/fasm_stack.hpp"
void dump_tokens(const char* filename, Lexer* lexer)
{
while (lexer->NextToken()) {
std::print("{}:{}:{}: ", filename, lexer->token().line_number, lexer->token().offset_start);
if (lexer->token().token == TokenType::Id)
std::println("id = {}", lexer->token().string);
else if (lexer->token().token == TokenType::IntLiteral)
std::println("int = {}", lexer->token().int_number);
else
std::println("token = {}", (char)lexer->token().token);
}
}
int main(int argc, char** argv)
{
char* filename;
if (argc > 1) {
filename = (++argv)[0];
} else {
fprintf(stderr, "ERROR: Input file is required.\n");
return 1;
}
std::ifstream f(filename);
if (!f.is_open()) {
fprintf(stderr, "ERROR: Failed to open input file: %s\n", filename);
return 1;
}
std::string content((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
f.close();
Lexer lexer(filename, StringView(content.c_str()));
AstParser parser(&lexer);
auto program = parser.Parse();
IR::IRBuilder irBuilder(program);
auto ops = irBuilder.Build();
for (size_t i = 0; i < ops.size; ++i)
{
printf("%s\n", ops.data[i]->Format(0).c_str());
}
// StackFasmX86_64Generator gen;
// gen.Generate(filename, ops);
// printf("%s\n", gen.GetOutput().c_str());
return 0;
}