Skip to content

Commit

Permalink
feat: Added std imports
Browse files Browse the repository at this point in the history
  • Loading branch information
alinalihassan committed May 6, 2022
1 parent c785973 commit be3e0eb
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/liblesma/AST/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,23 +115,26 @@ namespace lesma {
class Import : public Statement {
std::string file_path;
std::string alias;
bool std;

public:
Import(Span Loc, std::string file_path, std::string alias) : Statement(Loc), file_path(std::move(file_path)), alias(std::move(alias)){};
Import(Span Loc, std::string file_path, std::string alias, bool std) : Statement(Loc), file_path(std::move(file_path)), alias(std::move(alias)), std(std){};
~Import() override = default;

[[nodiscard]] [[maybe_unused]] std::string getFilePath() const { return file_path; }
[[nodiscard]] [[maybe_unused]] std::string getAlias() const { return alias; }
[[nodiscard]] [[maybe_unused]] bool isStd() const { return std; }

std::string toString(int ind) override {
return fmt::format("{}Import[Line({}-{}):Col({}-{})]: {} as {}\n",
return fmt::format("{}Import[Line({}-{}):Col({}-{})]: {} as {} from {}\n",
std::string(ind, ' '),
getStart().Line,
getEnd().Line,
getStart().Col,
getEnd().Col,
file_path,
alias);
alias,
std ? "std" : "file");
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/liblesma/Backend/Codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ std::unique_ptr<llvm::TargetMachine> Codegen::InitializeTargetMachine() {
return target_machine;
}

void Codegen::CompileModule(Span span, const std::string &filepath) {
void Codegen::CompileModule(Span span, const std::string &filepath, bool isStd) {
std::filesystem::path mainPath = filename;
// Read source
auto source = readFile(fmt::format("{}/{}", std::filesystem::absolute(mainPath).parent_path().c_str(), filepath));
auto source = readFile(isStd ? filepath : fmt::format("{}/{}", std::filesystem::absolute(mainPath).parent_path().c_str(), filepath));

try {
// Lexer
Expand Down Expand Up @@ -565,7 +565,7 @@ void Codegen::visit(ExpressionStatement *node) {

// TODO: Implement me
void Codegen::visit(Import *node) {
CompileModule(node->getSpan(), node->getFilePath());
CompileModule(node->getSpan(), node->getFilePath(), node->isStd());
}

llvm::Value *Codegen::visit(FuncCall *node) {
Expand Down
2 changes: 1 addition & 1 deletion src/liblesma/Backend/Codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ namespace lesma {
protected:
std::unique_ptr<llvm::TargetMachine> InitializeTargetMachine();
llvm::Function *InitializeTopLevel();
void CompileModule(Span span, const std::string &filepath);
void CompileModule(Span span, const std::string &filepath, bool isStd);

void visit(Statement *node) override;
void visit(Compound *node) override;
Expand Down
5 changes: 5 additions & 0 deletions src/liblesma/Common/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ namespace lesma {
lineNum++;
}
}

std::string getStdDir() {
std::string file_path = __FILE__;
return file_path.substr(0, file_path.rfind("src") + 3) + "/std/";
}
}// namespace lesma
1 change: 1 addition & 0 deletions src/liblesma/Common/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@ namespace lesma {
std::string readFile(const std::string &path);
void showInline(Span span, const std::string &reason, const std::string &file, bool is_error);
std::string getBasename(const std::string &file_path);
std::string getStdDir();
}// namespace lesma
19 changes: 15 additions & 4 deletions src/liblesma/Frontend/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,17 +424,28 @@ Statement *Parser::ParseImport() {
auto loc = Peek()->span;
Consume(TokenType::IMPORT);

auto token = Consume(TokenType::STRING);
auto filepath = token->lexeme.erase(0, 1).erase(token->lexeme.size() - 1);
Token token;
std::string filepath;

if (Peek()->type == TokenType::STRING) {
token = Consume(TokenType::STRING);
filepath = token->lexeme.erase(0, 1).erase(token->lexeme.size() - 1);
} else if (Peek()->type == TokenType::IDENTIFIER) {
token = Consume(TokenType::IDENTIFIER);
filepath = getStdDir() + token->lexeme + ".les";
} else {
Error(Peek(), "Imports must be either strings for files or identifiers for standard library");
return nullptr;
}

if (AdvanceIfMatchAny<TokenType::AS>()) {
auto alias = Consume(TokenType::IDENTIFIER);
ConsumeNewline();
return new Import({loc.Start, alias.getEnd()}, token->lexeme, alias->lexeme);
return new Import({loc.Start, alias.getEnd()}, token->lexeme, alias->lexeme, token->type == TokenType::IDENTIFIER);
}

ConsumeNewline();
return new Import({loc.Start, token.getEnd()}, filepath, getBasename(token->lexeme));
return new Import({loc.Start, token.getEnd()}, filepath, getBasename(token->lexeme), token->type == TokenType::IDENTIFIER);
}

Compound *Parser::ParseCompound() {
Expand Down

0 comments on commit be3e0eb

Please sign in to comment.