Improved lexer debugging

This commit is contained in:
Pasha Bibko
2025-04-21 14:23:47 +01:00
parent a560c53c58
commit 49e4bba510
15 changed files with 405 additions and 98 deletions

View File

@@ -11,7 +11,7 @@ namespace LX
// Reserves space for nodes (stops excess allocations) //
FunctionDefinition::FunctionDefinition()
: body{}
: body{}, name{}
{ body.reserve(32); }
// Reserves space for functions (stops excess allocations) //

View File

@@ -8,7 +8,7 @@
namespace LX
{
// Tells the generator if the current node is allowed to be within a top-level context //
// TODO: Make this function do something other than return true
// TODO: Make this function do something other than return true //
static constexpr bool IsValidTopLevelNode(AST::Node::NodeType type)
{
return true;
@@ -20,7 +20,7 @@ namespace LX
// Creates the functions signature and return type //
llvm::FunctionType* retType = llvm::FunctionType::get(llvm::Type::getInt32Ty(LLVM.context), false); // <- Defaults to int currently
llvm::Function* func = llvm::Function::Create(retType, llvm::Function::ExternalLinkage, "main", LLVM.module); // Defaults to main currently
llvm::Function* func = llvm::Function::Create(retType, llvm::Function::ExternalLinkage, funcAST.name, LLVM.module);
llvm::BasicBlock* entry = llvm::BasicBlock::Create(LLVM.context, "entry", func);
LLVM.builder.SetInsertPoint(entry);
@@ -42,10 +42,10 @@ namespace LX
}
// Turns an abstract binary tree into LLVM intermediate representation //
void GenerateIR(FileAST& ast)
void GenerateIR(FileAST& ast, const std::string& name)
{
// Creates the LLVM variables needed for generating IR that are shared between functions //
InfoLLVM LLVM("add_itns");
InfoLLVM LLVM(name);
// Loops over the functions to generate their LLVM IR //
for (auto& func : ast.functions)

View File

@@ -7,6 +7,12 @@
namespace LX
{
template<Token::TokenType type>
static inline void ExpectToken(const Token& t)
{
ThrowIf<int>(type != t.type);
}
// Local struct so everything can be public //
struct Parser
{
@@ -118,14 +124,17 @@ namespace LX
{
case Token::FUNCTION:
{
// Skips over function token + name token
// TODO: Store function name in the type
p.index++; p.index++;
// Skips over function token + name token //
p.index++;
// Pushes a new function to the vector and gets a reference to it for adding the body //
output.functions.emplace_back();
FunctionDefinition& func = output.functions.back();
// Assigns the function name //
ExpectToken<Token::IDENTIFIER>(p.tokens[p.index]);
func.name = p.tokens[p.index++].contents;
// Loops over the body until it reaches the end //
// TODO: Detect the end instead of looping over the entire token vector
while (p.index < p.len)
@@ -144,7 +153,8 @@ namespace LX
// Lets the user know there is an error //
// TODO: Makes this error actually output useful information //
default:
std::cout << "UNKNOWN TOKEN FOUND" << std::endl;
std::cout << "UNKNOWN TOKEN FOUND: " << p.tokens[p.index].type << std::endl;
return output;
}
}