feat: basic lexer + parser with basic example

This commit is contained in:
2025-11-26 22:26:10 +01:00
parent 1975059b1b
commit 7febbb80d4
10 changed files with 414 additions and 1 deletions

View File

@ -1,8 +1,55 @@
#include <iostream>
#include <fstream>
#include <print>
#include "lexer.hpp"
#include "ast.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 == Id)
std::println("id = {}", lexer->token().string);
else if (lexer->token().token == IntLiteral)
std::println("int = {}", lexer->token().int_number);
else
std::println("token = {}", (char)lexer->token().token);
}
}
int main(int argc, char** argv)
{
std::println("Hello, World!");
for (int i = 0; i < argc; ++i) {
std::println("arg#{}: {}", i, argv[i]);
}
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();
// std::println("{}", content);
Lexer lexer(filename, StringView(content.c_str(), content.size()));
// dump_tokens(filename, &lexer);
AstParser parser(&lexer);
auto node = parser.ParseStatement();
ExternNode* extrn = reinterpret_cast<ExternNode*>(node);
return 0;
}