Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / loglan96 / loglan93 / expr.h
1 //****************************************************************
2 //*                                                              *
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.               *
6 //*                                                              *
7 //* (c) LITA, university of PAU (France), summer 1993.           *
8 //****************************************************************
9
10 //**************************************
11 //**                                  **
12 //**  Objects decribing an expression **
13 //**  inside an abstract tree.        **
14 //**                                  **
15 //**************************************
16
17 // This is a global variable used for identify the location of a lexem.
18 extern Location ThisPlace;
19
20 //**
21 //** Enumeration type used for calculating the return type
22 //** of one expression node.
23 //**
24
25 typedef enum
26 {
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
35                 // predefined type ).
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.
44 } ExprType;
45
46 // We overload this operator for let it print an ExprType object.
47 ostream& operator << ( ostream& , ExprType    );
48
49 //**
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.
54 //**
55
56 class Entry
57 {
58   int dummy; //** Just to say the class is not empty.
59 };
60
61 //**
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.
65 //**
66
67 class Expression
68 {
69
70   public:
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.
74
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.
80
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 );
84 } ;
85
86 //**
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.
90 //**
91
92 class Identifier : public Expression
93 {
94   public:
95   String *Title; // The name of the operator.
96   Entry  Where;  // The entry if defined (else NULL).
97
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.
103
104 } ;
105
106 //**
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.
111 //**
112
113 class UnaryOperator : public Expression
114 {
115   public:
116   Expression *Argument;  // This holds the argument which is another
117                          // expression.
118
119 // We need to construct this the argument taken and what kind of result
120 // this operator returns.
121
122   UnaryOperator( Expression *ThisExp, ExprType ret, Location * ); 
123
124   virtual void Print( ostream& ); // This print outputs the name of
125                                   // the operator and the argument into brackets.
126
127 } ;
128
129 //**
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 ).
135 //**
136
137 class BinaryOperator : public Expression
138 {
139 public:
140   Expression *LeftMember,*RightMember;
141
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 ','.
146
147 } ;
148
149 //**
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
154 //** TYPE).
155 //**
156
157 typedef enum
158 {
159   Less,            // A <     B
160   LessOrEqual,     // A <=    B
161   Equal,           // A =     B
162   NotEqual,        // A =/=   B or A <> B
163   Greater,         // A >     B
164   GreaterOrEqual,  // A >=    B
165   Or,              // A OR    B (inclusif OR)
166   And,             // A AND   B
167   AndIf,           // A ANDIF B (same as AND)
168   OrIf             // A ORIF  B (same as OR)
169 } BoolOpType;
170
171 // We overload << for printing BoolOpType variable.
172
173 ostream& operator << ( ostream& , BoolOpType  );
174
175 class BoolOperator : public BinaryOperator
176 {
177 public:
178   BoolOpType OpKind;
179
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& );
187 } ;
188
189 //**
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).
193 //**
194
195 typedef enum
196 {
197   Plus,         // A  +  B
198   Minus,        // A  -  B
199   Multiply,     // A  *  B
200   Divide,       // A  /  B
201   IntDivide,    // A DIV B
202   Modulo        // A MOD B
203 } ArithOpType;
204
205 ostream& operator << ( ostream& , ArithOpType );
206
207 class ArithOperator : public BinaryOperator
208 {
209 public:
210   ArithOpType OpKind;
211   ArithOperator( Expression  *LeftExpr, Expression  *RightExpr,
212                  ArithOpType  Operator, Location * );
213   void Print( ostream& );
214 } ;
215
216 //**
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 ).
220 //**
221
222 typedef enum
223 {
224   Qua,
225   Is,
226   In
227 } ObjOpType;
228
229 ostream& operator << ( ostream& , ObjOpType   );
230
231 class ObjOperator   : public BinaryOperator
232 {
233 public:
234   ObjOpType OpKind;
235   ObjOperator( Expression *LeftExpr, Expression *RightExpr,
236                ObjOpType   Operator, Location * );
237   void Print( ostream& );
238 } ;
239
240 class Not : public UnaryOperator
241 {
242 public:
243   Not( Expression *TheExpr, Location * );
244 } ;
245
246 class This : public UnaryOperator
247 {
248   public:
249   This( Expression *TheExpr, Location * );
250   void Print( ostream& );
251 } ;
252
253 class New : public UnaryOperator
254 {
255   public:
256   New( Expression *TheExpr, Location * );
257 } ;
258
259 class Copy : public UnaryOperator
260 {
261   public:
262   Copy( Expression *TheExpr, Location * );
263 } ;
264
265 class ArrayOf : public UnaryOperator
266 {
267   public:
268   ArrayOf( Expression *TheExpr, ExprType ret, Location * );
269 } ;
270
271 class NoneObject : public Expression
272 {
273   public:
274   NoneObject( Location * );
275 } ;
276
277 class Result : public Expression
278 {
279   public:
280   Result( Location * );
281 } ;
282
283 class Error : public Expression
284 {
285   public:
286   Error( void );
287 } ;
288
289 class IntegerConstant : public Expression
290 {
291 public:
292   int rvalue;
293   IntegerConstant( int value, Location * );
294   void Print( ostream& );
295
296 } ;
297
298 class RealConstant : public Expression
299 {
300 public:
301   double rvalue;
302   RealConstant( double value, Location * );
303   void Print( ostream& );
304
305 } ;
306
307 class BoolConstant : public Expression
308 {
309   public:
310   int rvalue;
311
312   BoolConstant( int, Location * );
313   void Print( ostream& );
314 } ;
315
316 class StringConstant : public Expression
317 {
318   public:
319   String *rvalue;
320
321   StringConstant( String *, Location * );
322   void Print( ostream& );
323 } ;
324
325 class CharConstant : public Expression
326 {
327   public:
328   char rvalue;
329
330   CharConstant( char rvalue, Location * );
331   void Print( ostream& );
332 } ;