OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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
20
25#include "Parser/Parser.h"
26#include "Parser/Token.h"
27#include "Parser/TokenStream.h"
29
30#include "Utility/IpplInfo.h"
31
32// class WhileStatement
33// Statement of the form "WHILE ( <condition> ) <statement>".
34// ------------------------------------------------------------------------
35
37 Statement("", 0), while_block(0) {
38 Token key = is.readToken();
39 Token token = is.readToken();
40
41 if(key.isKey("WHILE") && token.isDel('(')) {
42 int level = 1;
43 append(token);
44 token = is.readToken();
45
46 while(! token.isEOF()) {
47 append(token);
48
49 if(token.isDel('(')) {
50 level++;
51 } else if(token.isDel(')')) {
52 level--;
53 if(level == 0) break;
54 }
55
56 token = is.readToken();
57 }
58
59 while_block = parser.readStatement(&is);
60 } else {
61 throw ParseError("WhileStatement::WhileStatement()",
62 "Invalid \"WHILE\" statement.");
63 }
64}
65
66
68 delete while_block;
69}
70
72void WhileStatement::execute(const Parser &parser) {
73 curr = tokens.begin();
74 keep = ++curr;
75 Attribute condition = Attributes::makeBool("WHILE()", "while condition");
76
77 try {
78 condition.parse(*this, false);
80
81 while(Attributes::getBool(condition)) {
82 while_block->execute(parser);
84 }
85 } catch(...) {
86 std::ostringstream oss;
87 this->print(oss);
88 ERRORMSG("Invalid WHILE condition '" + oss.str() + "'");
89 throw;
90 }
91}
#define ERRORMSG(msg)
Definition: IpplInfo.h:350
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:90
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:100
A representation of an Object attribute.
Definition: Attribute.h:52
void parse(Statement &stat, bool eval)
Parse attribute.
Definition: Attribute.cpp:127
void update()
Update all objects.
Definition: OpalData.cpp:685
static OpalData * getInstance()
Definition: OpalData.cpp:196
Interface for abstract language parser.
Definition: Parser.h:31
virtual Statement * readStatement(TokenStream *ts) const =0
Read complete statement from token stream.
Interface for statements.
Definition: Statement.h:38
void append(const Token &)
Append a token.
Definition: Statement.cpp:45
TokenList::iterator keep
Definition: Statement.h:181
TokenList tokens
Definition: Statement.h:179
TokenList::iterator curr
Definition: Statement.h:180
virtual void execute(const Parser &)=0
Execute.
virtual void print(std::ostream &os) const
Print statement.
Definition: Statement.cpp:204
Representation of a single input token.
Definition: Token.h:33
bool isDel(char del) const
Test for delimiter.
Definition: Token.cpp:92
bool isEOF() const
Test for end of file.
Definition: Token.cpp:107
bool isKey(const char *key) const
Test for keyword.
Definition: Token.cpp:137
Abstract interface for a stream of input tokens.
Definition: TokenStream.h:33
virtual Token readToken()=0
Read single token from stream.
Parse exception.
Definition: ParseError.h:32
Statement * while_block
virtual void execute(const Parser &)
Execute.
virtual ~WhileStatement()