Commit 6a9c2a74 authored by Tobias WEBER's avatar Tobias WEBER
Browse files

integrated dataset operators into parser

parent 687f2d73
......@@ -23,6 +23,10 @@ std::shared_ptr<Symbol> Symbol::uminus(const Symbol &sym)
{
return std::make_shared<SymbolReal>(-dynamic_cast<const SymbolReal&>(sym).GetValue());
}
else if(sym.GetType()==SymbolType::DATASET)
{
return std::make_shared<SymbolDataset>(-dynamic_cast<const SymbolDataset&>(sym).GetValue());
}
return nullptr;
}
......@@ -45,6 +49,12 @@ std::shared_ptr<Symbol> Symbol::add(const Symbol &sym1, const Symbol &sym2)
dynamic_cast<const SymbolString&>(sym1).GetValue() +
dynamic_cast<const SymbolString&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::DATASET)
{
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() +
dynamic_cast<const SymbolDataset&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::STRING && sym2.GetType()==SymbolType::REAL)
{
const std::string &str = dynamic_cast<const SymbolString&>(sym1).GetValue();
......@@ -77,6 +87,12 @@ std::shared_ptr<Symbol> Symbol::sub(const Symbol &sym1, const Symbol &sym2)
dynamic_cast<const SymbolReal&>(sym1).GetValue() -
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::DATASET)
{
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() -
dynamic_cast<const SymbolDataset&>(sym2).GetValue());
}
return nullptr;
}
......@@ -115,6 +131,18 @@ std::shared_ptr<Symbol> Symbol::mul(const Symbol &sym1, const Symbol &sym2)
return std::make_shared<SymbolString>(strOut);
}
else if(sym1.GetType()==SymbolType::REAL && sym2.GetType()==SymbolType::DATASET)
{
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)
{
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() *
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
return nullptr;
}
......@@ -131,6 +159,12 @@ std::shared_ptr<Symbol> Symbol::div(const Symbol &sym1, const Symbol &sym2)
dynamic_cast<const SymbolReal&>(sym1).GetValue() /
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
else if(sym1.GetType()==SymbolType::DATASET && sym2.GetType()==SymbolType::REAL)
{
return std::make_shared<SymbolDataset>(
dynamic_cast<const SymbolDataset&>(sym1).GetValue() /
dynamic_cast<const SymbolReal&>(sym2).GetValue());
}
return nullptr;
}
......@@ -177,70 +211,130 @@ std::shared_ptr<Symbol> Symbol::pow(const Symbol &sym1, const Symbol &sym2)
// evaluation of the AST
// ----------------------------------------------------------------------------
/**
* real constant
*/
std::shared_ptr<Symbol> CliASTReal::Eval() const
{
return std::make_shared<SymbolReal>(m_val);
}
/**
* string constant
*/
std::shared_ptr<Symbol> CliASTString::Eval() const
{
return std::make_shared<SymbolString>(m_val);
}
/**
* variable identifier
*/
std::shared_ptr<Symbol> CliASTIdent::Eval() const
{
return nullptr;
}
/**
* assignment operation
*/
std::shared_ptr<Symbol> CliASTAssign::Eval() const
{
return nullptr;
}
/**
* addition
*/
std::shared_ptr<Symbol> CliASTPlus::Eval() const
{
if(m_left && m_right)
return Symbol::add(*m_left->Eval(), *m_right->Eval());
if(!m_left || !m_right)
return nullptr;
if(auto lefteval=m_left->Eval(), righteval=m_right->Eval(); lefteval && righteval)
return Symbol::add(*lefteval, *righteval);
return nullptr;
}
/**
* subtraction
*/
std::shared_ptr<Symbol> CliASTMinus::Eval() const
{
if(m_left && m_right)
return Symbol::sub(*m_left->Eval(), *m_right->Eval());
{
if(auto lefteval=m_left->Eval(), righteval=m_right->Eval(); lefteval && righteval)
return Symbol::sub(*lefteval, *righteval);
}
else if(m_right && !m_left)
{
if(auto righteval=m_right->Eval(); righteval)
return Symbol::uminus(*righteval);
return Symbol::uminus(*m_right->Eval());
}
return nullptr;
}
/**
* multiplication
*/
std::shared_ptr<Symbol> CliASTMult::Eval() const
{
if(m_left && m_right)
return Symbol::mul(*m_left->Eval(), *m_right->Eval());
if(!m_left || !m_right)
return nullptr;
if(auto lefteval=m_left->Eval(), righteval=m_right->Eval(); lefteval && righteval)
return Symbol::mul(*lefteval, *righteval);
return nullptr;
}
/**
* division
*/
std::shared_ptr<Symbol> CliASTDiv::Eval() const
{
if(m_left && m_right)
return Symbol::div(*m_left->Eval(), *m_right->Eval());
if(!m_left || !m_right)
return nullptr;
if(auto lefteval=m_left->Eval(), righteval=m_right->Eval(); lefteval && righteval)
return Symbol::div(*lefteval, *righteval);
return nullptr;
}
/**
* modulo
*/
std::shared_ptr<Symbol> CliASTMod::Eval() const
{
if(m_left && m_right)
return Symbol::mod(*m_left->Eval(), *m_right->Eval());
if(!m_left || !m_right)
return nullptr;
if(auto lefteval=m_left->Eval(), righteval=m_right->Eval(); lefteval && righteval)
return Symbol::mod(*lefteval, *righteval);
return nullptr;
}
/**
* power
*/
std::shared_ptr<Symbol> CliASTPow::Eval() const
{
if(m_left && m_right)
return Symbol::pow(*m_left->Eval(), *m_right->Eval());
if(!m_left || !m_right)
return nullptr;
if(auto lefteval=m_left->Eval(), righteval=m_right->Eval(); lefteval && righteval)
return Symbol::pow(*lefteval, *righteval);
return nullptr;
}
/**
* function call operation
*/
std::shared_ptr<Symbol> CliASTCall::Eval() const
{
return nullptr;
......
......@@ -137,7 +137,7 @@ std::tuple<bool, Dataset> Dataset::convert_instr_file(const char* pcFile)
// ----------------------------------------------------------------------------
// operators
// data operators
// ----------------------------------------------------------------------------
const Data& operator +(const Data& dat)
......@@ -270,3 +270,93 @@ Data operator /(const Data& dat1, t_real d)
}
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// dataset operators
// ----------------------------------------------------------------------------
Dataset operator +(const Dataset& dat1, const Dataset& dat2)
{
Dataset dataset;
for(std::size_t ch=0; ch<std::min(dat1.GetNumChannels(), dat2.GetNumChannels()); ++ch)
{
Data dat = dat1.GetChannel(ch) + dat2.GetChannel(ch);
dataset.AddChannel(std::move(dat));
}
return dataset;
}
Dataset operator -(const Dataset& dat1, const Dataset& dat2)
{
Dataset dataset;
for(std::size_t ch=0; ch<std::min(dat1.GetNumChannels(), dat2.GetNumChannels()); ++ch)
{
Data dat = dat1.GetChannel(ch) - dat2.GetChannel(ch);
dataset.AddChannel(std::move(dat));
}
return dataset;
}
Dataset operator *(const Dataset& dat1, t_real_dat d)
{
Dataset dataset;
for(std::size_t ch=0; ch<dat1.GetNumChannels(); ++ch)
{
Data dat = dat1.GetChannel(ch) * d;
dataset.AddChannel(std::move(dat));
}
return dataset;
}
Dataset operator *(t_real_dat d, const Dataset& dat1)
{
return operator *(dat1, d);
}
Dataset operator /(const Dataset& dat1, t_real_dat d)
{
Dataset dataset;
for(std::size_t ch=0; ch<dat1.GetNumChannels(); ++ch)
{
Data dat = dat1.GetChannel(ch) / d;
dataset.AddChannel(std::move(dat));
}
return dataset;
}
const Dataset& operator +(const Dataset& dat)
{
return dat;
}
Dataset operator -(const Dataset& dat1)
{
Dataset dataset;
for(std::size_t ch=0; ch<dat1.GetNumChannels(); ++ch)
{
Data dat = -dat1.GetChannel(ch);
dataset.AddChannel(std::move(dat));
}
return dataset;
}
// ----------------------------------------------------------------------------
......@@ -126,6 +126,18 @@ public:
void AddChannel(Data &&data) { m_data.emplace_back(std::move(data)); }
// binary operators
friend Dataset operator +(const Dataset& dat1, const Dataset& dat2);
friend Dataset operator -(const Dataset& dat1, const Dataset& dat2);
friend Dataset operator *(const Dataset& dat1, t_real_dat d);
friend Dataset operator *(t_real_dat d, const Dataset& dat1);
friend Dataset operator /(const Dataset& dat1, t_real_dat d);
// unary operators
friend const Dataset& operator +(const Dataset& dat);
friend Dataset operator -(const Dataset& dat);
static std::tuple<bool, Dataset> convert_instr_file(const char* pcFile);
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment