mirror of
https://github.com/lisk77/lambda.git
synced 2025-10-24 02:08:49 +00:00
feat: added simple import support
This commit is contained in:
parent
c14c50dee1
commit
9ff3943819
6 changed files with 61 additions and 3 deletions
|
|
@ -42,5 +42,5 @@ For a file to be able to be run, it needs to define a `main` function.
|
||||||
## Future ideas
|
## Future ideas
|
||||||
|
|
||||||
- [ ] types
|
- [ ] types
|
||||||
- [ ] imports
|
- [x] imports (at least simple)
|
||||||
- [ ] small standard library
|
- [ ] small standard library
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@
|
||||||
// ast stuff lul
|
// ast stuff lul
|
||||||
struct Expr {
|
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"
|
// represents a simple symbol like "x" or even "thisIsACoolFunctionTrustme100"
|
||||||
|
|
@ -16,6 +17,9 @@ struct Variable : Expr {
|
||||||
std::string name;
|
std::string name;
|
||||||
|
|
||||||
Variable(std::string n) : name(std::move(n)) {}
|
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
|
// represents a function itself like \ x . x
|
||||||
|
|
@ -24,6 +28,9 @@ struct Abstraction : Expr {
|
||||||
std::unique_ptr<Expr> body;
|
std::unique_ptr<Expr> body;
|
||||||
|
|
||||||
Abstraction(std::string p, std::unique_ptr<Expr> b) : param(std::move(p)), body(std::move(b)) {}
|
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
|
// 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;
|
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)) {}
|
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
|
#endif // AST_HPP
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,8 @@ typedef enum {
|
||||||
LPAREN,
|
LPAREN,
|
||||||
RPAREN,
|
RPAREN,
|
||||||
VARIABLE,
|
VARIABLE,
|
||||||
EQUALS
|
EQUALS,
|
||||||
|
IMPORT
|
||||||
} TokenType;
|
} TokenType;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,25 @@ std::vector<Token> Lexer::tokenize() {
|
||||||
position++;
|
position++;
|
||||||
}
|
}
|
||||||
std::string lex = src.substr(start, position - start);
|
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 });
|
tokens.push_back({ VARIABLE, lex, start, position });
|
||||||
}
|
}
|
||||||
else if (IS_SPACE(curr)) {
|
else if (IS_SPACE(curr)) {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
|
#include "lexer.hpp"
|
||||||
#include "ast.hpp"
|
#include "ast.hpp"
|
||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
@ -6,6 +7,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
Parser::Parser(std::vector<Token> toks)
|
Parser::Parser(std::vector<Token> toks)
|
||||||
: tokens(std::move(toks))
|
: tokens(std::move(toks))
|
||||||
|
|
@ -88,6 +90,31 @@ std::unique_ptr<Expr> Parser::parseSimple() {
|
||||||
return e;
|
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));
|
throw std::runtime_error("parser: expected \\, variable, or '(' at position " + std::to_string(peek().start));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ std::string display_tokentype(TokenType type) {
|
||||||
case RPAREN : return "RPAREN";
|
case RPAREN : return "RPAREN";
|
||||||
case EQUALS : return "EQUALS";
|
case EQUALS : return "EQUALS";
|
||||||
case VARIABLE : return "VARIABLE";
|
case VARIABLE : return "VARIABLE";
|
||||||
|
case IMPORT : return "IMPORT";
|
||||||
default : return "";
|
default : return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue