Skip to content

Commit

Permalink
fix: Import path is now relative to the calling file. Added test
Browse files Browse the repository at this point in the history
  • Loading branch information
alinalihassan committed Mar 8, 2022
1 parent a8e9fee commit 0af010d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Backend/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Codegen::Codegen(std::unique_ptr<Parser> parser, const std::string &filename, bo
Scope = new SymbolTable(nullptr);
TargetMachine = InitializeTargetMachine();

this->filename = filename;
isJIT = jit;
isMain = main;

Expand Down Expand Up @@ -53,8 +54,9 @@ llvm::TargetMachine *Codegen::InitializeTargetMachine() {
}

void Codegen::CompileModule(const std::string &filepath) {
std::filesystem::path mainPath = filename;
// Read source
auto source = readFile(filepath);
auto source = readFile(fmt::format("{}/{}", std::filesystem::absolute(mainPath).parent_path().c_str(), filepath));
// Lexer
auto lexer = std::make_unique<Lexer>(source, filepath.substr(filepath.find_last_of("/\\") + 1));
lexer->ScanAll();
Expand Down
1 change: 1 addition & 0 deletions src/Backend/Codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "AST/StmtVisitor.h"
#include "Frontend/Parser.h"
#include "LesmaJIT.h"
#include <filesystem>
#include "Symbol/SymbolTable.h"
#include <clang/Driver/Driver.h>
#include <llvm/Analysis/CGSCCPassManager.h>
Expand Down
6 changes: 6 additions & 0 deletions tests/lesma/import.les
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ret=101
import "functioncall.les"

def extern exit(x: int)

exit(fib())

0 comments on commit 0af010d

Please sign in to comment.