xref: /trunk/main/basic/source/inc/expr.hxx (revision 234bd5c559aaf7abbd02d045859137b774cd8b34)
1*234bd5c5SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*234bd5c5SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*234bd5c5SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*234bd5c5SAndrew Rist  * distributed with this work for additional information
6*234bd5c5SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*234bd5c5SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*234bd5c5SAndrew Rist  * "License"); you may not use this file except in compliance
9*234bd5c5SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*234bd5c5SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*234bd5c5SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*234bd5c5SAndrew Rist  * software distributed under the License is distributed on an
15*234bd5c5SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*234bd5c5SAndrew Rist  * KIND, either express or implied.  See the License for the
17*234bd5c5SAndrew Rist  * specific language governing permissions and limitations
18*234bd5c5SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*234bd5c5SAndrew Rist  *************************************************************/
21*234bd5c5SAndrew Rist 
22*234bd5c5SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _EXPR_HXX
25cdf0e10cSrcweir #define _EXPR_HXX
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include "opcodes.hxx"
28cdf0e10cSrcweir #include "token.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir class SbiExprNode;
31cdf0e10cSrcweir class SbiExpression;
32cdf0e10cSrcweir class SbiExprList;
33cdf0e10cSrcweir class SbiDimList;
34cdf0e10cSrcweir class SbiParameters;
35cdf0e10cSrcweir class SbiParser;
36cdf0e10cSrcweir class SbiCodeGen;
37cdf0e10cSrcweir class SbiSymDef;
38cdf0e10cSrcweir class SbiProcDef;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <vector>
42cdf0e10cSrcweir typedef ::std::vector<SbiExprList*> SbiExprListVector;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir struct SbVar {                      // Variablen-Element:
45cdf0e10cSrcweir     SbiExprNode*        pNext;      // Weiteres Element (bei Strukturen)
46cdf0e10cSrcweir     SbiSymDef*          pDef;       // Symboldefinition
47cdf0e10cSrcweir     SbiExprList*        pPar;       // optionale Parameter (wird geloescht)
48cdf0e10cSrcweir     SbiExprListVector*  pvMorePar;  // Array of arrays foo(pPar)(avMorePar[0])(avMorePar[1])...
49cdf0e10cSrcweir };
50cdf0e10cSrcweir 
51cdf0e10cSrcweir struct KeywordSymbolInfo
52cdf0e10cSrcweir {
53cdf0e10cSrcweir     String          m_aKeywordSymbol;
54cdf0e10cSrcweir     SbxDataType     m_eSbxDataType;
55cdf0e10cSrcweir     SbiToken        m_eTok;
56cdf0e10cSrcweir };
57cdf0e10cSrcweir 
58cdf0e10cSrcweir enum SbiExprType {                  // Expression-Typen:
59cdf0e10cSrcweir     SbSTDEXPR,                      // normaler Ausdruck
60cdf0e10cSrcweir     SbLVALUE,                       // beliebiger lValue
61cdf0e10cSrcweir     SbSYMBOL,                       // beliebiges zusammengesetztes Symbol
62cdf0e10cSrcweir     SbOPERAND                       // Variable/Funktion
63cdf0e10cSrcweir };
64cdf0e10cSrcweir 
65cdf0e10cSrcweir enum SbiExprMode {                  // Expression context:
66cdf0e10cSrcweir     EXPRMODE_STANDARD,              // default
67cdf0e10cSrcweir     EXPRMODE_STANDALONE,            // a param1, param2 OR a( param1, param2 ) = 42
68cdf0e10cSrcweir     EXPRMODE_LPAREN_PENDING,        // start of parameter list with bracket, special handling
69cdf0e10cSrcweir     EXPRMODE_LPAREN_NOT_NEEDED,     // pending LPAREN has not been used
70cdf0e10cSrcweir     EXPRMODE_ARRAY_OR_OBJECT,       // '=' or '(' or '.' found after ')' on ParenLevel 0, stopping
71cdf0e10cSrcweir                                     // expression, assuming array syntax a(...)[(...)] = ?
72cdf0e10cSrcweir                                     // or a(...).b(...)
73cdf0e10cSrcweir     EXPRMODE_EMPTY_PAREN            // It turned out that the paren don't contain anything: a()
74cdf0e10cSrcweir };
75cdf0e10cSrcweir 
76cdf0e10cSrcweir enum SbiNodeType {
77cdf0e10cSrcweir     SbxNUMVAL,                      // nVal = Wert
78cdf0e10cSrcweir     SbxSTRVAL,                      // aStrVal = Wert, before #i59791/#i45570: nStringId = Wert
79cdf0e10cSrcweir     SbxVARVAL,                      // aVar = Wert
80cdf0e10cSrcweir     SbxTYPEOF,                      // TypeOf ObjExpr Is Type
81cdf0e10cSrcweir     SbxNODE,                        // Node
82cdf0e10cSrcweir     SbxNEW,                         // new <type> expression
83cdf0e10cSrcweir     SbxDUMMY
84cdf0e10cSrcweir };
85cdf0e10cSrcweir 
86cdf0e10cSrcweir enum RecursiveMode
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     UNDEFINED,
89cdf0e10cSrcweir     FORCE_CALL,
90cdf0e10cSrcweir     PREVENT_CALL
91cdf0e10cSrcweir };
92cdf0e10cSrcweir 
93cdf0e10cSrcweir class SbiExprNode {                  // Operatoren (und Operanden)
94cdf0e10cSrcweir     friend class SbiExpression;
95cdf0e10cSrcweir     friend class SbiConstExpression;
96cdf0e10cSrcweir     union {
97cdf0e10cSrcweir         sal_uInt16 nTypeStrId;          // gepoolter String-ID, #i59791/#i45570 Now only for TypeOf
98cdf0e10cSrcweir         double nVal;                // numerischer Wert
99cdf0e10cSrcweir         SbVar  aVar;                // oder Variable
100cdf0e10cSrcweir     };
101cdf0e10cSrcweir     String aStrVal;                 // #i59791/#i45570 Store string directly
102cdf0e10cSrcweir     SbiExprNode* pLeft;             // linker Zweig
103cdf0e10cSrcweir     SbiExprNode* pRight;            // rechter Zweig (NULL bei unaeren Ops)
104cdf0e10cSrcweir     SbiExprNode* pWithParent;       // Knoten, dessen Member this per with ist
105cdf0e10cSrcweir     SbiCodeGen*  pGen;              // Code-Generator
106cdf0e10cSrcweir     SbiNodeType  eNodeType;         // Art des Nodes
107cdf0e10cSrcweir     SbxDataType eType;              // aktueller Datentyp
108cdf0e10cSrcweir     SbiToken     eTok;              // Token des Operators
109cdf0e10cSrcweir     sal_Bool  bComposite;               // sal_True: Zusammengesetzter Ausdruck
110cdf0e10cSrcweir     sal_Bool  bError;                   // sal_True: Fehlerhaft
111cdf0e10cSrcweir     void  FoldConstants();          // Constant Folding durchfuehren
112cdf0e10cSrcweir     void  CollectBits();            // Umwandeln von Zahlen in Strings
113cdf0e10cSrcweir     sal_Bool  IsOperand()               // sal_True, wenn Operand
114cdf0e10cSrcweir         { return sal_Bool( eNodeType != SbxNODE && eNodeType != SbxTYPEOF && eNodeType != SbxNEW ); }
115cdf0e10cSrcweir     sal_Bool  IsTypeOf()
116cdf0e10cSrcweir         { return sal_Bool( eNodeType == SbxTYPEOF ); }
117cdf0e10cSrcweir     sal_Bool  IsNew()
118cdf0e10cSrcweir         { return sal_Bool( eNodeType == SbxNEW ); }
119cdf0e10cSrcweir     sal_Bool  IsNumber();               // sal_True bei Zahlen
120cdf0e10cSrcweir     sal_Bool  IsString();               // sal_True bei Strings
121cdf0e10cSrcweir     sal_Bool  IsLvalue();               // sal_True, falls als Lvalue verwendbar
122cdf0e10cSrcweir     void  GenElement( SbiOpcode );  // Element
123cdf0e10cSrcweir     void  BaseInit( SbiParser* p ); // Hilfsfunktion fuer Ctor, AB 17.12.95
124cdf0e10cSrcweir public:
125cdf0e10cSrcweir     SbiExprNode( void );
126cdf0e10cSrcweir     SbiExprNode( SbiParser*, double, SbxDataType );
127cdf0e10cSrcweir     SbiExprNode( SbiParser*, const String& );
128cdf0e10cSrcweir     SbiExprNode( SbiParser*, const SbiSymDef&, SbxDataType, SbiExprList* = NULL );
129cdf0e10cSrcweir     SbiExprNode( SbiParser*, SbiExprNode*, SbiToken, SbiExprNode* );
130cdf0e10cSrcweir     SbiExprNode( SbiParser*, SbiExprNode*, sal_uInt16 );    // #120061 TypeOf
131cdf0e10cSrcweir     SbiExprNode( SbiParser*, sal_uInt16 );                  // new <type>
132cdf0e10cSrcweir     virtual ~SbiExprNode();
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     sal_Bool IsValid()                  { return sal_Bool( !bError ); }
135cdf0e10cSrcweir     sal_Bool IsConstant()               // sal_True bei konstantem Operanden
136cdf0e10cSrcweir         { return sal_Bool( eNodeType == SbxSTRVAL || eNodeType == SbxNUMVAL ); }
137cdf0e10cSrcweir     sal_Bool IsIntConst();              // sal_True bei Integer-Konstanten
138cdf0e10cSrcweir     sal_Bool IsVariable();              // sal_True, wenn Variable
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     SbiExprNode* GetWithParent()            { return pWithParent; }
141cdf0e10cSrcweir     void SetWithParent( SbiExprNode* p )    { pWithParent = p; }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     SbxDataType GetType()           { return eType; }
144cdf0e10cSrcweir     void SetType( SbxDataType eTp ) { eType = eTp; }
145cdf0e10cSrcweir     SbiNodeType GetNodeType()       { return eNodeType; }
146cdf0e10cSrcweir     SbiSymDef* GetVar();            // Variable (falls vorhanden)
147cdf0e10cSrcweir     SbiSymDef* GetRealVar();        // letzte Variable in x.y.z
148cdf0e10cSrcweir     SbiExprNode* GetRealNode();     // letzter Knoten in x.y.z
149cdf0e10cSrcweir     short GetDepth();               // Tiefe eines Baumes berechnen
150cdf0e10cSrcweir     const String& GetString()       { return aStrVal; }
151cdf0e10cSrcweir     short GetNumber()               { return (short)nVal; }
152cdf0e10cSrcweir     SbiExprList* GetParameters()    { return aVar.pPar; }
153cdf0e10cSrcweir     SbiExprListVector* GetMoreParameters()  { return aVar.pvMorePar; }
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     void Optimize();                // Baumabgleich
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     void Gen( RecursiveMode eRecMode = UNDEFINED ); // Ausgabe eines Nodes
158cdf0e10cSrcweir };
159cdf0e10cSrcweir 
160cdf0e10cSrcweir class SbiExpression {                // der Ausdruck:
161cdf0e10cSrcweir     friend class SbiExprList;
162cdf0e10cSrcweir     friend class SbiParameters;
163cdf0e10cSrcweir     friend class SbiDimList;
164cdf0e10cSrcweir protected:
165cdf0e10cSrcweir     String        aArgName;         // Name fuer bananntes Argument
166cdf0e10cSrcweir     SbiParser*    pParser;          // fuer Fehlermeldungen, Parsing
167cdf0e10cSrcweir     SbiExpression* pNext;            // Link bei Parameterlisten
168cdf0e10cSrcweir     SbiExprNode*   pExpr;            // Der Expression-Baum
169cdf0e10cSrcweir     SbiExprType   eCurExpr;         // Art des Ausdrucks
170cdf0e10cSrcweir     SbiExprMode   m_eMode;          // Expression context
171cdf0e10cSrcweir     sal_Bool          bBased;           // sal_True: einfacher DIM-Teil (+BASE)
172cdf0e10cSrcweir     sal_Bool          bError;           // sal_True: Fehler
173cdf0e10cSrcweir     sal_Bool          bByVal;           // sal_True: ByVal-Parameter
174cdf0e10cSrcweir     sal_Bool          bBracket;         // sal_True: Parameter list with brackets
175cdf0e10cSrcweir     sal_uInt16        nParenLevel;
176cdf0e10cSrcweir     SbiExprNode* Term( const KeywordSymbolInfo* pKeywordSymbolInfo = NULL );
177cdf0e10cSrcweir     SbiExprNode* ObjTerm( SbiSymDef& );
178cdf0e10cSrcweir     SbiExprNode* Operand( bool bUsedForTypeOf = false );
179cdf0e10cSrcweir     SbiExprNode* Unary();
180cdf0e10cSrcweir     SbiExprNode* Exp();
181cdf0e10cSrcweir     SbiExprNode* MulDiv();
182cdf0e10cSrcweir     SbiExprNode* IntDiv();
183cdf0e10cSrcweir     SbiExprNode* Mod();
184cdf0e10cSrcweir     SbiExprNode* AddSub();
185cdf0e10cSrcweir     SbiExprNode* Cat();
186cdf0e10cSrcweir     SbiExprNode* Like();
187cdf0e10cSrcweir     SbiExprNode* VBA_Not();
188cdf0e10cSrcweir     SbiExprNode* Comp();
189cdf0e10cSrcweir     SbiExprNode* Boolean();
190cdf0e10cSrcweir public:
191cdf0e10cSrcweir     SbiExpression( SbiParser*, SbiExprType = SbSTDEXPR,
192cdf0e10cSrcweir         SbiExprMode eMode = EXPRMODE_STANDARD, const KeywordSymbolInfo* pKeywordSymbolInfo = NULL ); // Parsender Ctor
193cdf0e10cSrcweir     SbiExpression( SbiParser*, const String& );
194cdf0e10cSrcweir     SbiExpression( SbiParser*, double, SbxDataType = SbxDOUBLE );
195cdf0e10cSrcweir     SbiExpression( SbiParser*, const SbiSymDef&, SbiExprList* = NULL );
196cdf0e10cSrcweir     SbiExpression( SbiParser*, SbiToken );        // Spezial-Expr mit Spezial-Tokens
197cdf0e10cSrcweir    ~SbiExpression();
198cdf0e10cSrcweir     String& GetName()               { return aArgName;            }
199cdf0e10cSrcweir     void SetBased()                 { bBased = sal_True;              }
200cdf0e10cSrcweir     sal_Bool IsBased()                  { return bBased;              }
201cdf0e10cSrcweir     void SetByVal()                 { bByVal = sal_True;              }
202cdf0e10cSrcweir     sal_Bool IsByVal()                  { return bByVal;              }
203cdf0e10cSrcweir     sal_Bool IsBracket()                { return bBracket;            }
204cdf0e10cSrcweir     sal_Bool IsValid()                  { return pExpr->IsValid();    }
205cdf0e10cSrcweir     sal_Bool IsConstant()               { return pExpr->IsConstant(); }
206cdf0e10cSrcweir     sal_Bool IsVariable()               { return pExpr->IsVariable(); }
207cdf0e10cSrcweir     sal_Bool IsLvalue()                 { return pExpr->IsLvalue();   }
208cdf0e10cSrcweir     sal_Bool IsIntConstant()            { return pExpr->IsIntConst(); }
209cdf0e10cSrcweir     const String& GetString()       { return pExpr->GetString();  }
210cdf0e10cSrcweir     SbiSymDef* GetVar()             { return pExpr->GetVar();     }
211cdf0e10cSrcweir     SbiSymDef* GetRealVar()         { return pExpr->GetRealVar(); }
212cdf0e10cSrcweir     SbiExprNode* GetExprNode()      { return pExpr; }
213cdf0e10cSrcweir     SbxDataType GetType()           { return pExpr->GetType();    }
214cdf0e10cSrcweir     void SetType( SbxDataType eType){ pExpr->eType = eType;       }
215cdf0e10cSrcweir     void Gen( RecursiveMode eRecMode = UNDEFINED ); // Ausgabe eines Nodes
216cdf0e10cSrcweir };
217cdf0e10cSrcweir 
218cdf0e10cSrcweir class SbiConstExpression : public SbiExpression {
219cdf0e10cSrcweir     double nVal;
220cdf0e10cSrcweir     String aVal;
221cdf0e10cSrcweir     SbxDataType eType;
222cdf0e10cSrcweir public:                             // numerische Konstante
223cdf0e10cSrcweir     SbiConstExpression( SbiParser* );
224cdf0e10cSrcweir     SbxDataType GetType() { return eType; }
225cdf0e10cSrcweir     const String& GetString() { return aVal; }
226cdf0e10cSrcweir     double GetValue() { return nVal; }
227cdf0e10cSrcweir     short GetShortValue();
228cdf0e10cSrcweir };
229cdf0e10cSrcweir 
230cdf0e10cSrcweir class SbiExprList {                  // Basisklasse fuer Parameter und Dims
231cdf0e10cSrcweir protected:
232cdf0e10cSrcweir     SbiParser* pParser;             // Parser
233cdf0e10cSrcweir     SbiExpression* pFirst;          // Expressions
234cdf0e10cSrcweir     short nExpr;                    // Anzahl Expressions
235cdf0e10cSrcweir     short nDim;                     // Anzahl Dimensionen
236cdf0e10cSrcweir     sal_Bool  bError;                   // sal_True: Fehler
237cdf0e10cSrcweir     sal_Bool  bBracket;                 // sal_True: Klammern
238cdf0e10cSrcweir public:
239cdf0e10cSrcweir     SbiExprList( SbiParser* );
240cdf0e10cSrcweir     virtual ~SbiExprList();
241cdf0e10cSrcweir     sal_Bool  IsBracket()               { return bBracket;        }
242cdf0e10cSrcweir     sal_Bool  IsValid()                 { return sal_Bool( !bError ); }
243cdf0e10cSrcweir     short GetSize()                 { return nExpr;           }
244cdf0e10cSrcweir     short GetDims()                 { return nDim;            }
245cdf0e10cSrcweir     SbiExpression* Get( short );
246cdf0e10cSrcweir     sal_Bool  Test( const SbiProcDef& );    // Parameter-Checks
247cdf0e10cSrcweir     void  Gen();                    // Code-Erzeugung
248cdf0e10cSrcweir     void addExpression( SbiExpression* pExpr );
249cdf0e10cSrcweir };
250cdf0e10cSrcweir 
251cdf0e10cSrcweir class SbiParameters : public SbiExprList {
252cdf0e10cSrcweir public:
253cdf0e10cSrcweir     SbiParameters( SbiParser*, sal_Bool bConst = sal_False, sal_Bool bPar = sal_True);// parsender Ctor
254cdf0e10cSrcweir };
255cdf0e10cSrcweir 
256cdf0e10cSrcweir class SbiDimList : public SbiExprList {
257cdf0e10cSrcweir     sal_Bool  bConst;                   // sal_True: Alles sind Integer-Konstanten
258cdf0e10cSrcweir public:
259cdf0e10cSrcweir     SbiDimList( SbiParser* );         // Parsender Ctor
260cdf0e10cSrcweir     sal_Bool  IsConstant()              { return bConst; }
261cdf0e10cSrcweir };
262cdf0e10cSrcweir 
263cdf0e10cSrcweir #endif
264