2016-09-02 10:04:42 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Mark Roszko <mark.roszko@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sexpr/sexpr.h"
|
|
|
|
#include <cctype>
|
|
|
|
#include <iterator>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
namespace SEXPR
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR::SEXPR( SEXPR_TYPE aType, size_t aLineNumber ) :
|
|
|
|
m_type( aType ), m_lineNumber( aLineNumber )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR::SEXPR(SEXPR_TYPE aType) :
|
|
|
|
m_type( aType ), m_lineNumber( 1 )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SEXPR_VECTOR const * SEXPR::GetChildren() const
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( m_type != SEXPR_TYPE::SEXPR_TYPE_LIST )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
throw INVALID_TYPE_EXCEPTION("SEXPR is not a list type!");
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
return &static_cast< SEXPR_LIST const * >(this)->m_children;
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2018-07-19 16:15:36 +00:00
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR* SEXPR::GetChild( size_t aIndex ) const
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( m_type != SEXPR_TYPE::SEXPR_TYPE_LIST )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
throw INVALID_TYPE_EXCEPTION("SEXPR is not a list type!");
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
return static_cast< SEXPR_LIST const * >(this)->m_children[aIndex];
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
void SEXPR::AddChild( SEXPR* aChild )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( m_type != SEXPR_TYPE::SEXPR_TYPE_LIST )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
throw INVALID_TYPE_EXCEPTION("SEXPR is not a list type!");
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST* list = static_cast< SEXPR_LIST * >( this );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
list->m_children.push_back( aChild );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t SEXPR::GetNumberOfChildren() const
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( m_type != SEXPR_TYPE::SEXPR_TYPE_LIST )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
throw INVALID_TYPE_EXCEPTION("SEXPR is not a list type!");
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
return static_cast< SEXPR_LIST const * >(this)->m_children.size();
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string const & SEXPR::GetString() const
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( m_type != SEXPR_TYPE::SEXPR_TYPE_ATOM_STRING )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
throw INVALID_TYPE_EXCEPTION("SEXPR is not a string type!");
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
return static_cast< SEXPR_STRING const * >(this)->m_value;
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t SEXPR::GetInteger() const
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
return static_cast< int >( GetLongInteger() );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t SEXPR::GetLongInteger() const
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( m_type != SEXPR_TYPE::SEXPR_TYPE_ATOM_INTEGER )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
throw INVALID_TYPE_EXCEPTION("SEXPR is not a integer type!");
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
return static_cast< SEXPR_INTEGER const * >(this)->m_value;
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double SEXPR::GetDouble() const
|
|
|
|
{
|
|
|
|
// we may end up parsing "intended" floats/doubles as ints
|
|
|
|
// so here we allow silent casting back to doubles
|
2017-02-02 07:58:35 +00:00
|
|
|
if( m_type == SEXPR_TYPE::SEXPR_TYPE_ATOM_DOUBLE )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
return static_cast< SEXPR_DOUBLE const * >(this)->m_value;
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( m_type == SEXPR_TYPE::SEXPR_TYPE_ATOM_INTEGER )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
return static_cast< SEXPR_INTEGER const * >(this)->m_value;
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw INVALID_TYPE_EXCEPTION("SEXPR is not a double type!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float SEXPR::GetFloat() const
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
return static_cast< float >( GetDouble() );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string const & SEXPR::GetSymbol() const
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( m_type != SEXPR_TYPE::SEXPR_TYPE_ATOM_SYMBOL )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
throw INVALID_TYPE_EXCEPTION("SEXPR is not a symbol type!");
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
return static_cast< SEXPR_SYMBOL const * >(this)->m_value;
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SEXPR_LIST* SEXPR::GetList()
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( m_type != SEXPR_TYPE::SEXPR_TYPE_LIST )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
throw INVALID_TYPE_EXCEPTION("SEXPR is not a list type!");
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
return static_cast< SEXPR_LIST* >(this);
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 17:09:27 +00:00
|
|
|
std::string SEXPR::AsString( size_t aLevel ) const
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
if( IsList() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( aLevel != 0 )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
result = "\n";
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
|
2019-11-09 19:39:08 +00:00
|
|
|
result.append( aLevel * 2, ' ' );
|
2017-02-02 07:58:35 +00:00
|
|
|
aLevel++;
|
2016-09-02 10:04:42 +00:00
|
|
|
result += "(";
|
|
|
|
|
|
|
|
SEXPR_VECTOR const* list = GetChildren();
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
for( std::vector<SEXPR *>::const_iterator it = list->begin(); it != list->end(); ++it )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
result += (*it)->AsString( aLevel );
|
|
|
|
|
|
|
|
if( it != list->end() - 1 )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
result += " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += ")";
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
aLevel--;
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( IsString() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
result += "\"" + GetString() + "\"";
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( IsSymbol() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
result += GetSymbol();
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( IsInteger() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
std::stringstream out;
|
|
|
|
out << GetInteger();
|
|
|
|
result += out.str();
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( IsDouble() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
std::stringstream out;
|
2017-02-02 07:58:35 +00:00
|
|
|
out << std::setprecision( 16 ) << GetDouble();
|
2016-09-02 10:04:42 +00:00
|
|
|
result += out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
SEXPR_LIST::~SEXPR_LIST()
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
for( auto child : m_children )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
delete child;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_children.clear();
|
|
|
|
}
|
2018-07-19 16:15:36 +00:00
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator<< ( SEXPR_LIST& list, const ISEXPRABLE& obj )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
SEXPR* sobj = obj.SerializeSEXPR();
|
2017-02-02 07:58:35 +00:00
|
|
|
list.AddChild( sobj );
|
2018-07-19 16:15:36 +00:00
|
|
|
|
2016-09-02 10:04:42 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator<< ( SEXPR_LIST& list, int64_t value )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
list.AddChild( new SEXPR_INTEGER( value ) );
|
2016-09-02 10:04:42 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator<< ( SEXPR_LIST& list, int32_t value )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
list.AddChild( new SEXPR_INTEGER( value ) );
|
2016-09-02 10:04:42 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator<< ( SEXPR_LIST& list, float value )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
list.AddChild( new SEXPR_DOUBLE( value ) );
|
2016-09-02 10:04:42 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator<< ( SEXPR_LIST& list, double value )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
list.AddChild( new SEXPR_DOUBLE( value ) );
|
2016-09-02 10:04:42 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator<< ( SEXPR_LIST& list, std::string value )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
list.AddChild( new SEXPR_STRING( value ) );
|
2016-09-02 10:04:42 +00:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator<< ( SEXPR_LIST& list, SEXPR* obj )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
list.AddChild( obj );
|
2016-09-02 10:04:42 +00:00
|
|
|
return list;
|
|
|
|
}
|
2018-07-19 16:15:36 +00:00
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator<< ( SEXPR_LIST& list, const _OUT_STRING setting )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
SEXPR *res;
|
2017-02-02 07:58:35 +00:00
|
|
|
|
|
|
|
if( setting._Symbol )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
res = new SEXPR_SYMBOL( setting._String );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
res = new SEXPR_STRING( setting._String );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
|
|
|
|
list.AddChild( res );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator>> ( SEXPR_LIST& input, ISEXPRABLE& obj )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
obj.DeserializeSEXPR( input );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator>> ( SEXPR_LIST& input, int32_t& inte )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR* child = input.GetChild( input.m_inStreamChild );
|
|
|
|
|
|
|
|
if( child->IsInteger() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
inte = child->GetInteger();
|
|
|
|
input.m_inStreamChild++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
throw std::invalid_argument( "SEXPR is not a integer type!" );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator>> ( SEXPR_LIST& input, std::string& str )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR* child = input.GetChild( input.m_inStreamChild );
|
|
|
|
|
|
|
|
if( child->IsString() || child->IsSymbol() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
str = child->GetString();
|
|
|
|
input.m_inStreamChild++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
throw std::invalid_argument( "SEXPR is not a string type!" );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator>> ( SEXPR_LIST& input, int64_t& lint )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR* child = input.GetChild( input.m_inStreamChild );
|
|
|
|
|
|
|
|
if( child->IsInteger() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
lint = child->GetLongInteger();
|
|
|
|
input.m_inStreamChild++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw std::invalid_argument("SEXPR is not a long integer type!");
|
|
|
|
}
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator>> ( SEXPR_LIST& input, float& fl )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR* child = input.GetChild( input.m_inStreamChild );
|
|
|
|
if( child->IsDouble() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
fl = child->GetFloat();
|
|
|
|
input.m_inStreamChild++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
throw std::invalid_argument( "SEXPR is not a float type!" );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator>> ( SEXPR_LIST& input, double& dbl )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR* child = input.GetChild( input.m_inStreamChild );
|
|
|
|
|
|
|
|
if( child->IsDouble() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
dbl = child->GetDouble();
|
|
|
|
input.m_inStreamChild++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
throw std::invalid_argument( "SEXPR is not a double type!" );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator>> ( SEXPR_LIST& input, const _IN_STRING is )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR* child = input.GetChild( input.m_inStreamChild );
|
|
|
|
|
|
|
|
if( is._Symbol )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( child->IsSymbol() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
is._String = child->GetSymbol();
|
|
|
|
input.m_inStreamChild++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
throw std::invalid_argument( "SEXPR is not a symbol type!" );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( child->IsString() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
is._String = child->GetString();
|
|
|
|
input.m_inStreamChild++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
throw std::invalid_argument( "SEXPR is not a string type!" );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR_LIST& operator<< ( SEXPR_LIST& list, SEXPR_LIST* list2 )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
list.AddChild( list2 );
|
2016-09-02 10:04:42 +00:00
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
size_t SEXPR_LIST::doScan( const SEXPR_SCAN_ARG *args, size_t num_args )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
2017-02-02 07:58:35 +00:00
|
|
|
|
|
|
|
for( i = 0; i < num_args; i++ )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
SEXPR* child = GetChild( i );
|
2016-09-02 10:04:42 +00:00
|
|
|
const SEXPR_SCAN_ARG& arg = args[i];
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( arg.type == SEXPR_SCAN_ARG::Type::DOUBLE )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
*arg.u.dbl_value = child->GetDouble();
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( arg.type == SEXPR_SCAN_ARG::Type::INT )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
*arg.u.dbl_value = child->GetInteger();
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( arg.type == SEXPR_SCAN_ARG::Type::STRING )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( child->IsSymbol() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
*arg.u.str_value = child->GetSymbol();
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( child->IsString() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
*arg.u.str_value = child->GetString();
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( arg.type == SEXPR_SCAN_ARG::Type::LONGINT )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
*arg.u.lint_value = child->GetLongInteger();
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( arg.type == SEXPR_SCAN_ARG::Type::SEXPR_STRING )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( arg.u.sexpr_str->_Symbol )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
arg.u.sexpr_str->_String = child->GetSymbol();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
arg.u.sexpr_str->_String = child->GetString();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( arg.type == SEXPR_SCAN_ARG::Type::STRING_COMP )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( child->IsSymbol() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( child->GetSymbol() != arg.str_value )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( child->IsString() )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( child->GetString() != arg.str_value )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
throw std::invalid_argument( "unsupported argument type, this shouldn't have happened" );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-19 16:15:36 +00:00
|
|
|
catch( const INVALID_TYPE_EXCEPTION& )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
void SEXPR_LIST::doAddChildren( const SEXPR_CHILDREN_ARG *args, size_t num_args )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
size_t i = 0;
|
2017-02-02 07:58:35 +00:00
|
|
|
|
|
|
|
for( i = 0; i < num_args; i++ )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
|
|
|
const SEXPR_CHILDREN_ARG& arg = args[i];
|
|
|
|
|
2017-02-02 07:58:35 +00:00
|
|
|
if( arg.type == SEXPR_CHILDREN_ARG::Type::DOUBLE )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
AddChild( new SEXPR_DOUBLE( arg.u.dbl_value ) );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( arg.type == SEXPR_CHILDREN_ARG::Type::INT )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
AddChild( new SEXPR_INTEGER( arg.u.int_value ) );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( arg.type == SEXPR_CHILDREN_ARG::Type::LONGINT )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
AddChild( new SEXPR_INTEGER( arg.u.lint_value ) );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( arg.type == SEXPR_CHILDREN_ARG::Type::STRING )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
AddChild( new SEXPR_STRING( arg.str_value ) );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( arg.type == SEXPR_CHILDREN_ARG::Type::SEXPR_ATOM )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
AddChild( arg.u.sexpr_ptr );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
2017-02-02 07:58:35 +00:00
|
|
|
else if( arg.type == SEXPR_CHILDREN_ARG::Type::SEXPR_STRING )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
if( arg.u.symbol )
|
2016-09-02 10:04:42 +00:00
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
AddChild( new SEXPR_SYMBOL( arg.str_value ) );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
AddChild( new SEXPR_STRING( arg.str_value ) );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-02-02 07:58:35 +00:00
|
|
|
throw std::invalid_argument( "unexpected argument type, this shouldn't have happened" );
|
2016-09-02 10:04:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|