7 //**************************************
9 //** Objects decribing an expression **
10 //** inside an abstract tree. **
12 //**************************************
14 ostream& operator <<( ostream& stream, ExprType type )
19 return stream << "Identifier";
21 return stream << "Integer Constant";
23 return stream << "Integer expression";
25 return stream << "Real Constant";
27 return stream << "Real expression";
29 return stream << "Boolean Constant";
31 return stream << "Boolean expression";
33 return stream << "Object expression";
35 return stream << "Object Constant";
37 return stream << "Other";
39 return stream << "Error Type";
41 return stream << "Unknown value";
45 ostream& operator <<( ostream& stream, ArithOpType type )
50 return stream << " Plus ";
52 return stream << " Minus ";
54 return stream << " Multiply ";
56 return stream << " Divide ";
58 return stream << " Integer Divide";
60 return stream << " Modulo ";
62 return stream << "Unknown value";
66 ostream& operator <<( ostream& stream, BoolOpType type )
71 return stream << " Less ";
73 return stream << " Less Or Equal ";
75 return stream << " Equal ";
77 return stream << " Not Equal ";
79 return stream << " Greater ";
81 return stream << " Greater Or Equal ";
83 return stream << " Or ";
85 return stream << " And ";
87 return stream << " AndIf ";
89 return stream << " OrIf ";
91 return stream << "Unknown value";
95 ostream& operator <<( ostream& stream, ObjOpType type )
100 return stream << " Qua ";
102 return stream << " Is ";
104 return stream << " In ";
106 return stream << "Unknown value";
110 Expression::Expression( ExprType rettype , Location *objloc )
113 returntype = rettype;
117 ostream& operator << ( ostream& stream , Expression ThisExp )
119 #ifdef VERBOSE_PRINT_EXPR_TREE
120 stream << "\nExpression :";
122 ThisExp.Print( stream );
126 void Expression::Print( ostream& stream )
128 #ifdef VERBOSE_PRINT_EXPR_TREE
129 stream << "\nkind :" << kind << \
130 "\ntype :" << type << \
131 "\nlocation :" << *place << \
132 "\nReturnType :" << returntype;
136 IntegerConstant::IntegerConstant( int value, Location *TheLoc ) :
137 Expression( IntegerConst,TheLoc ),rvalue(value)
139 kind = "IntegerConstant";
142 void IntegerConstant::Print( ostream& stream )
144 Expression::Print(stream);
145 #ifdef VERBOSE_PRINT_EXPR_TREE
146 stream = stream << "\nRValue :" << rvalue;
152 RealConstant::RealConstant( double value, Location *TheLoc ) :
153 Expression( RealConst,TheLoc ),rvalue(value)
155 kind = "RealConstant";
158 void RealConstant::Print( ostream& stream )
160 Expression::Print(stream);
161 #ifdef VERBOSE_PRINT_EXPR_TREE
162 stream = stream << "\nRValue :" << rvalue;
168 BoolConstant::BoolConstant( int value, Location *TheLoc ) :
169 Expression( BoolConst, TheLoc ),rvalue(value)
174 void BoolConstant::Print( ostream& stream )
176 Expression::Print(stream);
177 #ifdef VERBOSE_PRINT_EXPR_TREE
178 stream << "\nRValue :" << rvalue;
184 StringConstant::StringConstant( String *value, Location *TheLoc ) :
185 Expression( StringConst,TheLoc ),rvalue(value)
187 kind = "StringConstant";
190 void StringConstant::Print( ostream& stream )
192 Expression::Print(stream);
193 #ifdef VERBOSE_PRINT_EXPR_TREE
194 stream << "\nRValue :" << *rvalue;
200 CharConstant::CharConstant( char value, Location *TheLoc ) :
201 Expression( CharConst, TheLoc ),rvalue(value)
203 kind = "CharConstant";
206 void CharConstant::Print( ostream& stream )
208 Expression::Print(stream);
209 #ifdef VERBOSE_PRINT_EXPR_TREE
210 stream << "\nRValue :" << rvalue;
216 Identifier::Identifier( String * TheName, Location *TheLoc ) :
217 Expression( Ident, TheLoc )
223 Identifier::Identifier( Entry ThisEntry, Location *TheLoc ) :
224 Expression( Ident, TheLoc )
231 void Identifier::Print( ostream& stream )
233 Expression::Print(stream);
234 #ifdef VERBOSE_PRINT_EXPR_TREE
235 stream << "\nTitle :" << *Title;
241 NoneObject::NoneObject( Location *TheLoc ) : Expression( ObjConst, TheLoc )
246 Result::Result( Location *TheLoc ) : Expression( ObjConst, TheLoc )
251 UnaryOperator::UnaryOperator( Expression * ThisExp, ExprType ret, Location *TheLoc ) :
252 Expression( ret, TheLoc )
254 kind = "UnaryOperator";
256 place = new Location(*(ThisExp->place) + *TheLoc);
259 void UnaryOperator::Print ( ostream& stream )
261 Expression::Print(stream);
262 #ifdef VERBOSE_PRINT_EXPR_TREE
264 Argument->Print(stream);
268 Argument->Print(stream);
273 BinaryOperator::BinaryOperator( Expression *LeftExpr, Expression *RightExpr,
274 ExprType ret , Location *TheLoc ) :
275 Expression( ret, TheLoc )
277 kind = "BinaryOperator";
278 LeftMember = LeftExpr;
279 RightMember = RightExpr;
280 place = new Location(*(LeftMember->place) + *TheLoc + *(RightMember->place));
283 virtual void BinaryOperator::Print( ostream& stream )
285 Expression::Print( stream );
286 #ifdef VERBOSE_PRINT_EXPR_TREE
287 stream << "\n(\nLeft Member :\n";
288 LeftMember->Print( stream );
290 RightMember->Print( stream );
294 LeftMember->Print(stream);
295 stream << ' ' << kind << ' ';
296 RightMember->Print(stream);
302 BoolOperator::BoolOperator( Expression *LeftExpr, Expression *RightExpr,
303 BoolOpType TheOp , Location *TheLoc ) :
304 BinaryOperator( LeftExpr, RightExpr, Other,TheLoc )
306 kind = "Boolean Operator";
309 if ( (TheOp == Or) || (TheOp == And) )
310 switch( LeftExpr->returntype)
315 switch( RightExpr->returntype )
320 returntype = BoolExpr;
324 returntype = ExprError;
325 cout << "Operand 2 of Operator " << TheOp << " : \"";
326 RightExpr->Print( cout );
327 cout << "\" is not a boolean expression.\n";
333 cout << "Operand 1 of Operator " << TheOp << " : \"";
334 LeftExpr->Print( cout );
335 cout << "\" is not a boolean expression.\n";
336 switch( RightExpr->returntype )
344 cout << "Operand 2 of Operator " << TheOp << " : \"";
345 RightExpr->Print( cout );
346 cout << "\" is not a boolan expression.\n";
349 returntype = ExprError;
352 switch( LeftExpr->returntype )
356 switch( RightExpr->returntype )
361 returntype = BoolExpr;
365 returntype = ExprError;
366 cout << "As operand 1 of operator " << TheOp << " is "
367 << LeftExpr->returntype << ", operand 2 that is \"";
368 RightExpr->Print( cout );
369 cout << "\" is expected to be an Integer Expression too.\n";
375 switch( RightExpr->returntype )
380 returntype = BoolExpr;
384 returntype = ExprError;
385 cout << "As operand 1 of operator " << TheOp << " is "
386 << LeftExpr->returntype << ", operand 2 that is \"";
387 RightExpr->Print( cout );
388 cout << "\" is expected to be a Real Expression too.\n";
394 switch( RightExpr->returntype )
399 returntype = BoolExpr;
403 returntype = ExprError;
404 cout << "As operand 1 of operator " << TheOp << " is "
405 << LeftExpr->returntype << ", operand 2 that is \"";
406 RightExpr->Print( cout );
407 cout << "\" is expected to be a Character Expression too.\n";
413 switch( RightExpr->returntype )
418 returntype = BoolExpr;
422 returntype = ExprError;
423 cout << "As operand 1 of operator " << TheOp << " is "
424 << LeftExpr->returntype << ", operand 2 that is \"";
425 RightExpr->Print( cout );
426 cout << "\" is expected to be a String Expression too.\n";
432 if ( (TheOp == Equal) || (TheOp == NotEqual) )
433 switch( RightExpr->returntype )
438 returntype = BoolExpr;
442 returntype = ExprError;
443 cout << "As operand 1 of operator " << TheOp << " is "
444 << LeftExpr->returntype << ", operand 2 that is \"";
445 RightExpr->Print( cout );
446 cout << "\" is expected to be an Object Expression too.\n";
450 cout << " Only = and <> can be applied onto objects.\n";
451 returntype = ExprError;
457 if ( (TheOp == Equal) || (TheOp == NotEqual) )
458 switch( RightExpr->returntype )
463 returntype = BoolExpr;
467 returntype = ExprError;
468 cout << "As operand 1 of operator " << TheOp << " is "
469 << LeftExpr->returntype << ", operand 2 that is \"";
470 RightExpr->Print( cout );
471 cout << "\" is expected to be a Boolean Expression too.\n";
475 cout << " Only = and <> can be applied onto booleans.\n";
476 returntype = ExprError;
481 switch( RightExpr->returntype )
491 returntype = BoolExpr;
496 if ( (TheOp == Equal) || (TheOp == NotEqual) )
497 returntype = BoolExpr;
500 cout << " You can only apply Equal and NotEqual onto objects.\n";
501 returntype = ExprError;
507 if ( (TheOp == Equal) || (TheOp == NotEqual) )
508 returntype = BoolExpr;
511 cout << " You can only apply Equal and NotEqual onto booleans.\n";
512 returntype = ExprError;
517 returntype = BoolExpr;
521 returntype = ExprError;
522 cout << " Type of Operand 2 \"";
523 RightExpr->Print( cout );
524 cout << "\" of operand " << TheOp << " is wrong.\n";
529 returntype = ExprError;
533 returntype = ExprError;
534 cout << " The type of Operand 1 \"";
535 LeftExpr->Print( cout );
536 cout << "\" of operator " << TheOp << " is wrong.\n";
540 virtual void BoolOperator::Print( ostream& stream )
542 #ifdef VERBOSE_PRINT_EXPR_TREE
543 BinaryOperator::Print( stream );
544 stream << "\nKind of Boolean Operator : " << OpKind;
547 LeftMember->Print( stream );
548 stream << ' ' << OpKind << ' ';
549 RightMember->Print( stream );
554 ArithOperator::ArithOperator( Expression *LeftExpr, Expression *RightExpr,
555 ArithOpType TheOp , Location *TheLoc ) :
556 BinaryOperator( LeftExpr, RightExpr, Other, TheLoc )
558 kind = "Arithmetic Operator";
561 switch( LeftExpr->returntype)
566 switch( RightExpr->returntype )
570 returntype = IntegerExpr;
575 returntype = RealExpr;
579 returntype = IntegerExpr;
583 returntype = ExprError;
587 returntype = ExprError;
588 cout << "Operand 2 of Operator " << TheOp << " : \"";
589 RightExpr->Print( cout );
590 cout << "\" is not an arithmetic expression.\n";
597 switch( RightExpr->returntype )
605 returntype = RealExpr;
609 returntype = ExprError;
613 returntype = ExprError;
614 cout << "Operand 2 of Operator " << TheOp << " : \"";
615 RightExpr->Print( cout );
616 cout << "\" is not an arithmetic expression.\n";
622 switch( RightExpr->returntype )
626 returntype = IntegerExpr;
631 returntype = RealExpr;
639 returntype = ExprError;
643 returntype = ExprError;
644 cout << "Operand 2 of Operator " << TheOp << " : \"";
645 RightExpr->Print( cout );
646 cout << "\" is not an arithmetic expression.\n";
651 returntype = ExprError;
655 returntype = ExprError;
656 cout << "Operand 1 of Operator " << TheOp << " : \"";
657 LeftExpr->Print( cout );
658 cout << "\" is not an arithmetic expression.\n";
659 switch( RightExpr->returntype )
669 cout << "Operand 2 of Operator " << TheOp << " : \"";
670 RightExpr->Print( cout );
671 cout << "\" is not an arithmetic expression.\n";
677 virtual void ArithOperator::Print( ostream& stream)
679 #ifdef VERBOSE_PRINT_EXPR_TREE
680 BinaryOperator::Print( stream );
681 stream << "\nKind of Arithmetic Operator : " << OpKind;
684 LeftMember->Print( stream );
685 stream << ' ' << OpKind << ' ';
686 RightMember->Print( stream );
691 ObjOperator::ObjOperator( Expression *LeftExpr, Expression *RightExpr,
692 ObjOpType TheOp , Location *TheLoc ) :
693 BinaryOperator( LeftExpr, RightExpr, Other, TheLoc )
695 kind = "Object Operator";
698 switch(LeftExpr->returntype)
701 switch ( RightExpr->returntype )
705 returntype = ObjExpr;
707 returntype = BoolExpr;
711 returntype = ExprError;
715 returntype = ExprError;
716 cout << "Operand 2 of Operator " << TheOp << " : \"";
717 RightExpr->Print( cout );
718 cout << "\" is not an Identifier.\n";
723 returntype = ExprError;
727 cout << "Operand 1 of Operator " << TheOp << " : \"";
728 LeftExpr->Print( cout );
729 cout << "\" is not an Identifier.\n";
734 virtual void ObjOperator::Print( ostream& stream )
736 #ifdef VERBOSE_PRINT_EXPR_TREE
737 BinaryOperator::Print( stream );
738 stream << "\nKind of Object Operator : " << OpKind;
741 LeftMember->Print( stream );
742 stream << ' ' << OpKind << ' ';
743 RightMember->Print( stream );
748 Not::Not( Expression *TheExpr, Location *TheLoc ) :
749 UnaryOperator( TheExpr , Other, TheLoc )
753 switch( TheExpr->returntype )
758 returntype = BoolExpr;
762 returntype = ExprError;
766 returntype = ExprError;
767 cout << "Operand of Operator Not : \"";
768 TheExpr->Print( cout );
769 cout << "\" is not a Boolean Expression.\n";
773 This::This( Expression *TheExpr, Location *TheLoc ) :
774 UnaryOperator( TheExpr, Other, TheLoc )
779 switch ( TheExpr->returntype )
782 returntype = ObjExpr;
786 returntype = ExprError;
787 cout << "Operand is of operator 'This' is not an Identifier.\n";
790 returntype = ObjExpr;
793 void This::Print( ostream& stream )
797 UnaryOperator::Print( stream );
800 New::New( Expression *TheExpr, Location *TheLoc ) :
801 UnaryOperator( TheExpr , Other, TheLoc )
805 switch( TheExpr->returntype )
808 returntype = ObjExpr;
812 returntype = ExprError;
816 returntype = ExprError;
817 cout << "Operand \"";
818 TheExpr->Print(cout);
819 cout << "\"type of New operator is wrong.\n";
823 Copy::Copy( Expression *TheExpr, Location *TheLoc ) :
824 UnaryOperator( TheExpr, Other, TheLoc )
827 switch(TheExpr->returntype)
831 returntype = ObjExpr;
835 returntype = ExprError;
839 returntype = ExprError;
840 cout << "Wrong operand type for operand \"";
841 TheExpr->Print(cout);
842 cout << "\" of Copy.\n";
847 ArrayOf::ArrayOf( Expression *TheExpr, ExprType ret, Location *TheLoc ) :
848 UnaryOperator( TheExpr, ret, TheLoc )
853 Error::Error( ) : Expression( ExprError, NULL )
855 kind = "Error into Expression";