From a76181288ae27b85521838ee87352727c3cba2f8 Mon Sep 17 00:00:00 2001
From: Ferenc Szontágh <szf@fsociety.hu>
Date: Fri, 18 Apr 2025 10:15:12 +0000
Subject: [PATCH] call function when variable defined

---
 src/Parser/ParsedExpression.hpp |   25 ++++++++++++++++++++++++-
 1 files changed, 24 insertions(+), 1 deletions(-)

diff --git a/src/Parser/ParsedExpression.hpp b/src/Parser/ParsedExpression.hpp
index efc501f..06a462b 100644
--- a/src/Parser/ParsedExpression.hpp
+++ b/src/Parser/ParsedExpression.hpp
@@ -3,9 +3,11 @@
 
 #include <memory>
 #include <string>
+#include <vector>
 
 #include "../Symbols/SymbolContainer.hpp"
 #include "../Symbols/Value.hpp"
+#include "../Symbols/FunctionSymbol.hpp"
 
 namespace Parser {
 
@@ -14,7 +16,7 @@
 using ParsedExpressionPtr = std::unique_ptr<ParsedExpression>;
 
 struct ParsedExpression {
-    enum class Kind : std::uint8_t { Literal, Variable, Binary, Unary };
+    enum class Kind : std::uint8_t { Literal, Variable, Binary, Unary, Call };
 
     Kind kind;
 
@@ -25,6 +27,8 @@
     std::string         op;
     ParsedExpressionPtr lhs;
     ParsedExpressionPtr rhs;
+    // For function call arguments
+    std::vector<ParsedExpressionPtr> args;
 
     // Constructor for literal
     static ParsedExpressionPtr makeLiteral(const Symbols::Value & val) {
@@ -60,6 +64,14 @@
         expr->rhs  = std::move(operand);
         return expr;
     }
+    // Constructor for function call
+    static ParsedExpressionPtr makeCall(const std::string &name, std::vector<ParsedExpressionPtr> arguments) {
+        auto expr        = std::make_unique<ParsedExpression>();
+        expr->kind       = Kind::Call;
+        expr->name       = name;
+        expr->args       = std::move(arguments);
+        return expr;
+    }
 
     Symbols::Variables::Type getType() const {
         switch (kind) {
@@ -93,6 +105,17 @@
                     }
                     break;
                 }
+            case Kind::Call:
+                {
+                    const std::string ns = Symbols::SymbolContainer::instance()->currentScopeName() + ".functions";
+                    auto symbol = Symbols::SymbolContainer::instance()->get(ns, name);
+                    if (!symbol) {
+                        throw std::runtime_error("Unknown function: " + name + " in namespace: " + ns);
+                    }
+                    // FunctionSymbol holds return type
+                    auto funcSym = std::static_pointer_cast<Symbols::FunctionSymbol>(symbol);
+                    return funcSym->returnType();
+                }
 
             default:
                 throw std::runtime_error("Unknown expression kind");

--
Gitblit v1.9.3