//=============================================================================
//
//   File : kvi_kvs_treenode_operation.cpp
//   Created on Thu 18 Dec 2003 22:36:00 by Szymon Stefanek
//
//   This file is part of the KVIrc IRC client distribution
//   Copyright (C) 2003 Szymon Stefanek <pragma at kvirc dot net>
//
//   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 opinion) 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, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#define __KVIRC__

#include "kvi_kvs_treenode_operation.h"
#include "kvi_kvs_treenode_data.h"
#include "kvi_kvs_runtimecontext.h"
#include "kvi_locale.h"

#include <tqregexp.h>

#include <math.h>


KviKvsTreeNodeOperation::KviKvsTreeNodeOperation(const TQChar * pLocation)
: KviKvsTreeNodeInstruction(pLocation)
{
	//m_pTargetData = 0; no need to set it
}

KviKvsTreeNodeOperation::~KviKvsTreeNodeOperation()
{
	delete m_pTargetData;
}

void KviKvsTreeNodeOperation::setTargetVariableReference(KviKvsTreeNodeData * r)
{
	m_pTargetData = r;
	m_pTargetData->setParent(this);
}

void KviKvsTreeNodeOperation::contextDescription(TQString &szBuffer)
{
	szBuffer = "Operation";
}

void KviKvsTreeNodeOperation::dump(const TQString &prefix)
{
	tqDebug("%s Operation",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
}





KviKvsTreeNodeOperationAssignment::KviKvsTreeNodeOperationAssignment(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
}

KviKvsTreeNodeOperationAssignment::~KviKvsTreeNodeOperationAssignment()
{
	delete m_pRightSide;
}

void KviKvsTreeNodeOperationAssignment::contextDescription(TQString &szBuffer)
{
	szBuffer = "Assignment";
}


void KviKvsTreeNodeOperationAssignment::dump(const TQString &prefix)
{
	tqDebug("%s OperationAssignment",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
}

bool KviKvsTreeNodeOperationAssignment::execute(KviKvsRunTimeContext * c)
{
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;

	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;

	target->result()->takeFrom(v);
	delete target;
	return true;
}






KviKvsTreeNodeOperationDecrement::KviKvsTreeNodeOperationDecrement(const TQChar * pLocation)
: KviKvsTreeNodeOperation(pLocation)
{
}

KviKvsTreeNodeOperationDecrement::~KviKvsTreeNodeOperationDecrement()
{
}

void KviKvsTreeNodeOperationDecrement::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"--\"";
#endif
}


void KviKvsTreeNodeOperationDecrement::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationDecrement",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationDecrement::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsRWEvaluationResult * v = m_pTargetData->evaluateReadWrite(c);
	if(!v)return false;

	kvs_int_t iVal;
	if(v->result()->asInteger(iVal))
	{
		v->result()->setInteger(iVal - 1);
		delete v;
        v = 0;
		return true;
	}

	kvs_real_t dVal;
	if(v->result()->asReal(dVal))
	{
		v->result()->setReal(dVal - 1.0);
		delete v;
        v = 0;
		return true;
	}

	c->error(this,__tr2qs("The target variable didn't evaluate to an integer or real value"));
	delete v;
#endif
	return false;
}







KviKvsTreeNodeOperationIncrement::KviKvsTreeNodeOperationIncrement(const TQChar * pLocation)
: KviKvsTreeNodeOperation(pLocation)
{
}

KviKvsTreeNodeOperationIncrement::~KviKvsTreeNodeOperationIncrement()
{
}

void KviKvsTreeNodeOperationIncrement::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"++\"";
#endif
}


void KviKvsTreeNodeOperationIncrement::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationIncrement",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationIncrement::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsRWEvaluationResult * v = m_pTargetData->evaluateReadWrite(c);
	if(!v)return false;

	kvs_int_t iVal;
	if(v->result()->asInteger(iVal))
	{
		v->result()->setInteger(iVal + 1);
		delete v;
        v = 0;
		return true;
	}

	kvs_real_t dVal;
	if(v->result()->asReal(dVal))
	{
		v->result()->setReal(dVal + 1.0);
		delete v;
        v = 0;
		return true;
	}
	c->error(this,__tr2qs("The target variable didn't evaluate to an integer or real value"));
	delete v;
#endif
	return false;
}




KviKvsTreeNodeOperationSelfAnd::KviKvsTreeNodeOperationSelfAnd(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationSelfAnd::~KviKvsTreeNodeOperationSelfAnd()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationSelfAnd::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"&=\"";
#endif
}


void KviKvsTreeNodeOperationSelfAnd::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationSelfAnd",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationSelfAnd::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;

	kvs_int_t iRVal;

	if(!v.asInteger(iRVal))
	{
		c->error(this,__tr2qs("The right side of operator '&=' didn't evaluate to an integer"));
		return false;
	}

	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	kvs_int_t iLVal;
	
	if(!target->result()->asInteger(iLVal))
	{
		c->error(this,__tr2qs("The left side of operator '&=' didn't evaluate to an integer"));
		delete target;
        target = 0;
		return false;
	}
	
	target->result()->setInteger(iLVal & iRVal);
	delete target;
#endif
	return true;
}



KviKvsTreeNodeOperationSelfDivision::KviKvsTreeNodeOperationSelfDivision(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationSelfDivision::~KviKvsTreeNodeOperationSelfDivision()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationSelfDivision::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"/=\"";
#endif
}


void KviKvsTreeNodeOperationSelfDivision::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationSelfDivision",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationSelfDivision::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;

	KviKvsNumber rnum;

	if(!v.asNumber(rnum))
	{
		c->error(this,__tr2qs("The right side of operator '/=' didn't evaluate to a number"));
		return false;
	}
	
	KviKvsNumber lnum;

	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	if(!target->result()->asNumber(lnum))
	{
		c->error(this,__tr2qs("The left side of operator '/=' didn't evaluate to a number"));
		delete target;
        target = 0;
		return false;
	}

	if(rnum.isInteger())
	{
		if(rnum.integer() == 0)
		{
			c->error(this,__tr2qs("Division by zero"));
			delete target;
            target = 0;
			return false;
		}
		if(lnum.isInteger())
			target->result()->setInteger(lnum.integer() / rnum.integer());
		else
			target->result()->setReal(lnum.real() / (kvs_real_t)(rnum.integer()));
	} else {
		if(rnum.real() == 0.0)
		{
			c->error(this,__tr2qs("Division by zero"));
			delete target;
            target = 0;
			return false;
		}
		if(lnum.isInteger())
			target->result()->setReal(((kvs_real_t)(lnum.integer())) / rnum.real());
		else
			target->result()->setReal(lnum.real() / rnum.real());
	}

	delete target;
#endif
	return true;
}

KviKvsTreeNodeOperationSelfModulus::KviKvsTreeNodeOperationSelfModulus(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationSelfModulus::~KviKvsTreeNodeOperationSelfModulus()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationSelfModulus::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"Self Modulo\"";
#endif
}


void KviKvsTreeNodeOperationSelfModulus::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationSelfModulus",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationSelfModulus::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;
	KviKvsNumber rnum;

	if(!v.asNumber(rnum))
	{
		c->error(this,__tr2qs("The right side of operator '%=' didn't evaluate to a number"));
		return false;
	}
	
	KviKvsNumber lnum;

	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	if(!target->result()->asNumber(lnum))
	{
		c->error(this,__tr2qs("The left side of operator '%=' didn't evaluate to a number"));
		delete target;
        target = 0;
		return false;
	}

	if(rnum.isInteger())
	{
		if(rnum.integer() == 0)
		{
			c->error(this,__tr2qs("Division by zero"));
			delete target;
            target = 0;
			return false;
		}
		if(lnum.isInteger())
			target->result()->setInteger(lnum.integer() % rnum.integer());
		else
			target->result()->setReal(fmod(lnum.real(),(kvs_real_t)(rnum.integer())));
	} else {
		if(rnum.real() == 0.0)
		{
			c->error(this,__tr2qs("Division by zero"));
			delete target;
            target = 0;
			return false;
		}
		if(lnum.isInteger())
			target->result()->setReal(fmod(((kvs_real_t)(lnum.integer())),rnum.real()));
		else
			target->result()->setReal(fmod(lnum.real(),rnum.real()));
	}
#endif
	return true;
}





KviKvsTreeNodeOperationSelfMultiplication::KviKvsTreeNodeOperationSelfMultiplication(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationSelfMultiplication::~KviKvsTreeNodeOperationSelfMultiplication()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationSelfMultiplication::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"*=\"";
#endif
}


void KviKvsTreeNodeOperationSelfMultiplication::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationSelfMultiplication",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationSelfMultiplication::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;
	KviKvsNumber rnum;

	if(!v.asNumber(rnum))
	{
		c->error(this,__tr2qs("The right side of operator '*=' didn't evaluate to a number"));
		return false;
	}
	
	KviKvsNumber lnum;

	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	if(!target->result()->asNumber(lnum))
	{
		c->error(this,__tr2qs("The left side of operator '*=' didn't evaluate to a number"));
		delete target;
		return false;
	}

	if(rnum.isInteger())
	{
		if(lnum.isInteger())
			target->result()->setInteger(lnum.integer() * rnum.integer());
		else
			target->result()->setReal(lnum.real() * (kvs_real_t)(rnum.integer()));
	} else {
		if(lnum.isInteger())
			target->result()->setReal(((kvs_real_t)(lnum.integer())) * rnum.real());
		else
			target->result()->setReal(lnum.real() * rnum.real());
	}
#endif
	return true;
}




KviKvsTreeNodeOperationSelfOr::KviKvsTreeNodeOperationSelfOr(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationSelfOr::~KviKvsTreeNodeOperationSelfOr()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationSelfOr::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"|=\"";
#endif
}


void KviKvsTreeNodeOperationSelfOr::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationSelfOr",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationSelfOr::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;

	kvs_int_t iRVal;
	if(!v.asInteger(iRVal))
	{
		c->error(this,__tr2qs("The right side of operator '|=' didn't evaluate to an integer"));
		return false;
	}
	
	kvs_int_t iLVal;

	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	if(!target->result()->asInteger(iLVal))
	{
		c->error(this,__tr2qs("The left side of operator '|=' didn't evaluate to an integer"));
		delete target;
        target = 0;
		return false;
	}
	
	target->result()->setInteger(iLVal | iRVal);
	delete target;
#endif
	return true;
}




KviKvsTreeNodeOperationSelfShl::KviKvsTreeNodeOperationSelfShl(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationSelfShl::~KviKvsTreeNodeOperationSelfShl()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationSelfShl::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"<<=\"";
#endif
}


void KviKvsTreeNodeOperationSelfShl::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationSelfShl",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationSelfShl::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;

	kvs_int_t iRVal;

	if(!v.asInteger(iRVal))
	{
		c->error(this,__tr2qs("The right side of operator '<<=' didn't evaluate to an integer"));
		return false;
	}
	
	kvs_int_t iLVal;

	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	if(!target->result()->asInteger(iLVal))
	{
		c->error(this,__tr2qs("The left side of operator '<<=' didn't evaluate to an integer"));
		delete target;
        target = 0;
		return false;
	}
	
	target->result()->setInteger(iLVal << iRVal);
	delete target;
#endif
	return true;
}





KviKvsTreeNodeOperationSelfShr::KviKvsTreeNodeOperationSelfShr(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationSelfShr::~KviKvsTreeNodeOperationSelfShr()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationSelfShr::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \">>=\"";
#endif
}


void KviKvsTreeNodeOperationSelfShr::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationSelfShr",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationSelfShr::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;

	kvs_int_t iRVal;

	if(!v.asInteger(iRVal))
	{
		c->error(this,__tr2qs("The right side of operator '>>=' didn't evaluate to an integer"));
		return false;
	}
	
	kvs_int_t iLVal;

	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	if(!target->result()->asInteger(iLVal))
	{
		c->error(this,__tr2qs("The left side of operator '>>=' didn't evaluate to an integer"));
		delete target;
        target = 0;
		return false;
	}
	
	target->result()->setInteger(iLVal >> iRVal);
	delete target;
#endif
	return true;
}











KviKvsTreeNodeOperationSelfSubtraction::KviKvsTreeNodeOperationSelfSubtraction(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationSelfSubtraction::~KviKvsTreeNodeOperationSelfSubtraction()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationSelfSubtraction::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"-=\"";
#endif
}


void KviKvsTreeNodeOperationSelfSubtraction::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationSelfSubtraction",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationSelfSubtraction::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;
	KviKvsNumber rnum;

	if(!v.asNumber(rnum))
	{
		c->error(this,__tr2qs("The right side of operator '-=' didn't evaluate to a number"));
		return false;
	}
	
	KviKvsNumber lnum;

	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	if(!target->result()->asNumber(lnum))
	{
		c->error(this,__tr2qs("The left side of operator '-=' didn't evaluate to a number"));
		delete target;
        target = 0;
		return false;
	}

	if(rnum.isInteger())
	{
		if(lnum.isInteger())
			target->result()->setInteger(lnum.integer() - rnum.integer());
		else
			target->result()->setReal(lnum.real() - (kvs_real_t)(rnum.integer()));
	} else {
		if(lnum.isInteger())
			target->result()->setReal(((kvs_real_t)(lnum.integer())) - rnum.real());
		else
			target->result()->setReal(lnum.real() - rnum.real());
	}

	delete target;
#endif
	return true;
}





KviKvsTreeNodeOperationSelfSum::KviKvsTreeNodeOperationSelfSum(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationSelfSum::~KviKvsTreeNodeOperationSelfSum()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationSelfSum::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"+=\"";
#endif
}


void KviKvsTreeNodeOperationSelfSum::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationSelfSum",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationSelfSum::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;
	KviKvsNumber rnum;

	if(!v.asNumber(rnum))
	{
		c->error(this,__tr2qs("The right side of operator '+=' didn't evaluate to a number"));
		return false;
	}
	
	KviKvsNumber lnum;

	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	if(!target->result()->asNumber(lnum))
	{
		c->error(this,__tr2qs("The left side of operator '+=' didn't evaluate to a number"));
		delete target;
        target = 0;
		return false;
	}

	if(rnum.isInteger())
	{
		if(lnum.isInteger())
			target->result()->setInteger(lnum.integer() + rnum.integer());
		else
			target->result()->setReal(lnum.real() + (kvs_real_t)(rnum.integer()));
	} else {
		if(lnum.isInteger())
			target->result()->setReal(((kvs_real_t)(lnum.integer())) + rnum.real());
		else
			target->result()->setReal(lnum.real() + rnum.real());
	}

	delete target;
#endif
	return true;
}







KviKvsTreeNodeOperationSelfXor::KviKvsTreeNodeOperationSelfXor(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationSelfXor::~KviKvsTreeNodeOperationSelfXor()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationSelfXor::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"^=\"";
#endif
}


void KviKvsTreeNodeOperationSelfXor::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationSelfXor",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationSelfXor::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;

	kvs_int_t iRVal;

	if(!v.asInteger(iRVal))
	{
		c->error(this,__tr2qs("The right side of operator '^=' didn't evaluate to an integer"));
		return false;
	}
	
	kvs_int_t iLVal;

	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	if(!target->result()->asInteger(iLVal))
	{
		c->error(this,__tr2qs("The left side of operator '^=' didn't evaluate to an integer"));
		delete target;
        target = 0;
		return false;
	}
	
	target->result()->setInteger(iLVal ^ iRVal);
	delete target;
#endif
	return true;
}







KviKvsTreeNodeOperationStringAppend::KviKvsTreeNodeOperationStringAppend(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
}

KviKvsTreeNodeOperationStringAppend::~KviKvsTreeNodeOperationStringAppend()
{
	delete m_pRightSide;
}

void KviKvsTreeNodeOperationStringAppend::contextDescription(TQString &szBuffer)
{
	szBuffer = "Operator \"<<\"";
}


void KviKvsTreeNodeOperationStringAppend::dump(const TQString &prefix)
{
	tqDebug("%s OperationStringAppend",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
}

bool KviKvsTreeNodeOperationStringAppend::execute(KviKvsRunTimeContext * c)
{
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;
	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	TQString sz1;
	target->result()->asString(sz1);
	v.appendAsString(sz1);
	target->result()->setString(sz1);
	delete target;
	return true;
}






KviKvsTreeNodeOperationArrayAppend::KviKvsTreeNodeOperationArrayAppend(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
}

KviKvsTreeNodeOperationArrayAppend::~KviKvsTreeNodeOperationArrayAppend()
{
	delete m_pRightSide;
}

void KviKvsTreeNodeOperationArrayAppend::contextDescription(TQString &szBuffer)
{
	szBuffer = "Operator \"<+\"";
}


void KviKvsTreeNodeOperationArrayAppend::dump(const TQString &prefix)
{
	tqDebug("%s OperationArrayAppend",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
}

bool KviKvsTreeNodeOperationArrayAppend::execute(KviKvsRunTimeContext * c)
{
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;
	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;

	target->result()->convertToArray();
	KviKvsArray * a = target->result()->array();

	switch(v.type())
	{
		case KviKvsVariantData::Nothing:
			// do nothing
		break;
		case KviKvsVariantData::Array:
		{
			KviKvsArray * s = v.array();
			unsigned int uIdx = 0;
			unsigned int uSize = s->size();
			while(uIdx < uSize)
			{
				KviKvsVariant * pInternal = s->at(uIdx);
				if(pInternal)
					a->set(a->size(),new KviKvsVariant(*pInternal));
				// else
				//	leave an empty entry
				uIdx++;
			}
		}
		break;
		case KviKvsVariantData::Hash:
		{
			KviKvsHashIterator it(*(v.hash()->dict()));
			while(KviKvsVariant * pInternal = it.current())
			{
				a->set(a->size(),new KviKvsVariant(*pInternal));
				++it;
			}
		}
		break;
		default:
			a->set(a->size(),new KviKvsVariant(v));
		break;
	}
	delete target;

	return true;
}







KviKvsTreeNodeOperationStringAppendWithComma::KviKvsTreeNodeOperationStringAppendWithComma(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationStringAppendWithComma::~KviKvsTreeNodeOperationStringAppendWithComma()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationStringAppendWithComma::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"<,\"";
#endif
}


void KviKvsTreeNodeOperationStringAppendWithComma::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationStringAppendWithComma",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationStringAppendWithComma::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;
	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	TQString sz1;
	target->result()->asString(sz1);
	if(sz1.isEmpty())
	{
		v.asString(sz1);
	} else {
		TQString sz2;
		v.asString(sz2);
		sz1 += TQChar(',');
		sz1 += sz2;
	}
	target->result()->setString(sz1);
	delete target;
#endif
	return true;
}






KviKvsTreeNodeOperationStringAppendWithSpace::KviKvsTreeNodeOperationStringAppendWithSpace(const TQChar * pLocation,KviKvsTreeNodeData * pRightSide)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pRightSide = pRightSide;
	m_pRightSide->setParent(this);
#endif
}

KviKvsTreeNodeOperationStringAppendWithSpace::~KviKvsTreeNodeOperationStringAppendWithSpace()
{
#ifdef COMPILE_NEW_KVS
	delete m_pRightSide;
#endif
}

void KviKvsTreeNodeOperationStringAppendWithSpace::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Operator \"<+\"";
#endif
}


void KviKvsTreeNodeOperationStringAppendWithSpace::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationStringAppendWithSpace",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pTargetData->dump(tmp);
	m_pRightSide->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationStringAppendWithSpace::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant v;
	if(!m_pRightSide->evaluateReadOnly(c,&v))return false;
	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;
	
	TQString sz1;
	target->result()->asString(sz1);
	if(sz1.isEmpty())
	{
		v.asString(sz1);
	} else {
		TQString sz2;
		v.asString(sz2);
		sz1 += TQChar(' ');
		sz1 += sz2;
	}
	target->result()->setString(sz1);
	delete target;
#endif
	return true;
}









KviKvsTreeNodeOperationStringTransliteration::KviKvsTreeNodeOperationStringTransliteration(const TQChar * pLocation,KviKvsTreeNodeData * pLeft,KviKvsTreeNodeData * pRight,KviKvsTreeNodeData * pFlags)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pLeft = pLeft;
	m_pLeft->setParent(this);
	m_pRight = pRight;
	m_pRight->setParent(this);
	m_pFlags = pFlags;
	m_pFlags->setParent(this);
#endif
}

KviKvsTreeNodeOperationStringTransliteration::~KviKvsTreeNodeOperationStringTransliteration()
{
#ifdef COMPILE_NEW_KVS
	delete m_pLeft;
	delete m_pRight;
	delete m_pFlags;
#endif
}


void KviKvsTreeNodeOperationStringTransliteration::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Binding Operator tr///";
#endif
}


void KviKvsTreeNodeOperationStringTransliteration::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationStringTransliteration",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pLeft->dump(tmp);
	m_pRight->dump(tmp);
	m_pFlags->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationStringTransliteration::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant vl;
	if(!m_pLeft->evaluateReadOnly(c,&vl))return false;
	KviKvsVariant vr;
	if(!m_pRight->evaluateReadOnly(c,&vr))return false;

	// for now we don't use the flags at all here...
	//KviKvsVariant vf;
	//if(!m_pFlags->evaluateReadOnly(c,&vf))return false;
	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;

	TQString szL,szR,szF;

	vl.asString(szL);
	vr.asString(szR);
	//vf.asString(szF);

	TQString sz1;
	target->result()->asString(sz1);
	KviTQString::transliterate(sz1,szL,szR);
	target->result()->setString(sz1);
	delete target;
#endif
	return true;
}










KviKvsTreeNodeOperationStringSubstitution::KviKvsTreeNodeOperationStringSubstitution(const TQChar * pLocation,KviKvsTreeNodeData * pLeft,KviKvsTreeNodeData * pRight,KviKvsTreeNodeData * pFlags)
: KviKvsTreeNodeOperation(pLocation)
{
#ifdef COMPILE_NEW_KVS
	m_pLeft = pLeft;
	m_pLeft->setParent(this);
	m_pRight = pRight;
	m_pRight->setParent(this);
	m_pFlags = pFlags;
	m_pFlags->setParent(this);
#endif
}

KviKvsTreeNodeOperationStringSubstitution::~KviKvsTreeNodeOperationStringSubstitution()
{
#ifdef COMPILE_NEW_KVS
	delete m_pLeft;
	delete m_pRight;
	delete m_pFlags;
#endif
}


void KviKvsTreeNodeOperationStringSubstitution::contextDescription(TQString &szBuffer)
{
#ifdef COMPILE_NEW_KVS
	szBuffer = "Binding Operator s///";
#endif
}


void KviKvsTreeNodeOperationStringSubstitution::dump(const TQString &prefix)
{
#ifdef COMPILE_NEW_KVS
	tqDebug("%s OperationStringSubstitution",prefix.local8Bit().data());
	TQString tmp = prefix;
	tmp.append("  ");
	m_pLeft->dump(tmp);
	m_pRight->dump(tmp);
	m_pFlags->dump(tmp);
#endif
}

bool KviKvsTreeNodeOperationStringSubstitution::execute(KviKvsRunTimeContext * c)
{
#ifdef COMPILE_NEW_KVS
	KviKvsVariant vl;
	if(!m_pLeft->evaluateReadOnly(c,&vl))return false;
	KviKvsVariant vr;
	if(!m_pRight->evaluateReadOnly(c,&vr))return false;
	KviKvsVariant vf;
	if(!m_pFlags->evaluateReadOnly(c,&vf))return false;
	KviKvsRWEvaluationResult * target = m_pTargetData->evaluateReadWrite(c);
	if(!target)return false;

	TQString szL,szR,szF;

	vl.asString(szL);
	vr.asString(szR);
	vf.asString(szF);

	TQString str;
	target->result()->asString(str);

	bool bGlobal = szF.find('g',false) != -1;

	TQRegExp re(szL,szF.find('i',false) == -1,szF.find('w',false) != -1);
	re.setMinimal(szF.find('m',false) != -1); // greedy or minimal ?

	int idx = 0;

	while((!str.isEmpty()) && (idx != -1))
	{
		int len;
		idx = re.search(str,idx);
		if(idx != -1)
		{
			len = re.matchedLength();
			if(len == 0)
			{
				c->warning(this,__tr2qs("The regular expression matched a zero-length substring: this is not valid in the s/// operator, skipping"));
				goto end_of_this_stuff;
			}
			str.remove(idx,len);

			TQString szReplaced;
			const TQChar * p2 = KviTQString::nullTerminatedArray(szR);
			const TQChar * begin = p2;
			while(p2->unicode())
			{
				if(p2->unicode() == '\\')
				{
					p2++;
					if(p2->unicode() >= '0' && p2->unicode() <= '9')
					{
						szReplaced.append(TQString(begin,(p2 - begin) - 1));
						szReplaced.append(re.cap(p2->unicode() - '0'));
						p2++;
						begin = p2;
					} else if(p2->unicode())p2++;
				} else p2++;
			}
			if(begin != p2)szReplaced.append(TQString(begin,p2 - begin));
			str.insert(idx,szReplaced);
			idx += szReplaced.length();

			if(!bGlobal)goto end_of_this_stuff;
		}
	}
end_of_this_stuff:
	target->result()->setString(str);
	delete target;
#endif
	return true;
}

