From a3c1fb0d11d0618828fb6691a628aa6d27da94ef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Del=20Pino?= <stephane.delpino44@gmail.com>
Date: Fri, 23 Jul 2021 15:07:56 +0200
Subject: [PATCH] Fix issue #18

++ and -- operators are now forbidden in function definitions.
This is the correct behavior for mathematical functions

- concerning function arguments, it was already not supported but
  resulted in a crash
- with regard to outer variables, the behavior is changed since now
  these cannot be modified anymore while calling the
  function. (observe these external parameters can still be modified
  outside of the function definition itself)
---
 src/language/ast/ASTNodeExpressionBuilder.cpp | 15 +++++++++++++++
 src/language/ast/ASTNodeExpressionBuilder.hpp |  2 ++
 2 files changed, 17 insertions(+)

diff --git a/src/language/ast/ASTNodeExpressionBuilder.cpp b/src/language/ast/ASTNodeExpressionBuilder.cpp
index f8d26d197..d9b3b4e43 100644
--- a/src/language/ast/ASTNodeExpressionBuilder.cpp
+++ b/src/language/ast/ASTNodeExpressionBuilder.cpp
@@ -25,6 +25,19 @@
 #include <language/node_processor/WhileProcessor.hpp>
 #include <language/utils/ParseError.hpp>
 
+void
+ASTNodeExpressionBuilder::_checkIsPureFunction(const ASTNode& node) const
+{
+  if (node.is_type<language::unary_plusplus>() or node.is_type<language::unary_minusminus>() or
+      node.is_type<language::post_plusplus>() or node.is_type<language::post_minusminus>()) {
+    throw ParseError("invalid function definition. Function data must be constant!", node.begin());
+  }
+
+  for (auto&& child : node.children) {
+    this->_checkIsPureFunction(*child);
+  }
+}
+
 void
 ASTNodeExpressionBuilder::_buildExpression(ASTNode& n)
 {
@@ -42,6 +55,8 @@ ASTNodeExpressionBuilder::_buildExpression(ASTNode& n)
   } else if (n.is_type<language::tuple_expression>()) {
     n.m_node_processor = std::make_unique<TupleToVectorProcessor<ASTNodeExpressionListProcessor>>(n);
   } else if (n.is_type<language::function_definition>()) {
+    this->_checkIsPureFunction(n);
+
     n.m_node_processor = std::make_unique<FakeProcessor>();
 
   } else if (n.is_type<language::function_evaluation>()) {
diff --git a/src/language/ast/ASTNodeExpressionBuilder.hpp b/src/language/ast/ASTNodeExpressionBuilder.hpp
index 74a2f7209..0e36b6528 100644
--- a/src/language/ast/ASTNodeExpressionBuilder.hpp
+++ b/src/language/ast/ASTNodeExpressionBuilder.hpp
@@ -5,7 +5,9 @@
 
 class ASTNodeExpressionBuilder
 {
+ private:
   void _buildExpression(ASTNode& n);
+  void _checkIsPureFunction(const ASTNode& n) const;
 
  public:
   ASTNodeExpressionBuilder(ASTNode& n);
-- 
GitLab