1 //****************************************************************
3 //* Instr.h : Header file for the object class hierarchy for *
4 //* the instructions. *
6 //* (c) LITA, university of PAU (France), summer 1993. *
7 //****************************************************************
15 //***************************************
17 //** Objects decribing an instruction **
18 //** inside an abstract tree. **
20 //***************************************
25 Single_Instruction Complex_Instruction
30 *:= *job io signal control object
32 Attach Put Raise Return Wind
35 Detach ReadLn Generator
48 virtual void Instruction::Print( ostream& stream )
50 stream << " \"" << kind << "\" ";
53 ostream& operator << ( ostream& stream , Instruction TheInstr )
55 TheInstr.Print( stream );
58 OneArgInstr::OneArgInstr( Expression * TheExpr, String TheStr, Location *TheLoc ) :
59 Instruction( TheStr, TheLoc )
64 void OneArgInstr::Print( ostream& stream )
66 Instruction::Print( stream );
67 stream << "(" << *argument << ");\n";
70 TwoArgInstr::TwoArgInstr( Expression *TheExpr1, Expression *TheExpr2,
71 String TheStr , Location *TheLoc) :
72 Instruction( TheStr, TheLoc )
78 void TwoArgInstr::Print( ostream& stream )
80 Instruction::Print( stream );
81 stream << "(" << *arg1 << "," << *arg2 << ");";
84 Affectation::Affectation( Expression *L_Value, Expression *R_Value ) :
85 TwoArgInstr( L_Value , R_Value, String("Affectation"),
86 new Location(*L_Value->place + *R_Value->place) )
88 switch( L_Value->returntype )
91 /* Here we should verify if the returntype of the Expression is coherent
92 with the type of the Identifier ( if it is declared ). */
95 L_Value->Print( cout );
96 cout << " is not an Identifier .\n" ;
101 void Affectation::Print( ostream& stream )
103 arg1->Print( stream );
105 arg2->Print( stream );
108 Attach::Attach( Expression *TheExpr, Location *TheLoc ) :
109 OneArgInstr( TheExpr, String("Attach"),
110 new Location( *TheLoc + *TheExpr->place ))
112 switch( TheExpr->returntype )
117 /* Here we should verify if the Expression
118 is coherent with a coroutine object. */
122 /* Here we test if the expression is correct or not.
123 If it is not correct, a report of the error has already been
124 emited and we have to go ahead without any comment for avoiding
125 errors provoked by the wrong expression. */
129 /* Here the Expression is correct according to the rules of building
130 an expression but the expression is not an object type so we emit
131 an error saying that there is a mismatched expression used with
132 the Attach instruction. */
133 cout << " Operand \"";
134 TheExpr->Print( cout );
135 cout << "\" for instruction Attach is wrong.\n";
140 Resume::Resume( Expression *TheExpr, Location *TheLoc ) :
141 OneArgInstr( TheExpr, String("Resume"),
142 new Location( *TheLoc + *TheExpr->place ) )
144 switch( TheExpr->returntype )
149 /* Here we should verify if the Expression
150 is coherent with a process object. */
154 /* Here we test if the expression is correct or not.
155 If it is not correct, a report of the error has already been
156 emited and we have to go ahead without any comment for avoiding
157 errors provoked by the wrong expression. */
161 /* Here the Expression is correct according to the rules of building
162 an expression but the expression is not an object type so we emit
163 an error saying that there is a mismatched expression used with
164 the Resume instruction. */
165 cout << " Operand \"";
166 TheExpr->Print( cout );
167 cout << "\" for instruction Resume is wrong.\n";
172 Stop::Stop( Expression *TheExpr, Location *TheLoc ) :
173 OneArgInstr( TheExpr, String("Stop"),
174 new Location( *TheLoc + *TheExpr->place) )
176 switch( TheExpr->returntype )
181 /* Here we should verify if the Expression
182 is coherent with a process object. */
186 /* Here we test if the expression is correct or not.
187 If it is not correct, a report of the error has already been
188 emited and we have to go ahead without any comment for avoiding
189 errors provoked by the wrong expression. */
193 /* Here the Expression is correct according to the rules of building
194 an expression but the expression is not an object type so we emit
195 an error saying that there is a mismatched expression used with
196 the instruction Stop. */
197 cout << " Operand \"";
198 TheExpr->Print( cout );
199 cout << "\" for instruction Stop is wrong.\n";
204 Stop::Stop( Location *TheLoc ) : OneArgInstr( NULL, String("Stop"), TheLoc ) {}
206 void Stop::Print( ostream& stream )
209 OneArgInstr::Print( stream );
214 Get::Get( Expression *TheArg, Location *TheLoc) :
215 OneArgInstr( TheArg, String("Get"),
216 new Location(*TheArg->place + *TheLoc) )
218 switch(TheArg->returntype)
225 cout << " Operand \"";
226 TheArg->Print( cout );
227 cout << "\" for instruction Get is wrong.\n";
232 Put::Put( Expression *TheArg, Location *TheLoc) :
233 OneArgInstr( TheArg, String("Put"),
234 new Location(*TheArg->place + *TheLoc) )
236 switch(TheArg->returntype)
243 cout << " Operand \"";
244 TheArg->Print( cout );
245 cout << "\" for instruction Put is wrong.\n";
250 Block::Block( Instruction *FirstInstr, Location *TheLoc ) :
251 Instruction( String("Block"), TheLoc ), InstrList( FirstInstr )
254 // We have to test the pointer because a block of instruction could be empty.
255 // i.e. : for i := 1 to 2 do od;
256 // \___/-> empty block;
258 *place = *place + *FirstInstr->place;
262 Block& operator += ( Block& TheBlock, Instruction *TheInstr )
264 TheBlock.InstrList += TheInstr;
265 // The test is not worth here because the block is expanded only if there is
266 // a new instruction who of course is not empty.
267 *TheBlock.place = *TheBlock.place + *TheInstr->place;
271 void Block::Print( ostream& stream )
274 InstrList.Print( stream );
275 stream << "End of Block.\n";
278 ListOfInstr::ListOfInstr( Instruction *TheInstr )
284 ListOfInstr& operator += ( ListOfInstr& TheList , Instruction *TheInstr )
286 ListOfInstr *Current = &TheList;
288 while (Current->NextInst != NULL ) Current = Current->NextInst;
289 Current->NextInst = new ListOfInstr( TheInstr );
293 void ListOfInstr::Print( ostream& stream )
295 ListOfInstr *Current;
298 Instruct->Print( stream );
301 while (Current != NULL)
303 Current->Instruct->Print( stream );
305 Current = Current->NextInst;
310 ConditionIf::ConditionIf( Expression *TheCondition,
311 Block *Block1, Block *Block2, Location *TheLoc ) :
312 Instruction( String( " If " ),
313 new Location( *TheLoc + *Block1->place + *Block2->place ) ),
314 Condition( TheCondition ), BlockThen( Block1 ), BlockElse( Block2 )
316 switch( TheCondition->returntype )
325 cout << " Bad Expression \"";
326 TheCondition->Print( cout );
327 cout << " for If condition.\n";
332 void ConditionIf::Print( ostream& stream )
335 Condition->Print( cout );
336 cout << ")\nThen \n";
337 BlockThen->Print( cout );
341 BlockElse->Print( cout );
345 void Loop::Print( ostream& stream )
348 Body->Print( stream );
352 While::While( Expression *TheExpr, Block *TheBlock, Location *TheLoc ) :
353 Loop( TheBlock, String("While") ), Condition( TheExpr )
357 *place = *TheLoc + *TheBlock->place;
358 switch( TheExpr->returntype )
367 cout << "Wrong expression \"";
368 TheExpr->Print( cout );
369 cout << "\" for Instruction While.\n";
375 void While::Print( ostream& stream )
378 Condition->Print( stream );
379 Loop::Print( stream );
382 For::For( Expression *TheVar, Expression *Initial, Expression *Final,
383 Expression *Step, Block *TheBlock, Location *TheLoc ) :
384 Loop( TheBlock, String("For") ), Counter( TheVar ),
385 CounterInit( Initial ), CounterStop( Final ), CounterStep( Step )
387 *place = *TheLoc + *TheVar->place + *Initial->place + *Final->place +
388 *Step->place + *TheBlock->place;
389 switch( CounterInit->returntype )
400 cout << " Bad Expression type for Initial value \"";
401 CounterInit->Print( cout );
402 cout << "\" of variable ";
403 Counter->Print( cout );
407 switch( CounterStop->returntype )
418 cout << " Bad Expression type for Initial value \"";
419 CounterInit->Print( cout );
420 cout << "\" of variable ";
421 Counter->Print( cout );
425 switch( CounterStep->returntype )
436 cout << " Bad Expression type for Step value \"";
437 CounterStep->Print( cout );
443 void For::Print( ostream& stream )
446 Counter->Print( cout );
448 CounterInit->Print( cout );
450 CounterStop->Print( cout );
452 CounterStep->Print( cout );
453 Loop::Print( stream );