OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
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 "OpalParser/Parser.h"
26#include "OpalParser/Token.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)
54 break;
55 }
56
57 token = is.readToken();
58 }
59
60 while_block = parser.readStatement(&is);
61 } else {
62 throw ParseError("WhileStatement::WhileStatement()", "Invalid \"WHILE\" statement.");
63 }
64}
65
69
71void WhileStatement::execute(const Parser& parser) {
72 curr = tokens.begin();
73 keep = ++curr;
74 Attribute condition = Attributes::makeBool("WHILE()", "while condition");
75
76 try {
77 condition.parse(*this, false);
79
80 while (Attributes::getBool(condition)) {
81 while_block->execute(parser);
83 }
84 } catch (...) {
85 std::ostringstream oss;
86 this->print(oss);
87 *ippl::Error << "Invalid WHILE condition '" + oss.str() + "'";
88 throw;
89 }
90}
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
bool getBool(const Attribute &attr)
Return logical value.
A representation of an Object attribute.
Definition Attribute.h:52
void parse(Statement &stat, bool eval)
Parse attribute.
void update()
Update all objects.
Definition OpalData.cpp:701
static OpalData * getInstance()
Definition OpalData.cpp:195
Interface for abstract language parser.
Definition Parser.h:31
virtual Statement * readStatement(TokenStream *ts) const =0
Read complete statement from token stream.
Statement(const std::string &name, int line)
Constructor.
Definition Statement.cpp:28
TokenList::iterator keep
Definition Statement.h:181
TokenList tokens
Definition Statement.h:179
TokenList::iterator curr
Definition Statement.h:180
virtual void print(std::ostream &os) const
Print statement.
Representation of a single input token.
Definition Token.h:33
bool isDel(char del) const
Test for delimiter.
Definition Token.cpp:92
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.
Statement * while_block
virtual void execute(const Parser &)
Execute.
virtual ~WhileStatement()
Parse exception.
Definition ParseError.h:32