From cb3065c34756a70cb6006fc25777ce3e720ff1a8 Mon Sep 17 00:00:00 2001
From: Ferenc Szontágh <szf@fsociety.hu>
Date: Sun, 13 Apr 2025 18:16:19 +0000
Subject: [PATCH] implement variable contexts, add function body store and parsing

---
 src/ScriptInterpreterHelpers.hpp |   59 +++++++++++++++++++++++++++++++++--------------------------
 1 files changed, 33 insertions(+), 26 deletions(-)

diff --git a/src/ScriptInterpreterHelpers.hpp b/src/ScriptInterpreterHelpers.hpp
index 6b615c7..debd1bc 100644
--- a/src/ScriptInterpreterHelpers.hpp
+++ b/src/ScriptInterpreterHelpers.hpp
@@ -14,7 +14,7 @@
 
 namespace ScriptInterpreterHelpers {
 
-static std::string extractSubstring(const std::string & str, size_t start, size_t end) {
+static std::string extractSubstring(const std::string & str, const size_t & start, const size_t & end) {
     if (start >= 0 && start < str.length() && end >= start && end < str.length()) {
         return str.substr(start, end - start + 1);
     }
@@ -37,28 +37,31 @@
 
     // check the arguments types
     if (tokens[i].type != TokenType::StringDeclaration && tokens[i].type != TokenType::BooleanDeclaration &&
-        tokens[i].type != TokenType::IntDeclaration && tokens[i].type != TokenType::DoubleDeclaration) {
+        tokens[i].type != TokenType::IntDeclaration && tokens[i].type != TokenType::DoubleDeclaration &&
+        tokens[i].type != TokenType::RightParenthesis) {
         THROW_UNEXPECTED_TOKEN_ERROR_HELPER(tokens[i], "variable declaration", file, line);
     }
-    const auto parameter_type = getVariableTypeFromTokenTypeDeclaration(tokens[i].type);
-    if (parameter_type == Variables::Type::VT_NOT_DEFINED) {
-        THROW_UNEXPECTED_TOKEN_ERROR_HELPER(tokens[i], "valid type identifier", file, line);
+    if (tokens[i].type != TokenType::RightParenthesis) {
+        const auto parameter_type = getVariableTypeFromTokenTypeDeclaration(tokens[i].type);
+        if (parameter_type == Variables::Type::VT_NOT_DEFINED) {
+            THROW_UNEXPECTED_TOKEN_ERROR_HELPER(tokens[i], "valid type identifier", file, line);
+        }
+
+        if (parameter_type == Variables::Type::VT_FUNCTION) {
+            THROW_UNEXPECTED_TOKEN_ERROR_HELPER(tokens[i], "valid type identifier", file, line);
+        }
+
+        if (parameter_type == Variables::Type::VT_NULL) {
+            THROW_UNEXPECTED_TOKEN_ERROR_HELPER(tokens[i], "valid type identifier", file, line);
+        }
+
+        Value val;
+        val.type  = parameter_type;
+        val.token = tokens[i];
+
+        arguments.emplace_back(std::move(val));
+        i++;  // Skip variable declaration
     }
-
-    if (parameter_type == Variables::Type::VT_FUNCTION) {
-        THROW_UNEXPECTED_TOKEN_ERROR_HELPER(tokens[i], "valid type identifier", file, line);
-    }
-
-    if (parameter_type == Variables::Type::VT_NULL) {
-        THROW_UNEXPECTED_TOKEN_ERROR_HELPER(tokens[i], "valid type identifier", file, line);
-    }
-
-    Value val;
-    val.type  = parameter_type;
-    val.token = tokens[i];
-
-    arguments.emplace_back(std::move(val));
-    i++;  // Skip variable declaration
 
     if (tokens[i].type != TokenType::RightParenthesis) {
         THROW_UNEXPECTED_TOKEN_ERROR_HELPER(tokens[i], ") - Only one argument is allowed", file, line);
@@ -68,17 +71,19 @@
     return arguments;
 }
 
-[[nodiscard]] static std::string getFunctionBody(const std::vector<Token> & tokens, std::size_t & i) {
-    const size_t first_index = i;
+static void getFunctionBody(const std::vector<Token> & tokens, std::size_t & i, std::size_t & start,
+                            std::size_t & end) {
+    start = tokens[i].pos.end;
+    std::cout << "START Token: " << tokens[i].lexeme << " start pos: " << std::to_string(tokens[i].pos.start)
+              << " end pos: " << std::to_string(tokens[i].pos.end) << std::endl;
+
     if (i >= tokens.size() || tokens[i].type != TokenType::LeftCurlyBracket) {
         THROW_UNEXPECTED_TOKEN_ERROR(tokens[i], "{");
     }
     i++;  // Skip '{'
-    std::string lines;
 
     while (i < tokens.size() && tokens[i].type != TokenType::RightCurlyBracket) {
         if (tokens[i].type == TokenType::EndOfLine) {
-            lines += "\n";
             i++;
             continue;
         }
@@ -86,14 +91,16 @@
             throw std::runtime_error("Unexpected end of file");
             break;
         }
-        lines += tokens[i].lexeme + " ";
         i++;
     }
+    end = tokens[i].pos.start - 1;
+
+    std::cout << "END Token: " << tokens[i].lexeme << " start pos: " << std::to_string(tokens[i].pos.start)
+              << " end pos: " << std::to_string(tokens[i].pos.end) << std::endl;
 
     if (i >= tokens.size() || tokens[i].type != TokenType::RightCurlyBracket) {
         THROW_UNEXPECTED_TOKEN_ERROR(tokens[i], "}");
     }
-    return lines;
 };
 
 };  // namespace ScriptInterpreterHelpers

--
Gitblit v1.9.3