From 353300db3c2904b0230349cae07693f4b7917766 Mon Sep 17 00:00:00 2001 From: Rohith-Raju Date: Wed, 15 May 2024 14:56:27 +0530 Subject: [PATCH] update(function): fix recursion and repl state --- include/Interpreter.h | 12 +++++++--- include/Parser.h | 3 ++- include/Return.h | 6 +++++ include/Statement.h | 10 ++++++++- include/env/Env.h | 2 +- main.cpp | 47 ++++++++++++++++++++++++++++++++++++---- src/Function.cpp | 4 ++-- src/Interpreter.cpp | 29 ++++++++++++++++++------- src/Parser.cpp | 12 ++++++++++ src/Statement.cpp | 3 +++ src/env/Env.cpp | 20 ++++++++--------- test/TestInterpreter.cpp | 36 +++++++++++++++++++++++++----- test/lib | 2 +- 13 files changed, 148 insertions(+), 38 deletions(-) create mode 100644 include/Return.h diff --git a/include/Interpreter.h b/include/Interpreter.h index ffc7817..346468f 100644 --- a/include/Interpreter.h +++ b/include/Interpreter.h @@ -14,7 +14,7 @@ class Interpreter { private: - static Environment *environment; + Environment *environment; void excecute(Statement *stmnt); @@ -31,13 +31,17 @@ class Interpreter { bool checkCompatibility(Token *op, Object left, Object right); public: - static Environment *globals; + Environment *globals; Interpreter(); bool isBreakUsed = false; - void excecuteBlock(std::vector stmnts, Environment *env); + bool isReturnUsed = false; + + Object returnObj; + + Object excecuteBlock(std::vector stmnts, Environment *env); void interpret(std::vector &statements); @@ -55,6 +59,8 @@ class Interpreter { void visitFuncStmnt(Function *stmnt); + void visitReturnStmnt(Return *stmnt); + Object visitAssignment(Assignment *expr); Object visitLogicalExp(Logical *expr); diff --git a/include/Parser.h b/include/Parser.h index e1f594a..11e7b4c 100644 --- a/include/Parser.h +++ b/include/Parser.h @@ -45,13 +45,14 @@ class Parser { Statement *breakStatement(); Statement *expressionStatement(); Statement *function(std::string str); + Statement *returnStatement(); std::vector blockStatement(); // Variable stuff Statement *declaration(); Statement *varDeclaration(); - // helper functions + // Helper functions Expr *equality(); bool check(TokenType type); Token advance(); diff --git a/include/Return.h b/include/Return.h new file mode 100644 index 0000000..c027a45 --- /dev/null +++ b/include/Return.h @@ -0,0 +1,6 @@ +#include "utls/Object.h" + +struct ReturnValue { + Object value; + ReturnValue(Object value) : value(value){}; +}; diff --git a/include/Statement.h b/include/Statement.h index 3ec9332..aae81ef 100644 --- a/include/Statement.h +++ b/include/Statement.h @@ -17,7 +17,8 @@ enum Statement_type { StmntIf_type, StmntWhile_type, StmntBreak_type, - StmntFunc_type + StmntFunc_type, + StmntReturn_type }; class Statement { @@ -88,4 +89,11 @@ class Function : public Statement { std::vector body); }; +class Return : public Statement { +public: + Token *keyword; + Expr *value; + Return(Token *keyword, Expr *value); +}; + #endif diff --git a/include/env/Env.h b/include/env/Env.h index d8a6b83..94bb0b0 100644 --- a/include/env/Env.h +++ b/include/env/Env.h @@ -16,7 +16,7 @@ class Environment { public: Environment(); - ~Environment(); + //~Environment(); Environment(Environment *enclosing); void define(Token *tkn, Object value); void define(std::string name, Object value); diff --git a/main.cpp b/main.cpp index affeef8..7cbb047 100644 --- a/main.cpp +++ b/main.cpp @@ -2,12 +2,42 @@ #include "Interpreter.h" #include "Scanner.h" #include +#include +#include +#include #include #include +#include +#include +#include #include +namespace fs = std::filesystem; + void runCode(std::string source); +std::string readFile(fs::path path) { + if (path.extension() != ".crux") { + std::cerr << "Filename " << path.filename() + << " should end with the extension \".crux\" \n"; + exit(64); + } + + std::ostringstream data; + std::fstream file(path, std::ios_base::in); + if (!file.is_open()) { + std::cerr << "File could't be found or opened \n"; + exit(64); + } + data << file.rdbuf(); + return data.str(); +} + +void runFile(std::string path) { + fs::path fPath(path); + runCode(readFile(fPath)); +} + void runPromt() { for (;;) { std::cout << "> "; @@ -25,16 +55,25 @@ void runPromt() { } } +Interpreter interpreter{}; + void runCode(std::string source) { Scanner scanner(source); std::vector tokens = scanner.scanTokens(); Parser parser(tokens); std::vector expression = parser.parse(); - std::unique_ptr interpreter = std::make_unique(); - interpreter->interpret(expression); + interpreter.interpret(expression); } -int main() { - runPromt(); +int main(int argc, char *argv[]) { + if (argc > 2) { + std::cout << "usage: crux