OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
WhileStatement.cpp
Go to the documentation of this file.
1 // ------------------------------------------------------------------------
2 // $RCSfile: WhileStatement.cpp,v $
3 // ------------------------------------------------------------------------
4 // $Revision: 1.1.1.1 $
5 // ------------------------------------------------------------------------
6 // Copyright: see Copyright.readme
7 // ------------------------------------------------------------------------
8 //
9 // Class: WhileStatement
10 // Representation for OPAL WHILE statement.
11 //
12 // ------------------------------------------------------------------------
13 //
14 // $Date: 2000/03/27 09:33:43 $
15 // $Author: Andreas Adelmann $
16 //
17 // ------------------------------------------------------------------------
18 
22 #include "Attributes/Attributes.h"
24 #include "Parser/Parser.h"
25 #include "Parser/Token.h"
26 #include "Parser/TokenStream.h"
27 #include "Utilities/ParseError.h"
28 
29 // class WhileStatement
30 // Statement of the form "WHILE ( <condition> ) <statement>".
31 // ------------------------------------------------------------------------
32 
34  Statement("", 0), while_block(0) {
35  Token key = is.readToken();
36  Token token = is.readToken();
37 
38  if(key.isKey("WHILE") && token.isDel('(')) {
39  int level = 1;
40  append(token);
41  token = is.readToken();
42 
43  while(! token.isEOF()) {
44  append(token);
45 
46  if(token.isDel('(')) {
47  level++;
48  } else if(token.isDel(')')) {
49  level--;
50  if(level == 0) break;
51  }
52 
53  token = is.readToken();
54  }
55 
56  while_block = parser.readStatement(&is);
57  } else {
58  throw ParseError("WhileStatement::WhileStatement()",
59  "Invalid \"WHILE\" statement.");
60  }
61 }
62 
63 
65  delete while_block;
66 }
67 
69 void WhileStatement::execute(const Parser &parser) {
70  curr = tokens.begin();
71  keep = ++curr;
72  Attribute condition = Attributes::makeBool("WHILE()", "while condition");
73 
74  try {
75  condition.parse(*this, false);
77 
78  while(Attributes::getBool(condition)) {
79  while_block->execute(parser);
81  }
82  } catch(...) {
83  std::ostringstream oss;
84  this->print(oss);
85  ERRORMSG("Invalid WHILE condition '" + oss.str() + "'");
86  throw;
87  }
88 }
bool isEOF() const
Test for end of file.
Definition: Token.cpp:107
TokenList::iterator keep
Definition: Statement.h:180
Parse exception.
Definition: ParseError.h:32
TokenList tokens
Definition: Statement.h:178
Abstract interface for a stream of input tokens.
Definition: TokenStream.h:33
#define ERRORMSG(msg)
Definition: IpplInfo.h:399
void update()
Update all objects.
Definition: OpalData.cpp:723
Statement * while_block
virtual Statement * readStatement(TokenStream *ts) const =0
Read complete statement from token stream.
virtual void execute(const Parser &)
Execute.
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:66
Interface for abstract language parser.
Definition: Parser.h:31
static OpalData * getInstance()
Definition: OpalData.cpp:209
virtual Token readToken()=0
Read single token from stream.
A representation of an Object attribute.
Definition: Attribute.h:55
Interface for statements.
Definition: Statement.h:38
Representation of a single input token.
Definition: Token.h:33
virtual void print(std::ostream &os) const
Print statement.
Definition: Statement.cpp:206
void append(const Token &)
Append a token.
Definition: Statement.cpp:47
TokenList::iterator curr
Definition: Statement.h:179
virtual void execute(const Parser &)=0
Execute.
virtual ~WhileStatement()
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:56
bool isKey(const char *key) const
Test for keyword.
Definition: Token.cpp:137
bool isDel(char del) const
Test for delimiter.
Definition: Token.cpp:92
void parse(Statement &stat, bool eval)
Parse attribute.
Definition: Attribute.cpp:125