Files
libreoffice/basic/source/comp/exprnode.cxx

465 lines
14 KiB
C++
Raw Normal View History

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
2000-09-18 15:18:56 +00:00
2000-09-18 15:18:56 +00:00
#include <math.h>
#include <rtl/math.hxx>
2000-09-18 15:18:56 +00:00
#include "sbcomp.hxx"
#include "expr.hxx"
SbiExprNode::SbiExprNode()
2008-11-20 14:05:36 +00:00
{
pLeft = NULL;
pRight = NULL;
pWithParent = NULL;
pGen = NULL;
2008-11-20 14:05:36 +00:00
eNodeType = SbxDUMMY;
eType = SbxVARIANT;
eTok = NIL;
bError = false;
2008-11-20 14:05:36 +00:00
}
2000-09-18 15:18:56 +00:00
SbiExprNode::SbiExprNode( SbiParser* p, SbiExprNode* l, SbiToken t, SbiExprNode* r )
{
BaseInit( p );
pLeft = l;
pRight = r;
eTok = t;
nVal = 0;
2010-11-27 18:43:39 +01:00
eType = SbxVARIANT; // Nodes are always Variant
2000-09-18 15:18:56 +00:00
eNodeType = SbxNODE;
}
SbiExprNode::SbiExprNode( SbiParser* p, double n, SbxDataType t )
{
BaseInit( p );
eType = t;
eNodeType = SbxNUMVAL;
nVal = n;
}
SbiExprNode::SbiExprNode( SbiParser* p, const OUString& rVal )
2000-09-18 15:18:56 +00:00
{
BaseInit( p );
eType = SbxSTRING;
eNodeType = SbxSTRVAL;
aStrVal = rVal;
2000-09-18 15:18:56 +00:00
}
SbiExprNode::SbiExprNode( SbiParser* p, const SbiSymDef& r, SbxDataType t, SbiExprList* l )
{
BaseInit( p );
eType = ( t == SbxVARIANT ) ? r.GetType() : t;
eNodeType = SbxVARVAL;
aVar.pDef = const_cast<SbiSymDef*>(&r);
2000-09-18 15:18:56 +00:00
aVar.pPar = l;
aVar.pvMorePar = NULL;
2000-09-18 15:18:56 +00:00
aVar.pNext= NULL;
}
// #120061 TypeOf
SbiExprNode::SbiExprNode( SbiParser* p, SbiExprNode* l, sal_uInt16 nId )
{
BaseInit( p );
pLeft = l;
eType = SbxBOOL;
eNodeType = SbxTYPEOF;
nTypeStrId = nId;
}
// new <type>
SbiExprNode::SbiExprNode( SbiParser* p, sal_uInt16 nId )
{
BaseInit( p );
eType = SbxOBJECT;
eNodeType = SbxNEW;
nTypeStrId = nId;
}
2010-11-27 18:43:39 +01:00
// From 1995-12-17, auxiliary function for Ctor for the uniform initialisation
2000-09-18 15:18:56 +00:00
void SbiExprNode::BaseInit( SbiParser* p )
{
pGen = &p->aGen;
eTok = NIL;
pLeft = NULL;
pRight = NULL;
2000-10-10 12:02:28 +00:00
pWithParent = NULL;
bError = false;
2000-09-18 15:18:56 +00:00
}
SbiExprNode::~SbiExprNode()
{
delete pLeft;
delete pRight;
if( IsVariable() )
{
delete aVar.pPar;
delete aVar.pNext;
SbiExprListVector* pvMorePar = aVar.pvMorePar;
if( pvMorePar )
{
SbiExprListVector::iterator it;
for( it = pvMorePar->begin() ; it != pvMorePar->end() ; ++it )
delete *it;
delete pvMorePar;
}
2000-09-18 15:18:56 +00:00
}
}
SbiSymDef* SbiExprNode::GetVar()
{
if( eNodeType == SbxVARVAL )
return aVar.pDef;
else
return NULL;
}
SbiSymDef* SbiExprNode::GetRealVar()
{
SbiExprNode* p = GetRealNode();
if( p )
return p->GetVar();
else
return NULL;
}
2010-11-27 18:43:39 +01:00
// From 1995-12-18
2000-09-18 15:18:56 +00:00
SbiExprNode* SbiExprNode::GetRealNode()
{
if( eNodeType == SbxVARVAL )
{
SbiExprNode* p = this;
while( p->aVar.pNext )
p = p->aVar.pNext;
return p;
}
else
return NULL;
}
2010-11-27 18:43:39 +01:00
// This method transform the type, if it fits into the Integer range
2000-09-18 15:18:56 +00:00
bool SbiExprNode::IsIntConst()
2000-09-18 15:18:56 +00:00
{
if( eNodeType == SbxNUMVAL )
{
if( eType >= SbxINTEGER && eType <= SbxDOUBLE )
{
double n;
if( nVal >= SbxMININT && nVal <= SbxMAXINT && modf( nVal, &n ) == 0 )
{
nVal = (double) (short) nVal;
eType = SbxINTEGER;
return true;
2000-09-18 15:18:56 +00:00
}
}
}
return false;
2000-09-18 15:18:56 +00:00
}
bool SbiExprNode::IsNumber()
2000-09-18 15:18:56 +00:00
{
return eNodeType == SbxNUMVAL;
2000-09-18 15:18:56 +00:00
}
bool SbiExprNode::IsVariable()
2000-09-18 15:18:56 +00:00
{
return eNodeType == SbxVARVAL;
2000-09-18 15:18:56 +00:00
}
bool SbiExprNode::IsLvalue()
2000-09-18 15:18:56 +00:00
{
return IsVariable();
}
2010-11-27 18:43:39 +01:00
// Identify of the depth of a tree
2000-09-18 15:18:56 +00:00
short SbiExprNode::GetDepth()
{
if( IsOperand() ) return 0;
else
{
short d1 = pLeft->GetDepth();
short d2 = pRight->GetDepth();
return( (d1 < d2 ) ? d2 : d1 ) + 1;
}
}
2010-11-27 18:43:39 +01:00
// Adjustment of a tree:
2000-09-18 15:18:56 +00:00
// 1. Constant Folding
2010-11-27 18:43:39 +01:00
// 2. Type-Adjustment
// 3. Conversion of the operans into Strings
// 4. Lifting of the composite- and error-bits
2000-09-18 15:18:56 +00:00
void SbiExprNode::Optimize()
{
FoldConstants();
CollectBits();
}
// Lifting of the error-bits
2000-09-18 15:18:56 +00:00
void SbiExprNode::CollectBits()
{
if( pLeft )
{
pLeft->CollectBits();
bError = bError || pLeft->bError;
2000-09-18 15:18:56 +00:00
}
if( pRight )
{
pRight->CollectBits();
bError = bError || pRight->bError;
2000-09-18 15:18:56 +00:00
}
}
2010-11-27 18:43:39 +01:00
// If a twig can be converted, True will be returned. In this case
// the result is in the left twig.
2000-09-18 15:18:56 +00:00
void SbiExprNode::FoldConstants()
{
if( IsOperand() || eTok == LIKE ) return;
if( pLeft )
pLeft->FoldConstants();
if (pLeft && pRight)
2000-09-18 15:18:56 +00:00
{
pRight->FoldConstants();
if( pLeft->IsConstant() && pRight->IsConstant()
&& pLeft->eNodeType == pRight->eNodeType )
{
CollectBits();
if( eTok == CAT )
2010-11-27 18:43:39 +01:00
// CAT affiliate also two numbers!
2000-09-18 15:18:56 +00:00
eType = SbxSTRING;
if( pLeft->eType == SbxSTRING )
2010-11-27 18:43:39 +01:00
// No Type Mismatch!
2000-09-18 15:18:56 +00:00
eType = SbxSTRING;
if( eType == SbxSTRING )
{
OUString rl( pLeft->GetString() );
OUString rr( pRight->GetString() );
delete pLeft; pLeft = NULL;
delete pRight; pRight = NULL;
if( eTok == PLUS || eTok == CAT )
2000-09-18 15:18:56 +00:00
{
eTok = CAT;
2010-11-27 18:43:39 +01:00
// Linking:
aStrVal = rl;
aStrVal += rr;
eType = SbxSTRING;
eNodeType = SbxSTRVAL;
}
else
{
eType = SbxDOUBLE;
eNodeType = SbxNUMVAL;
int eRes = rr.compareTo( rl );
switch( eTok )
{
case EQ:
nVal = ( eRes == 0 ) ? SbxTRUE : SbxFALSE;
break;
case NE:
nVal = ( eRes != 0 ) ? SbxTRUE : SbxFALSE;
break;
case LT:
nVal = ( eRes < 0 ) ? SbxTRUE : SbxFALSE;
break;
case GT:
nVal = ( eRes > 0 ) ? SbxTRUE : SbxFALSE;
break;
case LE:
nVal = ( eRes <= 0 ) ? SbxTRUE : SbxFALSE;
break;
case GE:
nVal = ( eRes >= 0 ) ? SbxTRUE : SbxFALSE;
break;
default:
pGen->GetParser()->Error( SbERR_CONVERSION );
bError = true;
break;
}
}
2000-09-18 15:18:56 +00:00
}
else
{
double nl = pLeft->nVal;
double nr = pRight->nVal;
long ll = 0, lr = 0;
long llMod = 0, lrMod = 0;
if( ( eTok >= AND && eTok <= IMP )
2000-09-18 15:18:56 +00:00
|| eTok == IDIV || eTok == MOD )
{
2010-11-27 18:43:39 +01:00
// Integer operations
bool err = false;
if( nl > SbxMAXLNG ) err = true, nl = SbxMAXLNG;
else if( nl < SbxMINLNG ) err = true, nl = SbxMINLNG;
if( nr > SbxMAXLNG ) err = true, nr = SbxMAXLNG;
else if( nr < SbxMINLNG ) err = true, nr = SbxMINLNG;
ll = static_cast<long>(nl); lr = static_cast<long>(nr);
llMod = static_cast<long>(nl);
lrMod = static_cast<long>(nr);
2000-09-18 15:18:56 +00:00
if( err )
{
pGen->GetParser()->Error( SbERR_MATH_OVERFLOW );
bError = true;
2000-09-18 15:18:56 +00:00
}
}
bool bBothInt = ( pLeft->eType < SbxSINGLE
2000-09-18 15:18:56 +00:00
&& pRight->eType < SbxSINGLE );
delete pLeft; pLeft = NULL;
delete pRight; pRight = NULL;
nVal = 0;
eType = SbxDOUBLE;
eNodeType = SbxNUMVAL;
bool bCheckType = false;
2000-09-18 15:18:56 +00:00
switch( eTok )
{
case EXPON:
nVal = pow( nl, nr ); break;
case MUL:
bCheckType = true;
2000-09-18 15:18:56 +00:00
nVal = nl * nr; break;
case DIV:
if( !nr )
{
pGen->GetParser()->Error( SbERR_ZERODIV ); nVal = HUGE_VAL;
bError = true;
2000-09-18 15:18:56 +00:00
} else nVal = nl / nr;
break;
case PLUS:
bCheckType = true;
2000-09-18 15:18:56 +00:00
nVal = nl + nr; break;
case MINUS:
bCheckType = true;
2000-09-18 15:18:56 +00:00
nVal = nl - nr; break;
case EQ:
nVal = ( nl == nr ) ? SbxTRUE : SbxFALSE;
eType = SbxINTEGER; break;
case NE:
nVal = ( nl != nr ) ? SbxTRUE : SbxFALSE;
eType = SbxINTEGER; break;
case LT:
nVal = ( nl < nr ) ? SbxTRUE : SbxFALSE;
eType = SbxINTEGER; break;
case GT:
nVal = ( nl > nr ) ? SbxTRUE : SbxFALSE;
eType = SbxINTEGER; break;
case LE:
nVal = ( nl <= nr ) ? SbxTRUE : SbxFALSE;
eType = SbxINTEGER; break;
case GE:
nVal = ( nl >= nr ) ? SbxTRUE : SbxFALSE;
eType = SbxINTEGER; break;
case IDIV:
if( !lr )
{
pGen->GetParser()->Error( SbERR_ZERODIV ); nVal = HUGE_VAL;
bError = true;
2000-09-18 15:18:56 +00:00
} else nVal = ll / lr;
eType = SbxLONG; break;
case MOD:
if( !lr )
{
pGen->GetParser()->Error( SbERR_ZERODIV ); nVal = HUGE_VAL;
bError = true;
} else nVal = llMod - lrMod * (llMod/lrMod);
2000-09-18 15:18:56 +00:00
eType = SbxLONG; break;
case AND:
nVal = (double) ( ll & lr ); eType = SbxLONG; break;
case OR:
nVal = (double) ( ll | lr ); eType = SbxLONG; break;
case XOR:
nVal = (double) ( ll ^ lr ); eType = SbxLONG; break;
case EQV:
nVal = (double) ( ~ll ^ lr ); eType = SbxLONG; break;
case IMP:
nVal = (double) ( ~ll | lr ); eType = SbxLONG; break;
default: break;
2000-09-18 15:18:56 +00:00
}
2001-08-22 09:01:44 +00:00
if( !::rtl::math::isFinite( nVal ) )
2001-08-22 09:01:44 +00:00
pGen->GetParser()->Error( SbERR_MATH_OVERFLOW );
2010-11-27 18:43:39 +01:00
// Recover the data type to kill rounding error
2000-09-18 15:18:56 +00:00
if( bCheckType && bBothInt
&& nVal >= SbxMINLNG && nVal <= SbxMAXLNG )
{
2010-11-27 18:43:39 +01:00
// Decimal place away
2000-09-18 15:18:56 +00:00
long n = (long) nVal;
nVal = n;
eType = ( n >= SbxMININT && n <= SbxMAXINT )
? SbxINTEGER : SbxLONG;
}
}
}
}
else if (pLeft && pLeft->IsNumber())
2000-09-18 15:18:56 +00:00
{
nVal = pLeft->nVal;
delete pLeft;
pLeft = NULL;
eType = SbxDOUBLE;
eNodeType = SbxNUMVAL;
switch( eTok )
{
case NEG:
nVal = -nVal; break;
case NOT: {
2010-11-27 18:43:39 +01:00
// Integer operation!
bool err = false;
if( nVal > SbxMAXLNG ) err = true, nVal = SbxMAXLNG;
else if( nVal < SbxMINLNG ) err = true, nVal = SbxMINLNG;
2000-09-18 15:18:56 +00:00
if( err )
{
pGen->GetParser()->Error( SbERR_MATH_OVERFLOW );
bError = true;
2000-09-18 15:18:56 +00:00
}
nVal = (double) ~((long) nVal);
eType = SbxLONG;
} break;
default: break;
2000-09-18 15:18:56 +00:00
}
}
if( eNodeType == SbxNUMVAL )
{
2010-11-27 18:43:39 +01:00
// Potentially convolve in INTEGER (because of better opcode)?
2000-09-18 15:18:56 +00:00
if( eType == SbxSINGLE || eType == SbxDOUBLE )
{
double x;
if( nVal >= SbxMINLNG && nVal <= SbxMAXLNG
&& !modf( nVal, &x ) )
eType = SbxLONG;
}
if( eType == SbxLONG && nVal >= SbxMININT && nVal <= SbxMAXINT )
eType = SbxINTEGER;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */