1 //****************************************************************
3 //* Expr.h : Header file of the Expression hierarchy classes *
4 //* This file describes all the nodes contained into *
5 //* an expression tree of the program. *
7 //* (c) LITA, university of PAU (France), summer 1993. *
8 //****************************************************************
10 //**************************************
12 //** Objects decribing an expression **
13 //** inside an abstract tree. **
15 //**************************************
17 // This is a global variable used for identify the location of a lexem.
18 extern Location ThisPlace;
21 //** Enumeration type used for calculating the return type
22 //** of one expression node.
27 Ident, // Identifier node : could match any kind of expression
28 IntegerConst, // Integer constant node.
29 IntegerExpr, // Integer expression node.
30 RealConst, // Real constant node.
31 RealExpr, // Real expression node.
32 BoolConst, // Boolean constant node.
33 BoolExpr, // Boolean expression node.
34 ObjConst, // Object constant node (like NONE operator or
36 ObjExpr, // Object expression node.
37 StringExpr, // String expression node.
38 StringConst, // String constant node.
39 CharExpr, // Character expression node.
40 CharConst, // Character constant node.
41 Other, // Other Node (used for constructors, will be removed too ).
42 ExprError // This node and maybe its children is holding
43 // a wrong expression.
46 // We overload this operator for let it print an ExprType object.
47 ostream& operator << ( ostream& , ExprType );
50 //** This class is a dummy class that is just for testing code.
51 //** The code should be removed when joining to the symbol Table.
52 //** This is supposed to described the type of one symbol table entry.
53 //** The only node to use it is the Identifier Node for now.
58 int dummy; //** Just to say the class is not empty.
62 //** This is the Expression class. This class is the root of all the tree
63 //** of the expression classes. You find in it all the method and fields
64 //** that every node uses.
71 String kind; // Verbose description of the type of the node.
72 Location *place; // Position of the object in the source text.
73 ExprType returntype; // Kind of result the node produces.
75 Expression( ExprType,Location* );// The constructor take just the kind
76 // of the node, and eventually its position.
77 virtual void Print( ostream& ); // Virtual procedure that prints the contents
78 // of the node. Of course there is a lot of
79 // different version of this procedure.
81 // This overloading allow the programmer to write 'cout << TheExpression'
82 // for printing the contents of all the nodes in the tree.
83 friend ostream& operator << ( ostream& , Expression );
87 //** This is the Identifier Operator Class that derives from Expression
88 //** class. This nodes holds a string field to store temporaly the string that
89 //** lex has parsed and an entry type for the variable in the symbol table.
92 class Identifier : public Expression
95 String *Title; // The name of the operator.
96 Entry Where; // The entry if defined (else NULL).
98 Identifier( String *, Location * );
99 // Initialised with the name if it has not been
100 // declared (maybe further).
101 Identifier( Entry, Location *); // else initialised with its entry.
102 void Print( ostream& ); // This Print write down the name of the identifier.
107 //** This is the UnaryOperator class. This class derives from Expression
108 //** from which it adds an Expression pointer for one argument of type
109 //** Expression. This class in general should not be instantiated.
110 //** Use derivation for creating new unary operator.
113 class UnaryOperator : public Expression
116 Expression *Argument; // This holds the argument which is another
119 // We need to construct this the argument taken and what kind of result
120 // this operator returns.
122 UnaryOperator( Expression *ThisExp, ExprType ret, Location * );
124 virtual void Print( ostream& ); // This print outputs the name of
125 // the operator and the argument into brackets.
130 //** This is the BinaryOperator class. This is the same as UnaryOperator
131 //** except it takes two expression instead of one.
132 //** This class is separate from the UnaryOperator because functions that
133 //** takes one argument can't accept two arguments (which will surely happend
134 //** if BinaryOperator derives from UnaryOperator ).
137 class BinaryOperator : public Expression
140 Expression *LeftMember,*RightMember;
142 BinaryOperator( Expression *LeftExpr, Expression *RightExpr,
143 ExprType ret, Location *TheLoc );
144 virtual void Print( ostream& ); // The two expression are printed into
145 // brackets and separated by a ','.
150 //** This is the BoolOperator class. This class describes all the boolean
151 //** operators ( =, =/= or <>, <, <=, >, >=, OR, AND, ORIF and ANDIF ).
152 //** The operators are discimined by the field OpKind.
153 //** We use for this a new enumeration type called BoolOpType (BOOLean OPerator
160 LessOrEqual, // A <= B
162 NotEqual, // A =/= B or A <> B
164 GreaterOrEqual, // A >= B
165 Or, // A OR B (inclusif OR)
167 AndIf, // A ANDIF B (same as AND)
168 OrIf // A ORIF B (same as OR)
171 // We overload << for printing BoolOpType variable.
173 ostream& operator << ( ostream& , BoolOpType );
175 class BoolOperator : public BinaryOperator
180 // For creation, we need the two expression and the kind of boolean operator.
181 // For example if a is an 'BoolOperator *'
182 // then 'a = new BoolOperator( Expr1, Expr2, Or )' will describe the expression
183 // '(Expr1) OR (Expr2)'.
184 BoolOperator( Expression *LeftExpr, Expression *RightExpr,
185 BoolOpType Operator, Location * );
186 void Print( ostream& );
190 //** This is the ArithOperator class. This class is designed on the same model
191 //** as BoolOperator but for arithmetic Operator.
192 //** The new enumerated type is calls ArithOpType (ARITHmetic OPerator TYPE).
201 IntDivide, // A DIV B
205 ostream& operator << ( ostream& , ArithOpType );
207 class ArithOperator : public BinaryOperator
211 ArithOperator( Expression *LeftExpr, Expression *RightExpr,
212 ArithOpType Operator, Location * );
213 void Print( ostream& );
217 //** This is the ObjOperator class. This class derives from the class
218 //** BinaryOperator. It describes the operators which need object expression
219 //** as argument (essentially QUA, IS ,IN ).
229 ostream& operator << ( ostream& , ObjOpType );
231 class ObjOperator : public BinaryOperator
235 ObjOperator( Expression *LeftExpr, Expression *RightExpr,
236 ObjOpType Operator, Location * );
237 void Print( ostream& );
240 class Not : public UnaryOperator
243 Not( Expression *TheExpr, Location * );
246 class This : public UnaryOperator
249 This( Expression *TheExpr, Location * );
250 void Print( ostream& );
253 class New : public UnaryOperator
256 New( Expression *TheExpr, Location * );
259 class Copy : public UnaryOperator
262 Copy( Expression *TheExpr, Location * );
265 class ArrayOf : public UnaryOperator
268 ArrayOf( Expression *TheExpr, ExprType ret, Location * );
271 class NoneObject : public Expression
274 NoneObject( Location * );
277 class Result : public Expression
280 Result( Location * );
283 class Error : public Expression
289 class IntegerConstant : public Expression
293 IntegerConstant( int value, Location * );
294 void Print( ostream& );
298 class RealConstant : public Expression
302 RealConstant( double value, Location * );
303 void Print( ostream& );
307 class BoolConstant : public Expression
312 BoolConstant( int, Location * );
313 void Print( ostream& );
316 class StringConstant : public Expression
321 StringConstant( String *, Location * );
322 void Print( ostream& );
325 class CharConstant : public Expression
330 CharConstant( char rvalue, Location * );
331 void Print( ostream& );