From 448efaa47f1bab20b1466dcff6428e2d6d35ef9a Mon Sep 17 00:00:00 2001
From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz>
Date: Sat, 26 Aug 2017 20:59:03 +0200
Subject: [PATCH] take out core of algorithm evaluation in cli

---
 alib2cli/src/ast/common/AlgorithmHelper.cpp   | 71 +++++++++++++++++++
 alib2cli/src/ast/common/AlgorithmHelper.h     | 25 +++++++
 alib2cli/src/ast/common/CastHelper.cpp        | 32 +++++++++
 alib2cli/src/ast/common/CastHelper.h          | 26 +------
 alib2cli/src/ast/params/CastParam.h           |  2 +-
 alib2cli/src/ast/statements/CastStatement.h   |  2 +-
 .../src/ast/statements/SingleStatement.cpp    | 56 +--------------
 7 files changed, 133 insertions(+), 81 deletions(-)
 create mode 100644 alib2cli/src/ast/common/AlgorithmHelper.cpp
 create mode 100644 alib2cli/src/ast/common/AlgorithmHelper.h
 create mode 100644 alib2cli/src/ast/common/CastHelper.cpp

diff --git a/alib2cli/src/ast/common/AlgorithmHelper.cpp b/alib2cli/src/ast/common/AlgorithmHelper.cpp
new file mode 100644
index 0000000000..6092b0b4aa
--- /dev/null
+++ b/alib2cli/src/ast/common/AlgorithmHelper.cpp
@@ -0,0 +1,71 @@
+/*
+ * AlgorithmHelper.cpp
+ *
+ *  Created on: 25. 8. 2017
+ *	  Author: Jan Travnicek
+ */
+
+#include <abstraction/Registry.h>
+#include <exception/CommonException.h>
+#include <ast/common/AlgorithmHelper.h>
+#include <ast/common/CastHelper.h>
+
+namespace cli {
+
+std::shared_ptr < abstraction::OperationAbstraction > AlgorithmHelper::eval ( const std::string & name, const ext::vector < std::shared_ptr < abstraction::OperationAbstraction > > & params, std::vector < bool > & moves ) {
+	ext::vector < std::string > paramTypes;
+	for ( const std::shared_ptr < abstraction::OperationAbstraction > & param : params )
+		paramTypes.push_back ( param->getReturnType ( ) );
+
+	bool downcast = false;
+	bool normalize = false;
+
+	std::shared_ptr < abstraction::OperationAbstraction > algo = abstraction::Registry::getAlgorithmAbstraction ( name, paramTypes, downcast, normalize );
+
+	unsigned i = 0;
+	ext::vector < std::shared_ptr < abstraction::OperationAbstraction > > casted_params;
+	for ( const std::shared_ptr < abstraction::OperationAbstraction > & param : params ) {
+		if ( abstraction::Registry::isCastNoOp ( algo->getParamType ( i ), param->getReturnType ( ) ) ) {
+			casted_params.push_back ( param );
+		} else {
+			casted_params.push_back ( CastHelper::eval ( param, algo->getParamType ( i ), moves [ i ] ) );
+			moves [ i ] = true;
+		}
+		++ i;
+	}
+
+	i = 0;
+	for ( const std::shared_ptr < abstraction::OperationAbstraction > & param : casted_params ) {
+		if ( ! algo->attachInput ( param, i, moves [ i ] ) )
+			throw exception::CommonException ( "Can't connect param at " + ext::to_string ( i ) + " of algorithm " + name + " with result of type " + param->getReturnType ( ) + "." );
+		++ i;
+	}
+
+	if ( ! algo->eval ( ) )
+		throw exception::CommonException ( "Eval of algorithm " + name + " failed." );
+
+	if ( downcast ) {
+		std::shared_ptr < abstraction::OperationAbstraction > downcaster = abstraction::Registry::getDowncastAbstraction ( algo->getRuntimeReturnType ( ), algo->getReturnType ( ) );
+		if ( ! downcaster->attachInput ( algo, 0, true ) )
+			throw exception::CommonException ( "Can't connect param at 0 of downcast of algorithm " + name + " with result of type " + algo->getReturnType ( ) + "." );
+		if ( ! downcaster->eval ( ) )
+			throw exception::CommonException ( "Eval of downcast of algorithm " + name + " failed." );
+
+		algo = downcaster;
+	}
+
+	if ( normalize ) {
+		std::shared_ptr < abstraction::OperationAbstraction > normalized = abstraction::Registry::getNormalizeAbstraction ( algo->getReturnType ( ) );
+		if ( ! normalized->attachInput ( algo, 0, true ) )
+			throw exception::CommonException ( "Can't connect param at 0 of normalize of algorithm " + name + " with result of type " + algo->getReturnType ( ) + "." );
+		if ( ! normalized->eval ( ) )
+			throw exception::CommonException ( "Eval of normalize of algorithm " + name + " failed." );
+
+		algo = normalized;
+	}
+
+	return algo;
+}
+
+
+} /* namespace cli */
diff --git a/alib2cli/src/ast/common/AlgorithmHelper.h b/alib2cli/src/ast/common/AlgorithmHelper.h
new file mode 100644
index 0000000000..304a86e99f
--- /dev/null
+++ b/alib2cli/src/ast/common/AlgorithmHelper.h
@@ -0,0 +1,25 @@
+/*
+ * AbstractionHelper.hpp
+ *
+ *  Created on: 25. 8. 2017
+ *	  Author: Jan Travnicek
+ */
+
+#ifndef _ALGORITHM_HELPER_HPP_
+#define _ALGORITHM_HELPER_HPP_
+
+#include <string>
+#include <memory>
+#include <vector>
+
+namespace cli {
+
+class AlgorithmHelper {
+public:
+	static std::shared_ptr < abstraction::OperationAbstraction > eval ( const std::string & algo, const ext::vector < std::shared_ptr < abstraction::OperationAbstraction > > & params, std::vector < bool > & moves );
+
+};
+
+} /* namespace cli */
+
+#endif /* _ALGORITHM_HELPER_HPP_ */
diff --git a/alib2cli/src/ast/common/CastHelper.cpp b/alib2cli/src/ast/common/CastHelper.cpp
new file mode 100644
index 0000000000..9c0564fed7
--- /dev/null
+++ b/alib2cli/src/ast/common/CastHelper.cpp
@@ -0,0 +1,32 @@
+#include <abstraction/Registry.h>
+#include <ast/common/CastHelper.h>
+
+namespace cli {
+
+std::shared_ptr < abstraction::OperationAbstraction > CastHelper::eval ( const std::shared_ptr < abstraction::OperationAbstraction > & param, const std::string & type, bool move ) {
+	if ( abstraction::Registry::isCastNoOp ( type, param->getReturnType ( ) ) ) {
+		return param;
+	}
+
+	bool normalize;
+
+	std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getCastAbstraction ( type, param->getReturnType ( ), normalize );
+	if ( ! res->attachInput ( param, 0, move ) )
+		throw exception::CommonException ( "Can't connect param at 0 of cast to " + type + " with result of type " + param->getReturnType ( ) + "." );
+	if ( ! res->eval ( ) )
+		throw exception::CommonException ( "Eval of cast to " + type + " failed." );
+
+	if ( normalize ) {
+		std::shared_ptr < abstraction::OperationAbstraction > normalized = abstraction::Registry::getNormalizeAbstraction ( res->getReturnType ( ) );
+		if ( ! normalized->attachInput ( res, 0, true ) )
+			throw exception::CommonException ( "Can't connect param at 0 of normalize of cast to " + type + " with result of type " + res->getReturnType ( ) + "." );
+		if ( ! normalized->eval ( ) )
+			throw exception::CommonException ( "Eval of normalize of cast to " + type + " failed." );
+
+		res = normalized;
+	}
+
+	return res;
+}
+
+} /* namespace cli */
diff --git a/alib2cli/src/ast/common/CastHelper.h b/alib2cli/src/ast/common/CastHelper.h
index 22a6d3d264..769611a015 100644
--- a/alib2cli/src/ast/common/CastHelper.h
+++ b/alib2cli/src/ast/common/CastHelper.h
@@ -7,31 +7,7 @@ namespace cli {
 
 class CastHelper {
 public:
-	static std::shared_ptr < abstraction::OperationAbstraction > castHelper ( const std::shared_ptr < abstraction::OperationAbstraction > & param, const std::string & type, bool move ) {
-		if ( abstraction::Registry::isCastNoOp ( type, param->getReturnType ( ) ) ) {
-			return param;
-		}
-
-		bool normalize;
-
-		std::shared_ptr < abstraction::OperationAbstraction > res = abstraction::Registry::getCastAbstraction ( type, param->getReturnType ( ), normalize );
-		if ( ! res->attachInput ( param, 0, move ) )
-			throw exception::CommonException ( "Can't connect param at 0 of cast to " + type + " with result of type " + param->getReturnType ( ) + "." );
-		if ( ! res->eval ( ) )
-			throw exception::CommonException ( "Eval of cast to " + type + " failed." );
-
-		if ( normalize ) {
-			std::shared_ptr < abstraction::OperationAbstraction > normalized = abstraction::Registry::getNormalizeAbstraction ( res->getReturnType ( ) );
-			if ( ! normalized->attachInput ( res, 0, true ) )
-				throw exception::CommonException ( "Can't connect param at 0 of normalize of cast to " + type + " with result of type " + res->getReturnType ( ) + "." );
-			if ( ! normalized->eval ( ) )
-				throw exception::CommonException ( "Eval of normalize of cast to " + type + " failed." );
-
-			res = normalized;
-		}
-
-		return res;
-	}
+	static std::shared_ptr < abstraction::OperationAbstraction > eval ( const std::shared_ptr < abstraction::OperationAbstraction > & param, const std::string & type, bool move );
 
 };
 
diff --git a/alib2cli/src/ast/params/CastParam.h b/alib2cli/src/ast/params/CastParam.h
index ceec38b00a..960a503836 100644
--- a/alib2cli/src/ast/params/CastParam.h
+++ b/alib2cli/src/ast/params/CastParam.h
@@ -19,7 +19,7 @@ public:
 
 		std::shared_ptr < abstraction::OperationAbstraction > translatedParam = m_param->translateAndEval ( prev, environment );
 
-		return CastHelper::castHelper ( translatedParam, type, getMove ( ) );
+		return CastHelper::eval ( translatedParam, type, getMove ( ) );
 	}
 };
 
diff --git a/alib2cli/src/ast/statements/CastStatement.h b/alib2cli/src/ast/statements/CastStatement.h
index ba0fc9bfdd..f66aafbc10 100644
--- a/alib2cli/src/ast/statements/CastStatement.h
+++ b/alib2cli/src/ast/statements/CastStatement.h
@@ -20,7 +20,7 @@ public:
 
 		std::shared_ptr < abstraction::OperationAbstraction > translatedStatement = m_statement->translateAndEval ( prev, environment );
 
-		return CastHelper::castHelper ( translatedStatement, type, m_move );
+		return CastHelper::eval ( translatedStatement, type, m_move );
 	}
 
 };
diff --git a/alib2cli/src/ast/statements/SingleStatement.cpp b/alib2cli/src/ast/statements/SingleStatement.cpp
index b3ac9752d5..a579159d22 100644
--- a/alib2cli/src/ast/statements/SingleStatement.cpp
+++ b/alib2cli/src/ast/statements/SingleStatement.cpp
@@ -2,7 +2,7 @@
 #include <ast/Option.h>
 #include <ast/Param.h>
 #include <ast/Arg.h>
-#include <ast/common/CastHelper.h>
+#include <ast/common/AlgorithmHelper.h>
 
 namespace cli {
 
@@ -23,61 +23,9 @@ std::shared_ptr < abstraction::OperationAbstraction > SingleStatement::translate
 		moves.push_back ( param->getMove ( ) );
 	}
 
-	ext::vector < std::string > paramTypes;
-	for ( const std::shared_ptr < abstraction::OperationAbstraction > & param : params ) {
-		paramTypes.push_back ( param->getReturnType ( ) );
-	}
-
 	std::string name = m_name->eval ( environment );
 
-	bool downcast = false;
-	bool normalize = false;
-
-	std::shared_ptr < abstraction::OperationAbstraction > algo = abstraction::Registry::getAlgorithmAbstraction ( name, paramTypes, downcast, normalize );
-
-	unsigned i = 0;
-	ext::vector < std::shared_ptr < abstraction::OperationAbstraction > > casted_params;
-	for ( const std::shared_ptr < abstraction::OperationAbstraction > & param : params ) {
-		if ( abstraction::Registry::isCastNoOp ( algo->getParamType ( i ), param->getReturnType ( ) ) ) {
-			casted_params.push_back ( param );
-		} else {
-			casted_params.push_back ( CastHelper::castHelper ( param, algo->getParamType ( i ), moves [ i ] ) );
-			moves [ i ] = true;
-		}
-		++ i;
-	}
-
-	i = 0;
-	for ( const std::shared_ptr < abstraction::OperationAbstraction > & param : casted_params ) {
-		if ( ! algo->attachInput ( param, i, moves [ i ] ) )
-			throw exception::CommonException ( "Can't connect param at " + ext::to_string ( i ) + " of algorithm " + name + " with result of type " + param->getReturnType ( ) + "." );
-		++ i;
-	}
-
-	if ( ! algo->eval ( ) )
-		throw exception::CommonException ( "Eval of algorithm " + name + " failed." );
-
-	if ( downcast ) {
-		std::shared_ptr < abstraction::OperationAbstraction > downcaster = abstraction::Registry::getDowncastAbstraction ( algo->getRuntimeReturnType ( ), algo->getReturnType ( ) );
-		if ( ! downcaster->attachInput ( algo, 0, true ) )
-			throw exception::CommonException ( "Can't connect param at 0 of downcast of algorithm " + name + " with result of type " + algo->getReturnType ( ) + "." );
-		if ( ! downcaster->eval ( ) )
-			throw exception::CommonException ( "Eval of downcast of algorithm " + name + " failed." );
-
-		algo = downcaster;
-	}
-
-	if ( normalize ) {
-		std::shared_ptr < abstraction::OperationAbstraction > normalized = abstraction::Registry::getNormalizeAbstraction ( algo->getReturnType ( ) );
-		if ( ! normalized->attachInput ( algo, 0, true ) )
-			throw exception::CommonException ( "Can't connect param at 0 of normalize of algorithm " + name + " with result of type " + algo->getReturnType ( ) + "." );
-		if ( ! normalized->eval ( ) )
-			throw exception::CommonException ( "Eval of normalize of algorithm " + name + " failed." );
-
-		algo = normalized;
-	}
-
-	return algo;
+	return AlgorithmHelper::eval ( name, params, moves );
 }
 
 } /* namespace cli */
-- 
GitLab