]> git.leonardobizzoni.com Git - LBPL/commitdiff
Changed `src` file structure
authorLeonardoBizzoni <leo2002714@gmail.com>
Fri, 6 Sep 2024 13:12:38 +0000 (15:12 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Fri, 6 Sep 2024 13:12:38 +0000 (15:12 +0200)
36 files changed:
src/AST-generation/expressions.hpp [moved from src/expressions.hpp with 81% similarity]
src/AST-generation/lexer.cpp [moved from src/lexer.cpp with 99% similarity]
src/AST-generation/lexer.hpp [moved from src/lexer.hpp with 97% similarity]
src/AST-generation/parser.cpp [moved from src/parser.cpp with 99% similarity]
src/AST-generation/parser.hpp [moved from src/parser.hpp with 96% similarity]
src/AST-generation/statements.hpp [moved from src/statements.hpp with 81% similarity]
src/AST-generation/syntax_error.cpp [moved from src/syntax_error.cpp with 100% similarity]
src/AST-generation/syntax_error.hpp [moved from src/syntax_error.hpp with 96% similarity]
src/AST-generation/tokens/token.cpp [moved from src/token.cpp with 100% similarity]
src/AST-generation/tokens/token.hpp [moved from src/token.hpp with 100% similarity]
src/AST-generation/tokens/token_type.hpp [moved from src/token_type.hpp with 100% similarity]
src/LBPLClass.cpp [deleted file]
src/ast_printer.cpp [deleted file]
src/ast_printer.hpp [deleted file]
src/common.hpp [deleted file]
src/interpretation/builtin_methods.hpp [moved from src/builtin_methods.hpp with 97% similarity]
src/interpretation/environment.cpp [moved from src/environment.cpp with 98% similarity]
src/interpretation/environment.hpp [moved from src/environment.hpp with 92% similarity]
src/interpretation/interpreter.cpp [moved from src/interpreter.cpp with 99% similarity]
src/interpretation/interpreter.hpp [moved from src/interpreter.hpp with 93% similarity]
src/interpretation/resolver.cpp [moved from src/resolver.cpp with 97% similarity]
src/interpretation/resolver.hpp [moved from src/resolver.hpp with 91% similarity]
src/interpretation/runtime_error.cpp [moved from src/runtime_error.cpp with 100% similarity]
src/interpretation/runtime_error.hpp [moved from src/runtime_error.hpp with 88% similarity]
src/interpretation/tree_nodes.hpp [moved from src/tree_nodes.hpp with 100% similarity]
src/interpretation/types/LBPLCallable.hpp [moved from src/LBPLCallable.hpp with 100% similarity]
src/interpretation/types/LBPLClass.cpp [new file with mode: 0644]
src/interpretation/types/LBPLClass.hpp [moved from src/LBPLClass.hpp with 100% similarity]
src/interpretation/types/LBPLFunction.cpp [moved from src/LBPLFunction.cpp with 96% similarity]
src/interpretation/types/LBPLFunction.hpp [moved from src/LBPLFunction.hpp with 91% similarity]
src/interpretation/types/LBPLInstance.cpp [moved from src/LBPLInstance.cpp with 95% similarity]
src/interpretation/types/LBPLInstance.hpp [moved from src/LBPLInstance.hpp with 100% similarity]
src/interpretation/types/LBPLTypes.hpp [moved from src/LBPLTypes.hpp with 100% similarity]
src/interpretation/visitor.hpp [moved from src/visitor.hpp with 97% similarity]
src/main.cpp
src/main.hpp [deleted file]

similarity index 81%
rename from src/expressions.hpp
rename to src/AST-generation/expressions.hpp
index a774eb1cb6d2e830a77293e6800680c07983b19b..0a21b002a70734375a28fe8b1a436ed8c2412ea0 100755 (executable)
@@ -1,20 +1,18 @@
 #ifndef EXPRESSIONS_H
 #define EXPRESSIONS_H
 
-#include "token.hpp"
+#include "tokens/token.hpp"
 
-#include "token.hpp"
-#include "visitor.hpp"
-#include <string>
+#include "../interpretation/visitor.hpp"
 
 #include <memory>
 #include <vector>
 
 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<Expr> right;
   std::shared_ptr<const Token> op;
 
-  BinaryExpr(int line, int column, const std::string &file,
+  BinaryExpr(int line, int column, const char *file,
              std::unique_ptr<Expr> &left, std::unique_ptr<Expr> &right,
              std::shared_ptr<const Token> &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<Expr> &left, std::unique_ptr<Expr> &&right,
              std::shared_ptr<const Token> &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<Expr> right;
   std::shared_ptr<const Token> op;
 
-  UnaryExpr(int line, int column, const std::string &file,
-            std::unique_ptr<Expr> right, std::shared_ptr<const Token> &op)
+  UnaryExpr(int line, int column, const char *file, std::unique_ptr<Expr> right,
+            std::shared_ptr<const Token> &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<const Token> token;
 
-  LiteralExpr(int line, int column, const std::string &file,
+  LiteralExpr(int line, int column, const char *file,
               std::shared_ptr<const Token> &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<const Token> &&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<const Token> field;
 
-  SuperExpr(int line, int column, const std::string &file,
+  SuperExpr(int line, int column, const char *file,
             std::shared_ptr<const Token> &field)
       : field(field), Expr(line, column, file) {}
   Value accept(Expression::Visitor *visitor) {
@@ -100,7 +98,7 @@ struct ThisExpr : public Expr {
 public:
   std::shared_ptr<const Token> keyword;
 
-  ThisExpr(int line, int column, const std::string &file,
+  ThisExpr(int line, int column, const char *file,
            std::shared_ptr<const Token> &keyword)
       : keyword(keyword), Expr(line, column, file) {}
 
@@ -112,7 +110,7 @@ public:
 struct GroupingExpr : public Expr {
   std::unique_ptr<Expr> expr;
 
-  GroupingExpr(int line, int column, const std::string &file,
+  GroupingExpr(int line, int column, const char *file,
                std::unique_ptr<Expr> &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<const Token> variable;
 
-  VariableExpr(int line, int column, const std::string &file,
+  VariableExpr(int line, int column, const char *file,
                std::shared_ptr<const Token> &variable)
       : variable(variable), Expr(line, column, file) {}
   Value accept(Expression::Visitor *visitor) {
@@ -135,7 +133,7 @@ struct AssignExpr : public Expr {
   std::shared_ptr<const Token> variable;
   std::unique_ptr<Expr> value;
 
-  AssignExpr(int line, int column, const std::string &file,
+  AssignExpr(int line, int column, const char *file,
              std::shared_ptr<const Token> &variable,
              std::unique_ptr<Expr> &value)
       : variable(variable), value(std::move(value)), Expr(line, column, file) {}
@@ -149,7 +147,7 @@ struct FnCallExpr : public Expr {
   std::unique_ptr<Expr> callee;
   std::vector<std::unique_ptr<Expr>> args;
 
-  FnCallExpr(int line, int column, const std::string &file,
+  FnCallExpr(int line, int column, const char *file,
              std::unique_ptr<Expr> &callee,
              std::vector<std::unique_ptr<Expr>> &args)
       : callee(std::move(callee)), args(std::move(args)),
@@ -165,7 +163,7 @@ struct TernaryExpr : public Expr {
   std::unique_ptr<Expr> trueBranch;
   std::unique_ptr<Expr> falseBranch;
 
-  TernaryExpr(int line, int column, const std::string &file,
+  TernaryExpr(int line, int column, const char *file,
               std::unique_ptr<Expr> &condition,
               std::unique_ptr<Expr> &trueBranch,
               std::unique_ptr<Expr> &&falseBranch)
@@ -181,7 +179,7 @@ struct GetFieldExpr : public Expr {
   std::shared_ptr<const Token> field;
   std::unique_ptr<Expr> instance;
 
-  GetFieldExpr(int line, int column, const std::string &file,
+  GetFieldExpr(int line, int column, const char *file,
                std::unique_ptr<Expr> &instance,
                std::shared_ptr<const Token> &field)
       : instance(std::move(instance)), field(field), Expr(line, column, file) {}
@@ -196,7 +194,7 @@ struct SetFieldExpr : public Expr {
   std::unique_ptr<Expr> value;
   std::unique_ptr<Expr> instance;
 
-  SetFieldExpr(int line, int column, const std::string &file,
+  SetFieldExpr(int line, int column, const char *file,
                std::unique_ptr<Expr> &instance,
                std::shared_ptr<const Token> &field,
                std::unique_ptr<Expr> &value)
similarity index 99%
rename from src/lexer.cpp
rename to src/AST-generation/lexer.cpp
index 8c0914da9d4747c5f0faff53ed0964171019537d..8696a38059e8d412b1c2bc0f0d18bf4ccf55dc80 100755 (executable)
@@ -1,6 +1,4 @@
 #include "lexer.hpp"
-#include "token.hpp"
-#include "token_type.hpp"
 
 #include <cstdlib>
 #include <cstring>
similarity index 97%
rename from src/lexer.hpp
rename to src/AST-generation/lexer.hpp
index e1a7b56bdd8c5c1b36b347a79ce643b326de0036..5cac46570b38612a61855f9b220cf21df2686c37 100755 (executable)
@@ -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 <cstdint>
 #include <fstream>
similarity index 99%
rename from src/parser.cpp
rename to src/AST-generation/parser.cpp
index 3b9fd47afe7a5fd25e57dfddd2d013b928d22eca..239f921a5c51007ac11deecb6ccb7d52a3055427 100755 (executable)
@@ -1,6 +1,5 @@
 #include "parser.hpp"
 #include "syntax_error.hpp"
-#include "token_type.hpp"
 
 #include <fcntl.h>
 #include <fstream>
similarity index 96%
rename from src/parser.hpp
rename to src/AST-generation/parser.hpp
index a579adc04122517f55e056395281168dc79122e0..88106bbdb5d9d9459b793532db2c8e86ff944254 100755 (executable)
@@ -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 <fstream>
 #include <memory>
@@ -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<std::string> &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<std::unique_ptr<Stmt>> parse();
 
similarity index 81%
rename from src/statements.hpp
rename to src/AST-generation/statements.hpp
index 92331136a2ce862af98663e1cce7d9feca3cb974..7f7f278e991a7e61438ed0abb95a71e0cf37f282 100755 (executable)
@@ -5,10 +5,10 @@
 #include <memory>
 
 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<std::shared_ptr<const Token>> args;
   std::vector<std::unique_ptr<Stmt>> body;
 
-  FnStmt(int line, int column, const std::string &file,
+  FnStmt(int line, int column, const char *file,
          std::shared_ptr<const Token> &name,
          std::vector<std::shared_ptr<const Token>> &args,
          std::vector<std::unique_ptr<Stmt>> &&body)
@@ -34,7 +34,7 @@ struct VarStmt : public Stmt {
   std::shared_ptr<const Token> name;
   std::unique_ptr<Expr> value;
 
-  VarStmt(int line, int column, const std::string &file,
+  VarStmt(int line, int column, const char *file,
           std::shared_ptr<const Token> &name, std::unique_ptr<Expr> &value)
       : name(name), value(std::move(value)), Stmt(line, column, file) {}
 
@@ -46,7 +46,7 @@ struct ClassStmt : public Stmt {
   std::unique_ptr<VariableExpr> superclass;
   std::vector<std::unique_ptr<Stmt>> body;
 
-  ClassStmt(int line, int column, const std::string &file,
+  ClassStmt(int line, int column, const char *file,
             std::shared_ptr<const Token> &name,
             std::unique_ptr<VariableExpr> &superclass,
             std::vector<std::unique_ptr<Stmt>> &&body)
@@ -61,7 +61,7 @@ struct IfStmt : public Stmt {
   std::unique_ptr<Stmt> trueBranch;
   std::unique_ptr<Stmt> falseBranch;
 
-  IfStmt(int line, int column, const std::string &file,
+  IfStmt(int line, int column, const char *file,
          std::unique_ptr<Expr> &condition, std::unique_ptr<Stmt> &trueBranch,
          std::unique_ptr<Stmt> &falseBranch)
       : condition(std::move(condition)), trueBranch(std::move(trueBranch)),
@@ -74,13 +74,13 @@ struct WhileStmt : public Stmt {
   std::unique_ptr<Expr> condition;
   std::unique_ptr<Stmt> body;
 
-  WhileStmt(int line, int column, const std::string &file,
-            std::unique_ptr<Expr> &cond, std::unique_ptr<Stmt> &body)
+  WhileStmt(int line, int column, const char *file, std::unique_ptr<Expr> &cond,
+            std::unique_ptr<Stmt> &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<Expr> &cond, std::unique_ptr<Stmt> &&body)
+  WhileStmt(int line, int column, const char *file, std::unique_ptr<Expr> &cond,
+            std::unique_ptr<Stmt> &&body)
       : condition(std::move(cond)), body(std::move(body)),
         Stmt(line, column, file) {}
 
@@ -91,7 +91,7 @@ struct ForStmt : public Stmt {
   std::unique_ptr<Expr> increment, condition;
   std::unique_ptr<Stmt> initializer, body;
 
-  ForStmt(int line, int column, const std::string &file,
+  ForStmt(int line, int column, const char *file,
           std::unique_ptr<Stmt> &initializer, std::unique_ptr<Expr> &cond,
           std::unique_ptr<Expr> &increment, std::unique_ptr<Stmt> &body)
       : initializer(std::move(initializer)), condition(std::move(cond)),
@@ -104,7 +104,7 @@ struct ForStmt : public Stmt {
 struct ScopedStmt : public Stmt {
   std::vector<std::unique_ptr<Stmt>> body;
 
-  ScopedStmt(int line, int column, const std::string &file,
+  ScopedStmt(int line, int column, const char *file,
              std::vector<std::unique_ptr<Stmt>> &&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> expr;
 
-  ExprStmt(int line, int column, const std::string &file,
-           std::unique_ptr<Expr> &&expr)
+  ExprStmt(int line, int column, const char *file, std::unique_ptr<Expr> &&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<Expr> value;
 
-  ReturnStmt(int line, int column, const std::string &file,
+  ReturnStmt(int line, int column, const char *file,
              std::unique_ptr<Expr> &value)
       : value(std::move(value)), Stmt(line, column, file) {}
 
similarity index 96%
rename from src/syntax_error.hpp
rename to src/AST-generation/syntax_error.hpp
index c464fba2ed4d97cdcc4e9ce1c76e0a42a9be0f6f..ec0394679457727f2916c592a866d9f5ca3d96d7 100755 (executable)
@@ -3,7 +3,7 @@
 
 #include "expressions.hpp"
 #include "statements.hpp"
-#include "token.hpp"
+#include "tokens/token.hpp"
 
 #include <string>
 
diff --git a/src/LBPLClass.cpp b/src/LBPLClass.cpp
deleted file mode 100644 (file)
index 3870718..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-#include "LBPLClass.hpp"
-#include "LBPLInstance.hpp"
-#include "interpreter.hpp"
-
-Value LBPLClass::call(Interpreter *interpreter, std::vector<Value> &args) {
-    auto instance = std::make_shared<LBPLInstance>(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 (executable)
index 18dff9f..0000000
+++ /dev/null
@@ -1,215 +0,0 @@
-#include "ast_printer.hpp"
-#include "statements.hpp"
-
-#include <iostream>
-
-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 (executable)
index 0bf1bb7..0000000
+++ /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 (executable)
index cea8e3e..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-#ifndef COMMON_H
-#define COMMON_H
-
-#include "token.hpp"
-#include "token_type.hpp"
-
-#include <string>
-#include <iostream>
-
-#endif
similarity index 97%
rename from src/builtin_methods.hpp
rename to src/interpretation/builtin_methods.hpp
index be2a64c71bd5da2be7387879b7ac4c29aca0ead6..0ded93d3686223870c38b9aa9edd3916d57109d6 100644 (file)
@@ -1,7 +1,7 @@
 #ifndef BUILTIN_METHODS_H
 #define BUILTIN_METHODS_H
 
-#include "LBPLCallable.hpp"
+#include "types/LBPLCallable.hpp"
 
 #include <chrono>
 #include <iostream>
similarity index 98%
rename from src/environment.cpp
rename to src/interpretation/environment.cpp
index 20f2656af099cbce2514f4d1537cffdf9ffe9a5d..3ce90ab8011ab5636f9dda6e4c97decbdb773cc2 100755 (executable)
@@ -1,5 +1,5 @@
 #include "environment.hpp"
-#include "LBPLTypes.hpp"
+#include "types/LBPLTypes.hpp"
 #include "runtime_error.hpp"
 
 #include <iostream>
similarity index 92%
rename from src/environment.hpp
rename to src/interpretation/environment.hpp
index f8f9415c30c4c09dd10397e68f1aa0341c6ebaaf..b6490770a2c29f82ba6ff8ad4d9a4fa44e33d3e0 100755 (executable)
@@ -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 <map>
 #include <memory>
similarity index 99%
rename from src/interpreter.cpp
rename to src/interpretation/interpreter.cpp
index de80ef8a15b08583d5c0ea936dbac2827d57261d..53ecdd373b0fba5b70ffb32cc39e799772d6d96e 100644 (file)
@@ -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 <cstddef>
similarity index 93%
rename from src/interpreter.hpp
rename to src/interpretation/interpreter.hpp
index 1f3535b5c7a38a193d54685787d4c1ad1bdb489b..97411392f285e645a427bbfa5d9a821a9109c30f 100644 (file)
@@ -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 <map>
@@ -33,8 +33,8 @@ private:
   bool isTruthy(const Value &);
   bool isTruthy(Value &&);
 
-  Value performBinaryOperation(std::shared_ptr<const Token> &,
-                                  const Value &, const Value &);
+  Value performBinaryOperation(std::shared_ptr<const Token> &, const Value &,
+                               const Value &);
 
   void visitFnStmt(FnStmt *) override;
   void visitVarStmt(VarStmt *) override;
similarity index 97%
rename from src/resolver.cpp
rename to src/interpretation/resolver.cpp
index c515c9e0defe51a1253aea0616e6ef86ff987c86..51fcbdaedb490a8246d85dbdda5de24307c3ae92 100644 (file)
@@ -1,5 +1,5 @@
 #include "resolver.hpp"
-#include "syntax_error.hpp"
+#include "../AST-generation/syntax_error.hpp"
 #include <string_view>
 
 void Resolver::resolve(std::vector<std::unique_ptr<Stmt>> &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<const char *>(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;
     }
   }
similarity index 91%
rename from src/resolver.hpp
rename to src/interpretation/resolver.hpp
index a8b9e19cf77f006ba3d197095bd8bb3c7017bdc7..225a808a874ea589a819e03eb7ef888f5cda01c1 100644 (file)
@@ -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 <map>
@@ -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<std::unique_ptr<Stmt>> &);
 };
similarity index 88%
rename from src/runtime_error.hpp
rename to src/interpretation/runtime_error.hpp
index 5c06189e3be40c9188e8797329135a439c3943fd..4790d1676791b3465f932ef2763003013776f633 100644 (file)
@@ -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 <string>
 
diff --git a/src/interpretation/types/LBPLClass.cpp b/src/interpretation/types/LBPLClass.cpp
new file mode 100644 (file)
index 0000000..c83e108
--- /dev/null
@@ -0,0 +1,30 @@
+#include "LBPLClass.hpp"
+#include "../interpreter.hpp"
+#include "LBPLInstance.hpp"
+
+Value LBPLClass::call(Interpreter *interpreter, std::vector<Value> &args) {
+  auto instance = std::make_shared<LBPLInstance>(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();
+}
similarity index 96%
rename from src/LBPLFunction.cpp
rename to src/interpretation/types/LBPLFunction.cpp
index b5162853b1bd90c90775b75d2fd3ae4bf26730bf..c092a4f02d180c98893e5e20cc6178ec04a3d05c 100644 (file)
@@ -1,5 +1,5 @@
 #include "LBPLFunction.hpp"
-#include "interpreter.hpp"
+#include "../interpreter.hpp"
 
 void LBPLFunc::bind(std::shared_ptr<LBPLInstance> &&instance) {
   closureEnv = std::make_shared<Environment>();
similarity index 91%
rename from src/LBPLFunction.hpp
rename to src/interpretation/types/LBPLFunction.hpp
index f4df5de3693f0650963323a194d0eb4dcc4336cd..17df3e093679d95eb4c83c7d0093eb9d1d20daaf 100644 (file)
@@ -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 <memory>
 
similarity index 95%
rename from src/LBPLInstance.cpp
rename to src/interpretation/types/LBPLInstance.cpp
index c6771f776662c28206a19a2f1835020b39f7ae71..b8300e31130e71fdd87f486ef695b9c5f58011e9 100644 (file)
@@ -1,5 +1,5 @@
 #include "LBPLInstance.hpp"
-#include "runtime_error.hpp"
+#include "../runtime_error.hpp"
 #include <string>
 
 Value LBPLInstance::get(const Token *name) {
similarity index 97%
rename from src/visitor.hpp
rename to src/interpretation/visitor.hpp
index b2afa7340a7b2963a89d613bb8675694b1d626c9..79765f71fdf91bf034b04b5e205e6ad12def8ef3 100755 (executable)
@@ -1,7 +1,7 @@
 #ifndef VISITOR_H
 #define VISITOR_H
 
-#include "LBPLTypes.hpp"
+#include "types/LBPLTypes.hpp"
 #include "tree_nodes.hpp"
 
 namespace Statement {
index 4beb625a301c336b2750164795a4052f22dc57d8..4846d70b541872a1fc5d6cebdcb109b5a30f910a 100755 (executable)
@@ -1,43 +1,36 @@
 #include <fstream>
 #include <iostream>
 
-#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<std::unique_ptr<Stmt>> statements = parser->parse();
+  Parser parser(file, argv[1]);
+  std::vector<std::unique_ptr<Stmt>> 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 (executable)
index 78d6836..0000000
+++ /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 <fstream>
-#include <iomanip>
-#include <iostream>
-
-std::string readFile(const char *);
-
-#endif