feat: added simple import support

This commit is contained in:
lisk77 2025-07-01 23:52:19 +02:00
parent c14c50dee1
commit 9ff3943819
6 changed files with 61 additions and 3 deletions

View file

@ -42,5 +42,5 @@ For a file to be able to be run, it needs to define a `main` function.
## Future ideas
- [ ] types
- [ ] imports
- [x] imports (at least simple)
- [ ] small standard library

View file

@ -6,7 +6,8 @@
// ast stuff lul
struct Expr {
virtual ~Expr() = default;
virtual ~Expr() = default;
virtual std::unique_ptr<Expr> clone() const = 0;
};
// represents a simple symbol like "x" or even "thisIsACoolFunctionTrustme100"
@ -16,6 +17,9 @@ struct Variable : Expr {
std::string name;
Variable(std::string n) : name(std::move(n)) {}
std::unique_ptr<Expr> clone() const override {
return std::make_unique<Variable>(name);
}
};
// represents a function itself like \ x . x
@ -24,6 +28,9 @@ struct Abstraction : Expr {
std::unique_ptr<Expr> body;
Abstraction(std::string p, std::unique_ptr<Expr> b) : param(std::move(p)), body(std::move(b)) {}
std::unique_ptr<Expr> clone() const override {
return std::make_unique<Abstraction>(param, body->clone());
}
};
// wouldnt be turing complete if we couldnt apply to these functions innit
@ -34,6 +41,9 @@ struct Application : Expr {
std::unique_ptr<Expr> left, right;
Application(std::unique_ptr<Expr> l, std::unique_ptr<Expr> r) : left(std::move(l)), right(std::move(r)) {}
std::unique_ptr<Expr> clone() const override {
return std::make_unique<Application>(left->clone(), right->clone());
}
};
#endif // AST_HPP

View file

@ -14,7 +14,8 @@ typedef enum {
LPAREN,
RPAREN,
VARIABLE,
EQUALS
EQUALS,
IMPORT
} TokenType;
typedef struct {

View file

@ -55,6 +55,25 @@ std::vector<Token> Lexer::tokenize() {
position++;
}
std::string lex = src.substr(start, position - start);
if (lex == "import") {
tokens.push_back({ IMPORT, lex, start, position });
position++;
size_t startPath = position;
while (position < len && !IS_SPACE(src[position]) && src[position] != ';')
{
position++;
}
if (position >= len) {
std::cerr << "lexer: import statement was not closed with semicolon" << std::endl;
this->error_flag = true;
continue;
}
std::string path = src.substr(startPath, position - startPath);
tokens.push_back({ VARIABLE, path, startPath, position });
continue;
}
tokens.push_back({ VARIABLE, lex, start, position });
}
else if (IS_SPACE(curr)) {

View file

@ -1,4 +1,5 @@
#include "parser.hpp"
#include "lexer.hpp"
#include "ast.hpp"
#include <stdexcept>
@ -6,6 +7,7 @@
#include <string>
#include <memory>
#include <iostream>
#include <fstream>
Parser::Parser(std::vector<Token> toks)
: tokens(std::move(toks))
@ -88,6 +90,31 @@ std::unique_ptr<Expr> Parser::parseSimple() {
return e;
}
if (peek().type == TokenType::IMPORT) {
position++;
const auto &path = get();
std::ifstream file(path.lexeme, std::ios::binary);
if (!file) {
throw std::runtime_error("import: unable to open file: " + path.lexeme);
}
std::string content(std::istreambuf_iterator<char>(file), {});
Lexer lexImp(content);
auto tokensImp = lexImp.tokenize();
Parser parserImp(std::move(tokensImp));
parserImp.parseProgram();
std::cout << "importing " << path.lexeme << std::endl;
for (const auto &entry : parserImp.definitions()) {
defs.emplace(entry.first, entry.second->clone());
}
return nullptr;
}
throw std::runtime_error("parser: expected \\, variable, or '(' at position " + std::to_string(peek().start));
}

View file

@ -14,6 +14,7 @@ std::string display_tokentype(TokenType type) {
case RPAREN : return "RPAREN";
case EQUALS : return "EQUALS";
case VARIABLE : return "VARIABLE";
case IMPORT : return "IMPORT";
default : return "";
}
}