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