From 14208a27f5e793a94d76b331a1d20603472d7cf2 Mon Sep 17 00:00:00 2001 From: LeonardoBizzoni Date: Fri, 6 Sep 2024 15:12:38 +0200 Subject: [PATCH] Changed `src` file structure --- src/{ => AST-generation}/expressions.hpp | 44 ++-- src/{ => AST-generation}/lexer.cpp | 2 - src/{ => AST-generation}/lexer.hpp | 4 +- src/{ => AST-generation}/parser.cpp | 1 - src/{ => AST-generation}/parser.hpp | 9 +- src/{ => AST-generation}/statements.hpp | 29 ++- src/{ => AST-generation}/syntax_error.cpp | 0 src/{ => AST-generation}/syntax_error.hpp | 2 +- src/{ => AST-generation/tokens}/token.cpp | 0 src/{ => AST-generation/tokens}/token.hpp | 0 .../tokens}/token_type.hpp | 0 src/LBPLClass.cpp | 30 --- src/ast_printer.cpp | 215 ------------------ src/ast_printer.hpp | 36 --- src/common.hpp | 10 - src/{ => interpretation}/builtin_methods.hpp | 2 +- src/{ => interpretation}/environment.cpp | 2 +- src/{ => interpretation}/environment.hpp | 4 +- src/{ => interpretation}/interpreter.cpp | 8 +- src/{ => interpretation}/interpreter.hpp | 6 +- src/{ => interpretation}/resolver.cpp | 6 +- src/{ => interpretation}/resolver.hpp | 8 +- src/{ => interpretation}/runtime_error.cpp | 0 src/{ => interpretation}/runtime_error.hpp | 5 +- src/{ => interpretation}/tree_nodes.hpp | 0 .../types}/LBPLCallable.hpp | 0 src/interpretation/types/LBPLClass.cpp | 30 +++ src/{ => interpretation/types}/LBPLClass.hpp | 0 .../types}/LBPLFunction.cpp | 2 +- .../types}/LBPLFunction.hpp | 4 +- .../types}/LBPLInstance.cpp | 2 +- .../types}/LBPLInstance.hpp | 0 src/{ => interpretation/types}/LBPLTypes.hpp | 0 src/{ => interpretation}/visitor.hpp | 2 +- src/main.cpp | 37 ++- src/main.hpp | 18 -- 36 files changed, 113 insertions(+), 405 deletions(-) rename src/{ => AST-generation}/expressions.hpp (81%) rename src/{ => AST-generation}/lexer.cpp (99%) rename src/{ => AST-generation}/lexer.hpp (97%) rename src/{ => AST-generation}/parser.cpp (99%) rename src/{ => AST-generation}/parser.hpp (96%) rename src/{ => AST-generation}/statements.hpp (81%) rename src/{ => AST-generation}/syntax_error.cpp (100%) rename src/{ => AST-generation}/syntax_error.hpp (96%) rename src/{ => AST-generation/tokens}/token.cpp (100%) rename src/{ => AST-generation/tokens}/token.hpp (100%) rename src/{ => AST-generation/tokens}/token_type.hpp (100%) delete mode 100644 src/LBPLClass.cpp delete mode 100755 src/ast_printer.cpp delete mode 100755 src/ast_printer.hpp delete mode 100755 src/common.hpp rename src/{ => interpretation}/builtin_methods.hpp (97%) rename src/{ => interpretation}/environment.cpp (98%) rename src/{ => interpretation}/environment.hpp (92%) rename src/{ => interpretation}/interpreter.cpp (99%) rename src/{ => interpretation}/interpreter.hpp (93%) rename src/{ => interpretation}/resolver.cpp (97%) rename src/{ => interpretation}/resolver.hpp (91%) rename src/{ => interpretation}/runtime_error.cpp (100%) rename src/{ => interpretation}/runtime_error.hpp (88%) rename src/{ => interpretation}/tree_nodes.hpp (100%) rename src/{ => interpretation/types}/LBPLCallable.hpp (100%) create mode 100644 src/interpretation/types/LBPLClass.cpp rename src/{ => interpretation/types}/LBPLClass.hpp (100%) rename src/{ => interpretation/types}/LBPLFunction.cpp (96%) rename src/{ => interpretation/types}/LBPLFunction.hpp (91%) rename src/{ => interpretation/types}/LBPLInstance.cpp (95%) rename src/{ => interpretation/types}/LBPLInstance.hpp (100%) rename src/{ => interpretation/types}/LBPLTypes.hpp (100%) rename src/{ => interpretation}/visitor.hpp (97%) delete mode 100755 src/main.hpp diff --git a/src/expressions.hpp b/src/AST-generation/expressions.hpp similarity index 81% rename from src/expressions.hpp rename to src/AST-generation/expressions.hpp index a774eb1..0a21b00 100755 --- a/src/expressions.hpp +++ b/src/AST-generation/expressions.hpp @@ -1,20 +1,18 @@ #ifndef EXPRESSIONS_H #define EXPRESSIONS_H -#include "token.hpp" +#include "tokens/token.hpp" -#include "token.hpp" -#include "visitor.hpp" -#include +#include "../interpretation/visitor.hpp" #include #include struct Expr { - std::string file; + const char *file; int line, column; - Expr(int line, int column, const std::string &filename) + Expr(int line, int column, const char *filename) : line(line), column(column), file(filename) {} virtual ~Expr() {}; @@ -26,12 +24,12 @@ struct BinaryExpr : public Expr { std::unique_ptr right; std::shared_ptr op; - BinaryExpr(int line, int column, const std::string &file, + BinaryExpr(int line, int column, const char *file, std::unique_ptr &left, std::unique_ptr &right, std::shared_ptr &op) : left(std::move(left)), right(std::move(right)), op(op), Expr(line, column, file) {} - BinaryExpr(int line, int column, const std::string &file, + BinaryExpr(int line, int column, const char *file, std::unique_ptr &left, std::unique_ptr &&right, std::shared_ptr &op) : left(std::move(left)), right(std::move(right)), op(op), @@ -43,7 +41,7 @@ struct BinaryExpr : public Expr { }; struct BreakExpr : public Expr { - BreakExpr(int line, int column, const std::string &file) + BreakExpr(int line, int column, const char *file) : Expr(line, column, file) {} Value accept(Expression::Visitor *visitor) { @@ -52,7 +50,7 @@ struct BreakExpr : public Expr { }; struct ContinueExpr : public Expr { - ContinueExpr(int line, int column, const std::string &file) + ContinueExpr(int line, int column, const char *file) : Expr(line, column, file) {} Value accept(Expression::Visitor *visitor) { return visitor->visitContinueExpr(this); @@ -63,8 +61,8 @@ struct UnaryExpr : public Expr { std::unique_ptr right; std::shared_ptr op; - UnaryExpr(int line, int column, const std::string &file, - std::unique_ptr right, std::shared_ptr &op) + UnaryExpr(int line, int column, const char *file, std::unique_ptr right, + std::shared_ptr &op) : right(std::move(right)), op(op), Expr(line, column, file) {} Value accept(Expression::Visitor *visitor) { return visitor->visitUnaryExpr(this); @@ -74,10 +72,10 @@ struct UnaryExpr : public Expr { struct LiteralExpr : public Expr { std::shared_ptr token; - LiteralExpr(int line, int column, const std::string &file, + LiteralExpr(int line, int column, const char *file, std::shared_ptr &literal) : token(literal), Expr(line, column, file) {} - LiteralExpr(int line, int column, const std::string &file, + LiteralExpr(int line, int column, const char *file, std::shared_ptr &&literal) : token(literal), Expr(line, column, file) {} Value accept(Expression::Visitor *visitor) { @@ -88,7 +86,7 @@ struct LiteralExpr : public Expr { struct SuperExpr : public Expr { std::shared_ptr field; - SuperExpr(int line, int column, const std::string &file, + SuperExpr(int line, int column, const char *file, std::shared_ptr &field) : field(field), Expr(line, column, file) {} Value accept(Expression::Visitor *visitor) { @@ -100,7 +98,7 @@ struct ThisExpr : public Expr { public: std::shared_ptr keyword; - ThisExpr(int line, int column, const std::string &file, + ThisExpr(int line, int column, const char *file, std::shared_ptr &keyword) : keyword(keyword), Expr(line, column, file) {} @@ -112,7 +110,7 @@ public: struct GroupingExpr : public Expr { std::unique_ptr expr; - GroupingExpr(int line, int column, const std::string &file, + GroupingExpr(int line, int column, const char *file, std::unique_ptr &expr) : expr(std::move(expr)), Expr(line, column, file) {} Value accept(Expression::Visitor *visitor) { @@ -123,7 +121,7 @@ struct GroupingExpr : public Expr { struct VariableExpr : public Expr { std::shared_ptr variable; - VariableExpr(int line, int column, const std::string &file, + VariableExpr(int line, int column, const char *file, std::shared_ptr &variable) : variable(variable), Expr(line, column, file) {} Value accept(Expression::Visitor *visitor) { @@ -135,7 +133,7 @@ struct AssignExpr : public Expr { std::shared_ptr variable; std::unique_ptr value; - AssignExpr(int line, int column, const std::string &file, + AssignExpr(int line, int column, const char *file, std::shared_ptr &variable, std::unique_ptr &value) : variable(variable), value(std::move(value)), Expr(line, column, file) {} @@ -149,7 +147,7 @@ struct FnCallExpr : public Expr { std::unique_ptr callee; std::vector> args; - FnCallExpr(int line, int column, const std::string &file, + FnCallExpr(int line, int column, const char *file, std::unique_ptr &callee, std::vector> &args) : callee(std::move(callee)), args(std::move(args)), @@ -165,7 +163,7 @@ struct TernaryExpr : public Expr { std::unique_ptr trueBranch; std::unique_ptr falseBranch; - TernaryExpr(int line, int column, const std::string &file, + TernaryExpr(int line, int column, const char *file, std::unique_ptr &condition, std::unique_ptr &trueBranch, std::unique_ptr &&falseBranch) @@ -181,7 +179,7 @@ struct GetFieldExpr : public Expr { std::shared_ptr field; std::unique_ptr instance; - GetFieldExpr(int line, int column, const std::string &file, + GetFieldExpr(int line, int column, const char *file, std::unique_ptr &instance, std::shared_ptr &field) : instance(std::move(instance)), field(field), Expr(line, column, file) {} @@ -196,7 +194,7 @@ struct SetFieldExpr : public Expr { std::unique_ptr value; std::unique_ptr instance; - SetFieldExpr(int line, int column, const std::string &file, + SetFieldExpr(int line, int column, const char *file, std::unique_ptr &instance, std::shared_ptr &field, std::unique_ptr &value) diff --git a/src/lexer.cpp b/src/AST-generation/lexer.cpp similarity index 99% rename from src/lexer.cpp rename to src/AST-generation/lexer.cpp index 8c0914d..8696a38 100755 --- a/src/lexer.cpp +++ b/src/AST-generation/lexer.cpp @@ -1,6 +1,4 @@ #include "lexer.hpp" -#include "token.hpp" -#include "token_type.hpp" #include #include diff --git a/src/lexer.hpp b/src/AST-generation/lexer.hpp similarity index 97% rename from src/lexer.hpp rename to src/AST-generation/lexer.hpp index e1a7b56..5cac465 100755 --- a/src/lexer.hpp +++ b/src/AST-generation/lexer.hpp @@ -1,8 +1,8 @@ #ifndef LEXER_H #define LEXER_H -#include "token.hpp" -#include "token_type.hpp" +#include "tokens/token.hpp" +#include "tokens/token_type.hpp" #include #include diff --git a/src/parser.cpp b/src/AST-generation/parser.cpp similarity index 99% rename from src/parser.cpp rename to src/AST-generation/parser.cpp index 3b9fd47..239f921 100755 --- a/src/parser.cpp +++ b/src/AST-generation/parser.cpp @@ -1,6 +1,5 @@ #include "parser.hpp" #include "syntax_error.hpp" -#include "token_type.hpp" #include #include diff --git a/src/parser.hpp b/src/AST-generation/parser.hpp similarity index 96% rename from src/parser.hpp rename to src/AST-generation/parser.hpp index a579adc..88106bb 100755 --- a/src/parser.hpp +++ b/src/AST-generation/parser.hpp @@ -4,8 +4,7 @@ #include "expressions.hpp" #include "lexer.hpp" #include "statements.hpp" -#include "syntax_error.hpp" -#include "token_type.hpp" +#include "tokens/token_type.hpp" #include #include @@ -20,14 +19,16 @@ class Parser { public: Parser(std::ifstream &file, const char *filename) : source(Lexer::Source(file, filename)), - current(Lexer::getNextToken(this->source)), previous(current) { + current(Lexer::getNextToken(this->source)), previous(current), + hadError(false) { importedFiles.insert(filename); } Parser(std::ifstream &file, const char *filename, std::unordered_set &importedFiles) : importedFiles(importedFiles), source(Lexer::Source(file, filename)), - current(Lexer::getNextToken(this->source)), previous(current) {} + current(Lexer::getNextToken(this->source)), previous(current), + hadError(false) {} std::vector> parse(); diff --git a/src/statements.hpp b/src/AST-generation/statements.hpp similarity index 81% rename from src/statements.hpp rename to src/AST-generation/statements.hpp index 9233113..7f7f278 100755 --- a/src/statements.hpp +++ b/src/AST-generation/statements.hpp @@ -5,10 +5,10 @@ #include struct Stmt { - std::string filename; + const char *filename; int line, column; - Stmt(int line, int column, const std::string &filename) + Stmt(int line, int column, const char *filename) : line(line), column(column), filename(filename) {} virtual ~Stmt() {} @@ -20,7 +20,7 @@ struct FnStmt : public Stmt { std::vector> args; std::vector> body; - FnStmt(int line, int column, const std::string &file, + FnStmt(int line, int column, const char *file, std::shared_ptr &name, std::vector> &args, std::vector> &&body) @@ -34,7 +34,7 @@ struct VarStmt : public Stmt { std::shared_ptr name; std::unique_ptr value; - VarStmt(int line, int column, const std::string &file, + VarStmt(int line, int column, const char *file, std::shared_ptr &name, std::unique_ptr &value) : name(name), value(std::move(value)), Stmt(line, column, file) {} @@ -46,7 +46,7 @@ struct ClassStmt : public Stmt { std::unique_ptr superclass; std::vector> body; - ClassStmt(int line, int column, const std::string &file, + ClassStmt(int line, int column, const char *file, std::shared_ptr &name, std::unique_ptr &superclass, std::vector> &&body) @@ -61,7 +61,7 @@ struct IfStmt : public Stmt { std::unique_ptr trueBranch; std::unique_ptr falseBranch; - IfStmt(int line, int column, const std::string &file, + IfStmt(int line, int column, const char *file, std::unique_ptr &condition, std::unique_ptr &trueBranch, std::unique_ptr &falseBranch) : condition(std::move(condition)), trueBranch(std::move(trueBranch)), @@ -74,13 +74,13 @@ struct WhileStmt : public Stmt { std::unique_ptr condition; std::unique_ptr body; - WhileStmt(int line, int column, const std::string &file, - std::unique_ptr &cond, std::unique_ptr &body) + WhileStmt(int line, int column, const char *file, std::unique_ptr &cond, + std::unique_ptr &body) : condition(std::move(cond)), body(std::move(body)), Stmt(line, column, file) {} - WhileStmt(int line, int column, const std::string &file, - std::unique_ptr &cond, std::unique_ptr &&body) + WhileStmt(int line, int column, const char *file, std::unique_ptr &cond, + std::unique_ptr &&body) : condition(std::move(cond)), body(std::move(body)), Stmt(line, column, file) {} @@ -91,7 +91,7 @@ struct ForStmt : public Stmt { std::unique_ptr increment, condition; std::unique_ptr initializer, body; - ForStmt(int line, int column, const std::string &file, + ForStmt(int line, int column, const char *file, std::unique_ptr &initializer, std::unique_ptr &cond, std::unique_ptr &increment, std::unique_ptr &body) : initializer(std::move(initializer)), condition(std::move(cond)), @@ -104,7 +104,7 @@ struct ForStmt : public Stmt { struct ScopedStmt : public Stmt { std::vector> body; - ScopedStmt(int line, int column, const std::string &file, + ScopedStmt(int line, int column, const char *file, std::vector> &&body) : body(std::move(body)), Stmt(line, column, file) {} void accept(Statement::Visitor *visitor) { visitor->visitScopedStmt(this); } @@ -113,8 +113,7 @@ struct ScopedStmt : public Stmt { struct ExprStmt : public Stmt { std::unique_ptr expr; - ExprStmt(int line, int column, const std::string &file, - std::unique_ptr &&expr) + ExprStmt(int line, int column, const char *file, std::unique_ptr &&expr) : expr(std::move(expr)), Stmt(line, column, file) {} void accept(Statement::Visitor *visitor) { visitor->visitExprStmt(this); } @@ -123,7 +122,7 @@ struct ExprStmt : public Stmt { struct ReturnStmt : public Stmt { std::unique_ptr value; - ReturnStmt(int line, int column, const std::string &file, + ReturnStmt(int line, int column, const char *file, std::unique_ptr &value) : value(std::move(value)), Stmt(line, column, file) {} diff --git a/src/syntax_error.cpp b/src/AST-generation/syntax_error.cpp similarity index 100% rename from src/syntax_error.cpp rename to src/AST-generation/syntax_error.cpp diff --git a/src/syntax_error.hpp b/src/AST-generation/syntax_error.hpp similarity index 96% rename from src/syntax_error.hpp rename to src/AST-generation/syntax_error.hpp index c464fba..ec03946 100755 --- a/src/syntax_error.hpp +++ b/src/AST-generation/syntax_error.hpp @@ -3,7 +3,7 @@ #include "expressions.hpp" #include "statements.hpp" -#include "token.hpp" +#include "tokens/token.hpp" #include diff --git a/src/token.cpp b/src/AST-generation/tokens/token.cpp similarity index 100% rename from src/token.cpp rename to src/AST-generation/tokens/token.cpp diff --git a/src/token.hpp b/src/AST-generation/tokens/token.hpp similarity index 100% rename from src/token.hpp rename to src/AST-generation/tokens/token.hpp diff --git a/src/token_type.hpp b/src/AST-generation/tokens/token_type.hpp similarity index 100% rename from src/token_type.hpp rename to src/AST-generation/tokens/token_type.hpp diff --git a/src/LBPLClass.cpp b/src/LBPLClass.cpp deleted file mode 100644 index 3870718..0000000 --- a/src/LBPLClass.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "LBPLClass.hpp" -#include "LBPLInstance.hpp" -#include "interpreter.hpp" - -Value LBPLClass::call(Interpreter *interpreter, std::vector &args) { - auto instance = std::make_shared(this); - - LBPLFunc *init = findMethod("init"); - if (init) { - init->bind(instance); - init->call(interpreter, args); - } - - return instance; -} - -LBPLFunc *LBPLClass::findMethod(const std::string &name) { - if (methods.contains(name)) { - return methods.find(name)->second; - } else if (superclass) { - return superclass->findMethod(name); - } - - return nullptr; -} - -int LBPLClass::arity() { - auto it = methods.find("init"); - return it == methods.end() ? 0 : it->second->arity(); -} diff --git a/src/ast_printer.cpp b/src/ast_printer.cpp deleted file mode 100755 index 18dff9f..0000000 --- a/src/ast_printer.cpp +++ /dev/null @@ -1,215 +0,0 @@ -#include "ast_printer.hpp" -#include "statements.hpp" - -#include - -void AST_Printer::printLocation(Stmt *stmt) { - std::cout << "Location: " << stmt->filename << " [Line: " << stmt->line - << ", Column: " << stmt->column << "]\n"; -} - -void AST_Printer::visitFnStmt(FnStmt *stmt) { - printLocation(stmt); - std::cout << "("; - std::cout << "fn `" + stmt->name->lexeme + "` args["; - - for (auto &&arg : stmt->args) { - std::cout << "(" << arg->lexeme << ")"; - } - - std::cout << "] ` body {\n"; - for (auto &&bodyStmt : stmt->body) { - bodyStmt->accept(this); - } - - std::cout << "})\n"; -} - -void AST_Printer::visitVarStmt(VarStmt *var) { - printLocation(var); - std::cout << "(var " << var->name->lexeme << " value `"; - - if (var->value) { - var->value->accept(this); - } else { - std::cout << "nil"; - } - std::cout << "`)\n"; -} - -void AST_Printer::visitClassStmt(ClassStmt *stmt) { - printLocation(stmt); - std::cout << "(class `" << stmt->name->lexeme << "` implements("; - if (stmt->superclass) { - stmt->superclass->accept(this); - } - std::cout << ") body {\n"; - for (auto &&fn : stmt->body) { - fn->accept(this); - } - std::cout << "})\n"; -} - -void AST_Printer::visitIfStmt(IfStmt *fn) { - printLocation(fn); - std::cout << "(condition `"; - fn->condition->accept(this); - std::cout << "` if true "; - fn->trueBranch->accept(this); - std::cout << " if false "; - if (fn->falseBranch) { - fn->falseBranch->accept(this); - } - std::cout << ")\n"; -} - -void AST_Printer::visitWhileStmt(WhileStmt *stmt) { - printLocation(stmt); - std::cout << "(while "; - stmt->condition->accept(this); - std::cout << " do "; - stmt->body->accept(this); - std::cout << ")\n"; -} - -void AST_Printer::visitForStmt(ForStmt *stmt) { - printLocation(stmt); - std::cout << "(for "; - if (stmt->initializer) { - stmt->initializer->accept(this); - } - std::cout << " while "; - if (stmt->condition) { - stmt->condition->accept(this); - } else { - std::cout << "yes"; - } - std::cout << " do "; - stmt->body->accept(this); - std::cout << " then "; - if (stmt->increment) { - stmt->increment->accept(this); - } - std::cout << ")\n"; -} - -void AST_Printer::visitScopedStmt(ScopedStmt *stmt) { - printLocation(stmt); - std::cout << "{"; - for (auto &&stmt : stmt->body) { - stmt->accept(this); - } - std::cout << "}"; -} - -void AST_Printer::visitExprStmt(ExprStmt *stmt) { - printLocation(stmt); - stmt->expr->accept(this); - std::cout << "\n"; -} - -void AST_Printer::visitReturnStmt(ReturnStmt *stmt) { - printLocation(stmt); - std::cout << "(return "; - if (stmt->value) { - stmt->value->accept(this); - } else { - std::cout << "void"; - } - std::cout << ")\n"; -} - -Value AST_Printer::visitBinaryExpr(BinaryExpr *expr) { - std::cout << "(" << expr->op->lexeme << " "; - expr->left->accept(this); - std::cout << " "; - expr->right->accept(this); - std::cout << ")"; - return nullptr; -} - -Value AST_Printer::visitBreakExpr(BreakExpr *expr) { - std::cout << "(breaking)"; - return nullptr; -} - -Value AST_Printer::visitContinueExpr(ContinueExpr *expr) { - std::cout << "(continuing)\n"; - return nullptr; -} - -Value AST_Printer::visitUnaryExpr(UnaryExpr *expr) { - std::cout << "(" << expr->op->lexeme << " "; - expr->right->accept(this); - std::cout << ")"; - return nullptr; -} - -Value AST_Printer::visitLiteralExpr(LiteralExpr *expr) { - std::cout << expr->token->lexeme; - return nullptr; -} - -Value AST_Printer::visitSuperExpr(SuperExpr *expr) { - std::cout << "(super)"; - return nullptr; -} - -Value AST_Printer::visitThisExpr(ThisExpr *expr) { - std::cout << "(this)"; - return nullptr; -} - -Value AST_Printer::visitAssignExpr(AssignExpr *expr) { - std::cout << "(assign "; - expr->value->accept(this); - std::cout << " to " << expr->variable->lexeme << ")"; - return nullptr; -} - -Value AST_Printer::visitGroupExpr(GroupingExpr *expr) { - expr->expr->accept(this); - return nullptr; -} - -Value AST_Printer::visitCallExpr(FnCallExpr *expr) { - std::cout << "(calling `"; - expr->callee->accept(this); - std::cout << "` with parameters ["; - - for (auto &&arg : expr->args) { - arg->accept(this); - } - - std::cout << "])"; - return nullptr; -} - -Value AST_Printer::visitGetFieldExpr(GetFieldExpr *expr) { - std::cout << "get field `" << expr->field->lexeme << "` from "; - expr->instance->accept(this); - return nullptr; -} - -Value AST_Printer::visitSetFieldExpr(SetFieldExpr *expr) { - std::cout << "setting field `" << expr->field->lexeme << "` from "; - expr->instance->accept(this); - std::cout << " to "; - expr->value->accept(this); - return nullptr; -} - -Value AST_Printer::visitTernaryExpr(TernaryExpr *expr) { - std::cout << "condition `"; - expr->condition->accept(this); - std::cout << "` if true "; - expr->trueBranch->accept(this); - std::cout << " if false "; - expr->falseBranch->accept(this); - return nullptr; -} - -Value AST_Printer::visitVarExpr(VariableExpr *expr) { - std::cout << expr->variable->lexeme; - return nullptr; -} diff --git a/src/ast_printer.hpp b/src/ast_printer.hpp deleted file mode 100755 index 0bf1bb7..0000000 --- a/src/ast_printer.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef AST_PRINTER_H -#define AST_PRINTER_H - -#include "tree_nodes.hpp" -#include "visitor.hpp" - -class AST_Printer : public Statement::Visitor, public Expression::Visitor { - void printLocation(Stmt *); - - void visitFnStmt(FnStmt *) override; - void visitVarStmt(VarStmt *) override; - void visitClassStmt(ClassStmt *) override; - void visitIfStmt(IfStmt *) override; - void visitWhileStmt(WhileStmt *) override; - void visitForStmt(ForStmt *) override; - void visitScopedStmt(ScopedStmt *) override; - void visitExprStmt(ExprStmt *) override; - void visitReturnStmt(ReturnStmt *) override; - - Value visitBinaryExpr(BinaryExpr *) override; - Value visitBreakExpr(BreakExpr *) override; - Value visitContinueExpr(ContinueExpr *) override; - Value visitUnaryExpr(UnaryExpr *) override; - Value visitLiteralExpr(LiteralExpr *) override; - Value visitGroupExpr(GroupingExpr *) override; - Value visitSuperExpr(SuperExpr *) override; - Value visitThisExpr(ThisExpr *) override; - Value visitCallExpr(FnCallExpr *) override; - Value visitGetFieldExpr(GetFieldExpr *) override; - Value visitSetFieldExpr(SetFieldExpr *) override; - Value visitTernaryExpr(TernaryExpr *) override; - Value visitVarExpr(VariableExpr *) override; - Value visitAssignExpr(AssignExpr *) override; -}; - -#endif diff --git a/src/common.hpp b/src/common.hpp deleted file mode 100755 index cea8e3e..0000000 --- a/src/common.hpp +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef COMMON_H -#define COMMON_H - -#include "token.hpp" -#include "token_type.hpp" - -#include -#include - -#endif diff --git a/src/builtin_methods.hpp b/src/interpretation/builtin_methods.hpp similarity index 97% rename from src/builtin_methods.hpp rename to src/interpretation/builtin_methods.hpp index be2a64c..0ded93d 100644 --- a/src/builtin_methods.hpp +++ b/src/interpretation/builtin_methods.hpp @@ -1,7 +1,7 @@ #ifndef BUILTIN_METHODS_H #define BUILTIN_METHODS_H -#include "LBPLCallable.hpp" +#include "types/LBPLCallable.hpp" #include #include diff --git a/src/environment.cpp b/src/interpretation/environment.cpp similarity index 98% rename from src/environment.cpp rename to src/interpretation/environment.cpp index 20f2656..3ce90ab 100755 --- a/src/environment.cpp +++ b/src/interpretation/environment.cpp @@ -1,5 +1,5 @@ #include "environment.hpp" -#include "LBPLTypes.hpp" +#include "types/LBPLTypes.hpp" #include "runtime_error.hpp" #include diff --git a/src/environment.hpp b/src/interpretation/environment.hpp similarity index 92% rename from src/environment.hpp rename to src/interpretation/environment.hpp index f8f9415..b649077 100755 --- a/src/environment.hpp +++ b/src/interpretation/environment.hpp @@ -1,8 +1,8 @@ #ifndef ENVIRONMENT_H #define ENVIRONMENT_H -#include "LBPLTypes.hpp" -#include "token.hpp" +#include "../AST-generation/tokens/token.hpp" +#include "types/LBPLTypes.hpp" #include #include diff --git a/src/interpreter.cpp b/src/interpretation/interpreter.cpp similarity index 99% rename from src/interpreter.cpp rename to src/interpretation/interpreter.cpp index de80ef8..53ecdd3 100644 --- a/src/interpreter.cpp +++ b/src/interpretation/interpreter.cpp @@ -1,8 +1,8 @@ #include "interpreter.hpp" -#include "LBPLClass.hpp" -#include "LBPLFunction.hpp" -#include "LBPLInstance.hpp" -#include "LBPLTypes.hpp" +#include "types/LBPLClass.hpp" +#include "types/LBPLFunction.hpp" +#include "types/LBPLInstance.hpp" +#include "types/LBPLTypes.hpp" #include "runtime_error.hpp" #include diff --git a/src/interpreter.hpp b/src/interpretation/interpreter.hpp similarity index 93% rename from src/interpreter.hpp rename to src/interpretation/interpreter.hpp index 1f3535b..9741139 100644 --- a/src/interpreter.hpp +++ b/src/interpretation/interpreter.hpp @@ -1,9 +1,9 @@ #ifndef INTERPRETER_H #define INTERPRETER_H -#include "LBPLTypes.hpp" #include "builtin_methods.hpp" #include "environment.hpp" +#include "types/LBPLTypes.hpp" #include "visitor.hpp" #include @@ -33,8 +33,8 @@ private: bool isTruthy(const Value &); bool isTruthy(Value &&); - Value performBinaryOperation(std::shared_ptr &, - const Value &, const Value &); + Value performBinaryOperation(std::shared_ptr &, const Value &, + const Value &); void visitFnStmt(FnStmt *) override; void visitVarStmt(VarStmt *) override; diff --git a/src/resolver.cpp b/src/interpretation/resolver.cpp similarity index 97% rename from src/resolver.cpp rename to src/interpretation/resolver.cpp index c515c9e..51fcbda 100644 --- a/src/resolver.cpp +++ b/src/interpretation/resolver.cpp @@ -1,5 +1,5 @@ #include "resolver.hpp" -#include "syntax_error.hpp" +#include "../AST-generation/syntax_error.hpp" #include void Resolver::resolve(std::vector> &stmts) { @@ -39,7 +39,7 @@ void Resolver::define(const Token *name) { void Resolver::resolveLocal(Expr *expr, const Token *name) { for (int i = scopes.size() - 1; i >= 0; i--) { if (scopes[i].contains(std::get(name->lexeme))) { - interpreter->resolve(expr, scopes.size() - 1 - i); + interpreter.resolve(expr, scopes.size() - 1 - i); return; } } @@ -48,7 +48,7 @@ void Resolver::resolveLocal(Expr *expr, const Token *name) { void Resolver::resolveLocal(Expr *expr, const std::string &name) { for (int i = scopes.size() - 1; i >= 0; i--) { if (scopes[i].contains(name)) { - interpreter->resolve(expr, scopes.size() - 1 - i); + interpreter.resolve(expr, scopes.size() - 1 - i); return; } } diff --git a/src/resolver.hpp b/src/interpretation/resolver.hpp similarity index 91% rename from src/resolver.hpp rename to src/interpretation/resolver.hpp index a8b9e19..225a808 100644 --- a/src/resolver.hpp +++ b/src/interpretation/resolver.hpp @@ -1,8 +1,8 @@ #ifndef RESOLVER_H #define RESOLVER_H +#include "../AST-generation/statements.hpp" #include "interpreter.hpp" -#include "statements.hpp" #include "visitor.hpp" #include @@ -33,7 +33,7 @@ enum VarState { class Resolver : Statement::Visitor, Expression::Visitor { private: - Interpreter *interpreter; + Interpreter &interpreter; FunctionType::Type currentFn; ClassType::Type currentClass; int loops; @@ -79,9 +79,9 @@ private: Value visitAssignExpr(AssignExpr *) override; public: - Resolver(Interpreter *interpreter) + Resolver(Interpreter &interpreter) : interpreter(interpreter), currentFn(FunctionType::None), - currentClass(ClassType::None), loops(0), scopes() {} + currentClass(ClassType::None), loops(0), scopes(), hadError(false) {} void resolve(std::vector> &); }; diff --git a/src/runtime_error.cpp b/src/interpretation/runtime_error.cpp similarity index 100% rename from src/runtime_error.cpp rename to src/interpretation/runtime_error.cpp diff --git a/src/runtime_error.hpp b/src/interpretation/runtime_error.hpp similarity index 88% rename from src/runtime_error.hpp rename to src/interpretation/runtime_error.hpp index 5c06189..4790d16 100644 --- a/src/runtime_error.hpp +++ b/src/interpretation/runtime_error.hpp @@ -1,9 +1,8 @@ #ifndef RUNTIME_ERROR_H #define RUNTIME_ERROR_H -#include "expressions.hpp" -#include "statements.hpp" -#include "token.hpp" +#include "../AST-generation/expressions.hpp" +#include "../AST-generation/statements.hpp" #include diff --git a/src/tree_nodes.hpp b/src/interpretation/tree_nodes.hpp similarity index 100% rename from src/tree_nodes.hpp rename to src/interpretation/tree_nodes.hpp diff --git a/src/LBPLCallable.hpp b/src/interpretation/types/LBPLCallable.hpp similarity index 100% rename from src/LBPLCallable.hpp rename to src/interpretation/types/LBPLCallable.hpp diff --git a/src/interpretation/types/LBPLClass.cpp b/src/interpretation/types/LBPLClass.cpp new file mode 100644 index 0000000..c83e108 --- /dev/null +++ b/src/interpretation/types/LBPLClass.cpp @@ -0,0 +1,30 @@ +#include "LBPLClass.hpp" +#include "../interpreter.hpp" +#include "LBPLInstance.hpp" + +Value LBPLClass::call(Interpreter *interpreter, std::vector &args) { + auto instance = std::make_shared(this); + + LBPLFunc *init = findMethod("init"); + if (init) { + init->bind(instance); + init->call(interpreter, args); + } + + return instance; +} + +LBPLFunc *LBPLClass::findMethod(const std::string &name) { + if (methods.contains(name)) { + return methods.find(name)->second; + } else if (superclass) { + return superclass->findMethod(name); + } + + return nullptr; +} + +int LBPLClass::arity() { + auto it = methods.find("init"); + return it == methods.end() ? 0 : it->second->arity(); +} diff --git a/src/LBPLClass.hpp b/src/interpretation/types/LBPLClass.hpp similarity index 100% rename from src/LBPLClass.hpp rename to src/interpretation/types/LBPLClass.hpp diff --git a/src/LBPLFunction.cpp b/src/interpretation/types/LBPLFunction.cpp similarity index 96% rename from src/LBPLFunction.cpp rename to src/interpretation/types/LBPLFunction.cpp index b516285..c092a4f 100644 --- a/src/LBPLFunction.cpp +++ b/src/interpretation/types/LBPLFunction.cpp @@ -1,5 +1,5 @@ #include "LBPLFunction.hpp" -#include "interpreter.hpp" +#include "../interpreter.hpp" void LBPLFunc::bind(std::shared_ptr &&instance) { closureEnv = std::make_shared(); diff --git a/src/LBPLFunction.hpp b/src/interpretation/types/LBPLFunction.hpp similarity index 91% rename from src/LBPLFunction.hpp rename to src/interpretation/types/LBPLFunction.hpp index f4df5de..17df3e0 100644 --- a/src/LBPLFunction.hpp +++ b/src/interpretation/types/LBPLFunction.hpp @@ -1,9 +1,9 @@ #ifndef LBPL_FUNCTION_H #define LBPL_FUNCTION_H +#include "../../AST-generation/statements.hpp" +#include "../environment.hpp" #include "LBPLCallable.hpp" -#include "environment.hpp" -#include "statements.hpp" #include diff --git a/src/LBPLInstance.cpp b/src/interpretation/types/LBPLInstance.cpp similarity index 95% rename from src/LBPLInstance.cpp rename to src/interpretation/types/LBPLInstance.cpp index c6771f7..b8300e3 100644 --- a/src/LBPLInstance.cpp +++ b/src/interpretation/types/LBPLInstance.cpp @@ -1,5 +1,5 @@ #include "LBPLInstance.hpp" -#include "runtime_error.hpp" +#include "../runtime_error.hpp" #include Value LBPLInstance::get(const Token *name) { diff --git a/src/LBPLInstance.hpp b/src/interpretation/types/LBPLInstance.hpp similarity index 100% rename from src/LBPLInstance.hpp rename to src/interpretation/types/LBPLInstance.hpp diff --git a/src/LBPLTypes.hpp b/src/interpretation/types/LBPLTypes.hpp similarity index 100% rename from src/LBPLTypes.hpp rename to src/interpretation/types/LBPLTypes.hpp diff --git a/src/visitor.hpp b/src/interpretation/visitor.hpp similarity index 97% rename from src/visitor.hpp rename to src/interpretation/visitor.hpp index b2afa73..79765f7 100755 --- a/src/visitor.hpp +++ b/src/interpretation/visitor.hpp @@ -1,7 +1,7 @@ #ifndef VISITOR_H #define VISITOR_H -#include "LBPLTypes.hpp" +#include "types/LBPLTypes.hpp" #include "tree_nodes.hpp" namespace Statement { diff --git a/src/main.cpp b/src/main.cpp index 4beb625..4846d70 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,43 +1,36 @@ #include #include -#include "parser.hpp" -#include "interpreter.hpp" -#include "resolver.hpp" +#include "AST-generation/parser.hpp" +#include "interpretation/interpreter.hpp" +#include "interpretation/resolver.hpp" int main(const int argc, const char **argv) { if (argc < 2) { std::cerr << "\033[1;31mNot enough arguemnts.\tUsage: lbpl [script]" << std::endl; - return 1; + return -1; } std::ifstream file(argv[1]); if (!file.good()) { std::cerr << "I/O error: couldn't load file `" << argv[1] << "`."; - return 1; + return -1; } - Parser *parser = new Parser(file, argv[1]); - std::vector> statements = parser->parse(); + Parser parser(file, argv[1]); + std::vector> statements = parser.parse(); - // AST_Printer test; - // for (auto &&stmt : statements) { - // stmt->accept(&test); - // } + if (!parser.hadError) { + Interpreter interpreter; + Resolver resolver(interpreter); + resolver.resolve(statements); - if (!parser->hadError) { - Interpreter *interpreter = new Interpreter(); - Resolver *resolver = new Resolver(interpreter); - resolver->resolve(statements); - - if (!resolver->hadError) { - interpreter->interpret(statements); + if (!resolver.hadError) { + interpreter.interpret(statements); + return 0; } - - delete resolver; - delete interpreter; } - delete parser; + return -1; } diff --git a/src/main.hpp b/src/main.hpp deleted file mode 100755 index 78d6836..0000000 --- a/src/main.hpp +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef MAIN_H -#define MAIN_H - -#include "common.h" -#include "interpreter.h" -#include "parser.h" -#include "resolver.h" -#include "statements.h" - -#include "ast_printer.h" - -#include -#include -#include - -std::string readFile(const char *); - -#endif -- 2.52.0