Commit b6dd4811 authored by Tobias WEBER's avatar Tobias WEBER
Browse files

a) continued with data merging; b) cleanups

parent 7e46acbf
......@@ -81,7 +81,8 @@ add_executable(in20
${BISON_cliparser_OUTPUT_SOURCE} ${BISON_cliparser_OUTPUT_HEADER}
${FLEX_clilexer_OUTPUTS} ${FLEX_clilexer_OUTPUT_HEADER}
tools/cli/cliparser.cpp tools/cli/cliparser.h tools/cli/cliparser_types.h
tools/cli/ast.cpp tools/cli/funcs.cpp tools/cli/funcs.h
tools/cli/ast.cpp tools/cli/sym.cpp
tools/cli/funcs.cpp tools/cli/funcs.h
ext/tlibs/log/log.cpp ext/tlibs/file/loadinstr.cpp
......
......@@ -15,348 +15,6 @@
using t_real = t_real_cli;
// ----------------------------------------------------------------------------
// evaluation of symbols
// ----------------------------------------------------------------------------
/**
* symbol type name
*/
const std::string& Symbol::get_type_name(const Symbol &sym)
{
static const std::unordered_map<SymbolType, std::string> map =
{
std::make_pair(SymbolType::REAL, "real"),
std::make_pair(SymbolType::STRING, "string"),
std::make_pair(SymbolType::LIST, "list"),
std::make_pair(SymbolType::ARRAY, "array"),
std::make_pair(SymbolType::DATASET, "dataset"),
};
return map.find(sym.GetType())->second;
}
/**
* unary minus of a symbol
*/
std::shared_ptr<Symbol> Symbol::uminus(const Symbol &sym)
{
if(sym.GetType()==SymbolType::REAL)
{ // -1.23
return std::make_shared<SymbolReal>(-dynamic_cast<const SymbolReal&>(sym).GetValue());
}
if(sym.GetType()==SymbolType::ARRAY)
{ // -[1,2,3]
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr = dynamic_cast<const SymbolList&>(sym).GetValue();
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::uminus(*arr[idx]));
return std::make_shared<SymbolList>(arrNew, false);
}
else if(sym.GetType()==SymbolType::DATASET)
{ // -data
return std::make_shared<SymbolDataset>(-dynamic_cast<const SymbolDataset&>(sym).GetValue());
}
return nullptr;
}
/**
* addition of symbols
*/
std::shared_ptr<Symbol> Symbol::add(const Symbol &sym1, const Symbol &sym2)
{
if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
{ // 1.23 + 1.23
return std::make_shared<SymbolReal>(
dynamic_cast<const SymbolReal&>(sym1).GetValue() +
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::ARRAY)
{ // [1,2,3] + [1,2,3]
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr1 = dynamic_cast<const SymbolList&>(sym1).GetValue();
const auto& arr2 = dynamic_cast<const SymbolList&>(sym2).GetValue();
for(std::size_t idx=0; idx<std::min(arr1.size(), arr2.size()); ++idx)
arrNew.emplace_back(Symbol::add(*arr1[idx], *arr2[idx]));
return std::make_shared<SymbolList>(arrNew, false);
}
if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::ARRAY)
{ // 1.23 + [1,2,3], point-wise addition
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& val = dynamic_cast<const SymbolReal&>(sym1);
const auto& arr = dynamic_cast<const SymbolList&>(sym2).GetValue();
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::add(val, *arr[idx]));
return std::make_shared<SymbolList>(arrNew, false);
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
{ // [1,2,3] + 1.23, point-wise addition
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
const auto& val = dynamic_cast<const SymbolReal&>(sym2);
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::add(*arr[idx], val));
return std::make_shared<SymbolList>(arrNew, false);
}
else if(sym1.GetType()==SymbolType::STRING && sym2.GetType()==SymbolType::STRING)
{ // "abc" + "123"
return std::make_shared<SymbolString>(
dynamic_cast<const SymbolString&>(sym1).GetValue() +
dynamic_cast<const SymbolString&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::DATASET)
{ // data1 + data2
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() +
dynamic_cast<const SymbolDataset&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
{ // data + 1.23
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() +
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
{ // 1.23 + data
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolReal&>(sym1).GetValue() +
dynamic_cast<const SymbolDataset&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::STRING && sym2.GetType()==SymbolType::REAL)
{ // "abc" + 3
const std::string &str = dynamic_cast<const SymbolString&>(sym1).GetValue();
t_real val = dynamic_cast<const SymbolReal&>(sym2).GetValue();
std::ostringstream ostr;
ostr << str << val;
return std::make_shared<SymbolString>(ostr.str());
}
else if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::STRING)
{ // 3 + "abc"
t_real val = dynamic_cast<const SymbolReal&>(sym1).GetValue();
const std::string &str = dynamic_cast<const SymbolString&>(sym2).GetValue();
std::ostringstream ostr;
ostr << val << str;
return std::make_shared<SymbolString>(ostr.str());
}
return nullptr;
}
std::shared_ptr<Symbol> Symbol::add(std::shared_ptr<Symbol> sym1, std::shared_ptr<Symbol> sym2)
{
return Symbol::add(*sym1, *sym2);
}
/**
* subtraction of symbols
*/
std::shared_ptr<Symbol> Symbol::sub(const Symbol &sym1, const Symbol &sym2)
{
if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
{ // 1.23 - 2.34
return std::make_shared<SymbolReal>(
dynamic_cast<const SymbolReal&>(sym1).GetValue() -
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::ARRAY)
{ // [1,2,3] - [1,2,3]
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr1 = dynamic_cast<const SymbolList&>(sym1).GetValue();
const auto& arr2 = dynamic_cast<const SymbolList&>(sym2).GetValue();
for(std::size_t idx=0; idx<std::min(arr1.size(), arr2.size()); ++idx)
arrNew.emplace_back(Symbol::sub(*arr1[idx], *arr2[idx]));
return std::make_shared<SymbolList>(arrNew, false);
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
{ // [1,2,3] - 1.23, point-wise subtraction
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
const auto& val = dynamic_cast<const SymbolReal&>(sym2);
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::sub(*arr[idx], val));
return std::make_shared<SymbolList>(arrNew, false);
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::DATASET)
{ // data1 - data2
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() -
dynamic_cast<const SymbolDataset&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
{ // data - 1.23
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() -
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
return nullptr;
}
std::shared_ptr<Symbol> Symbol::sub(std::shared_ptr<Symbol> sym1, std::shared_ptr<Symbol> sym2)
{
return Symbol::sub(*sym1, *sym2);
}
/**
* multiplication of symbols
*/
std::shared_ptr<Symbol> Symbol::mul(const Symbol &sym1, const Symbol &sym2)
{
if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
{ // 1.23 * 2.34
return std::make_shared<SymbolReal>(
dynamic_cast<const SymbolReal&>(sym1).GetValue() *
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::STRING)
{ // 5 * "123"
const std::string &str = dynamic_cast<const SymbolString&>(sym2).GetValue();
std::string strOut;
std::size_t n = static_cast<std::size_t>(dynamic_cast<const SymbolReal&>(sym1).GetValue());
for(std::size_t i=0; i<n; ++i)
strOut += str;
return std::make_shared<SymbolString>(strOut);
}
else if(sym1.GetType()==SymbolType::STRING && sym2.GetType()==SymbolType::REAL)
{ // 5 * "123"
const std::string &str = dynamic_cast<const SymbolString&>(sym1).GetValue();
std::string strOut;
std::size_t n = static_cast<std::size_t>(dynamic_cast<const SymbolReal&>(sym2).GetValue());
for(std::size_t i=0; i<n; ++i)
strOut += str;
return std::make_shared<SymbolString>(strOut);
}
if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::ARRAY)
{ // 5 * [1,2,3]
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr = dynamic_cast<const SymbolList&>(sym2).GetValue();
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::mul(sym1, *arr[idx]));
return std::make_shared<SymbolList>(arrNew, false);
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
{ // [1,2,3] * 5
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::mul(*arr[idx], sym2));
return std::make_shared<SymbolList>(arrNew, false);
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::ARRAY)
{ // [1,2,3] * [1,2,3]
return func_dot(std::make_shared<SymbolList>(dynamic_cast<const SymbolList&>(sym1)),
std::make_shared<SymbolList>(dynamic_cast<const SymbolList&>(sym2)));
}
else if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::DATASET)
{ // 3 * data
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolReal&>(sym1).GetValue() *
dynamic_cast<const SymbolDataset&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
{ // data * 3
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() *
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
return nullptr;
}
std::shared_ptr<Symbol> Symbol::mul(std::shared_ptr<Symbol> sym1, std::shared_ptr<Symbol> sym2)
{
return Symbol::mul(*sym1, *sym2);
}
/**
* division of symbols
*/
std::shared_ptr<Symbol> Symbol::div(const Symbol &sym1, const Symbol &sym2)
{
if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
{ // 1.23 / 5.67
return std::make_shared<SymbolReal>(
dynamic_cast<const SymbolReal&>(sym1).GetValue() /
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
{ // [1,2,3] / 5
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::div(*arr[idx], sym2));
return std::make_shared<SymbolList>(arrNew, false);
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
{ // data / 5.
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() /
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
return nullptr;
}
std::shared_ptr<Symbol> Symbol::div(std::shared_ptr<Symbol> sym1, std::shared_ptr<Symbol> sym2)
{
return Symbol::div(*sym1, *sym2);
}
/**
* modulo of symbols
*/
std::shared_ptr<Symbol> Symbol::mod(const Symbol &sym1, const Symbol &sym2)
{
if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
{ // 12. % 5.
return std::make_shared<SymbolReal>(std::fmod(
dynamic_cast<const SymbolReal&>(sym1).GetValue(),
dynamic_cast<const SymbolReal&>(sym2).GetValue()));
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
{ // [1,2,3] % 5
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::mod(*arr[idx], sym2));
return std::make_shared<SymbolList>(arrNew, false);
}
return nullptr;
}
/**
* power of symbols
*/
std::shared_ptr<Symbol> Symbol::pow(const Symbol &sym1, const Symbol &sym2)
{
if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
{
return std::make_shared<SymbolReal>(std::pow(
dynamic_cast<const SymbolReal&>(sym1).GetValue(),
dynamic_cast<const SymbolReal&>(sym2).GetValue()));
}
return nullptr;
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// evaluation of the AST
......@@ -731,6 +389,17 @@ std::shared_ptr<Symbol> CliASTCall::Eval(CliParserContext& ctx) const
}
}
// general case: function call with a variable number of arguments
{
if(auto iter = g_funcs_gen_vararg.find(ident); iter != g_funcs_gen_vararg.end())
{ // general function
return (*std::get<0>(iter->second))(ctx, args);
}
}
// special cases
if(args.size() == 0) // function call with no arguments requested
{
if(auto iter = g_funcs_gen_0args.find(ident); iter != g_funcs_gen_0args.end())
......
This diff is collapsed.
......@@ -9,6 +9,7 @@
#define __FUNCS_H__
#include <unordered_map>
#include <vector>
#include <tuple>
#include <string>
#include <memory>
......@@ -29,6 +30,7 @@ extern std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)(std
extern std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)(CliParserContext&), std::string>> g_funcs_gen_0args;
extern std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)(CliParserContext&, std::shared_ptr<Symbol>), std::string>> g_funcs_gen_1arg;
extern std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)(CliParserContext&, std::shared_ptr<Symbol>, std::shared_ptr<Symbol>), std::string>> g_funcs_gen_2args;
extern std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)(CliParserContext&, const std::vector<std::shared_ptr<Symbol>>&), std::string>> g_funcs_gen_vararg;
// constants
extern std::unordered_map<std::string, std::tuple<t_real_cli, std::string>> g_consts_real;
......
</
/**
* Evaluates symbols
* @author Tobias Weber <tweber@ill.fr>
* @date 15-Jun-2018
* @license see 'LICENSE' file
*/
#include "cliparser.h"
#include "tools/in20/globals.h"
#include "funcs.h"
#include <cmath>
using t_real = t_real_cli;
// ----------------------------------------------------------------------------
// evaluation of symbols
// ----------------------------------------------------------------------------
/**
* symbol type name
*/
const std::string& Symbol::get_type_name(const Symbol &sym)
{
static const std::unordered_map<SymbolType, std::string> map =
{
std::make_pair(SymbolType::REAL, "real"),
std::make_pair(SymbolType::STRING, "string"),
std::make_pair(SymbolType::LIST, "list"),
std::make_pair(SymbolType::ARRAY, "array"),
std::make_pair(SymbolType::DATASET, "dataset"),
};
return map.find(sym.GetType())->second;
}
/**
* unary minus of a symbol
*/
std::shared_ptr<Symbol> Symbol::uminus(const Symbol &sym)
{
if(sym.GetType()==SymbolType::REAL)
{ // -1.23
return std::make_shared<SymbolReal>(-dynamic_cast<const SymbolReal&>(sym).GetValue());
}
if(sym.GetType()==SymbolType::ARRAY)
{ // -[1,2,3]
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr = dynamic_cast<const SymbolList&>(sym).GetValue();
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::uminus(*arr[idx]));
return std::make_shared<SymbolList>(arrNew, false);
}
else if(sym.GetType()==SymbolType::DATASET)
{ // -data
return std::make_shared<SymbolDataset>(-dynamic_cast<const SymbolDataset&>(sym).GetValue());
}
return nullptr;
}
/**
* addition of symbols
*/
std::shared_ptr<Symbol> Symbol::add(const Symbol &sym1, const Symbol &sym2)
{
if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
{ // 1.23 + 1.23
return std::make_shared<SymbolReal>(
dynamic_cast<const SymbolReal&>(sym1).GetValue() +
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::ARRAY)
{ // [1,2,3] + [1,2,3]
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr1 = dynamic_cast<const SymbolList&>(sym1).GetValue();
const auto& arr2 = dynamic_cast<const SymbolList&>(sym2).GetValue();
for(std::size_t idx=0; idx<std::min(arr1.size(), arr2.size()); ++idx)
arrNew.emplace_back(Symbol::add(*arr1[idx], *arr2[idx]));
return std::make_shared<SymbolList>(arrNew, false);
}
if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::ARRAY)
{ // 1.23 + [1,2,3], point-wise addition
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& val = dynamic_cast<const SymbolReal&>(sym1);
const auto& arr = dynamic_cast<const SymbolList&>(sym2).GetValue();
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::add(val, *arr[idx]));
return std::make_shared<SymbolList>(arrNew, false);
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
{ // [1,2,3] + 1.23, point-wise addition
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
const auto& val = dynamic_cast<const SymbolReal&>(sym2);
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::add(*arr[idx], val));
return std::make_shared<SymbolList>(arrNew, false);
}
else if(sym1.GetType()==SymbolType::STRING && sym2.GetType()==SymbolType::STRING)
{ // "abc" + "123"
return std::make_shared<SymbolString>(
dynamic_cast<const SymbolString&>(sym1).GetValue() +
dynamic_cast<const SymbolString&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::DATASET)
{ // data1 + data2
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() +
dynamic_cast<const SymbolDataset&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
{ // data + 1.23
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() +
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
{ // 1.23 + data
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolReal&>(sym1).GetValue() +
dynamic_cast<const SymbolDataset&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::STRING && sym2.GetType()==SymbolType::REAL)
{ // "abc" + 3
const std::string &str = dynamic_cast<const SymbolString&>(sym1).GetValue();
t_real val = dynamic_cast<const SymbolReal&>(sym2).GetValue();
std::ostringstream ostr;
ostr << str << val;
return std::make_shared<SymbolString>(ostr.str());
}
else if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::STRING)
{ // 3 + "abc"
t_real val = dynamic_cast<const SymbolReal&>(sym1).GetValue();
const std::string &str = dynamic_cast<const SymbolString&>(sym2).GetValue();
std::ostringstream ostr;
ostr << val << str;
return std::make_shared<SymbolString>(ostr.str());
}
return nullptr;
}
std::shared_ptr<Symbol> Symbol::add(std::shared_ptr<Symbol> sym1, std::shared_ptr<Symbol> sym2)
{
return Symbol::add(*sym1, *sym2);
}
/**
* subtraction of symbols
*/
std::shared_ptr<Symbol> Symbol::sub(const Symbol &sym1, const Symbol &sym2)
{
if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::REAL)
{ // 1.23 - 2.34
return std::make_shared<SymbolReal>(
dynamic_cast<const SymbolReal&>(sym1).GetValue() -
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::ARRAY)
{ // [1,2,3] - [1,2,3]
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr1 = dynamic_cast<const SymbolList&>(sym1).GetValue();
const auto& arr2 = dynamic_cast<const SymbolList&>(sym2).GetValue();
for(std::size_t idx=0; idx<std::min(arr1.size(), arr2.size()); ++idx)
arrNew.emplace_back(Symbol::sub(*arr1[idx], *arr2[idx]));
return std::make_shared<SymbolList>(arrNew, false);
}
if(sym1.GetType()==SymbolType::ARRAY && sym2.GetType()==SymbolType::REAL)
{ // [1,2,3] - 1.23, point-wise subtraction
std::vector<std::shared_ptr<Symbol>> arrNew;
const auto& arr = dynamic_cast<const SymbolList&>(sym1).GetValue();
const auto& val = dynamic_cast<const SymbolReal&>(sym2);
for(std::size_t idx=0; idx<arr.size(); ++idx)
arrNew.emplace_back(Symbol::sub(*arr[idx], val));
return std::make_shared<SymbolList>(arrNew, false);
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::DATASET)
{ // data1 - data2
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() -
dynamic_cast<const SymbolDataset&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
{ // data - 1.23
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() -