mirror of
https://github.com/PashaBibko/LX.git
synced 2026-04-04 01:49:05 +00:00
Allowed multiple functions + non "main" functions
Also fixed a bug in the lexer and changed how AST is logged.
This commit is contained in:
@@ -14,16 +14,35 @@ namespace LX
|
||||
return true;
|
||||
}
|
||||
|
||||
static llvm::GlobalValue::LinkageTypes GetLinkageType(const std::string& funcName)
|
||||
{
|
||||
if (funcName == "main")
|
||||
{
|
||||
return llvm::Function::ExternalLinkage;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
return llvm::GlobalValue::InternalLinkage;
|
||||
}
|
||||
}
|
||||
|
||||
// Generates the LLVM IR for the given function //
|
||||
static void GenerateFunctionIR(FunctionDefinition& funcAST, InfoLLVM& LLVM)
|
||||
{
|
||||
// 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, funcAST.name, LLVM.module);
|
||||
llvm::BasicBlock* entry = llvm::BasicBlock::Create(LLVM.context, "entry", func);
|
||||
llvm::Function* func = llvm::Function::Create(retType, GetLinkageType(funcAST.name), funcAST.name, LLVM.module);
|
||||
llvm::BasicBlock* entry = llvm::BasicBlock::Create(LLVM.context, funcAST.name + "-entry", func);
|
||||
LLVM.builder.SetInsertPoint(entry);
|
||||
|
||||
// Adds the function's parameters to the scope //
|
||||
for (std::string& param : funcAST.params)
|
||||
{
|
||||
LLVM.scope->CreateVar(param, LLVM);
|
||||
}
|
||||
|
||||
// Generates the IR within the function by looping over the nodes //
|
||||
for (auto& node : funcAST.body)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user