OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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) break;
54 }
55
56 token = is.readToken();
57 }
58
59 while_block = parser.readStatement(&is);
60 } else {
61 throw ParseError("WhileStatement::WhileStatement()", "Invalid \"WHILE\" statement.");
62 }
63}
64
66
68void WhileStatement::execute(const Parser& parser) {
69 curr = tokens.begin();
70 keep = ++curr;
71 Attribute condition = Attributes::makeBool("WHILE()", "while condition");
72
73 try {
74 condition.parse(*this, false);
76
77 while (Attributes::getBool(condition)) {
78 while_block->execute(parser);
80 }
81 } catch (...) {
82 std::ostringstream oss;
83 this->print(oss);
84 *ippl::Error << "Invalid WHILE condition '" + oss.str() + "'";
85 throw;
86 }
87}
A representation of an Object attribute.
Definition Attribute.h:52
void parse(Statement &stat, bool eval)
Parse attribute.
Definition Attribute.cpp:76
void update()
Update all objects.
Definition OpalData.cpp:587
static OpalData * getInstance()
Definition OpalData.cpp:193
Parse exception.
Definition ParseError.h:31
Interface for abstract language parser.
Definition Parser.h:30
virtual Statement * readStatement(TokenStream *ts) const =0
Read complete statement from token stream.
Interface for statements.
Definition Statement.h:37
void append(const Token &)
Append a token.
Definition Statement.cpp:39
TokenList::iterator keep
Definition Statement.h:178
TokenList tokens
Definition Statement.h:176
TokenList::iterator curr
Definition Statement.h:177
virtual void execute(const Parser &)=0
Execute.
virtual void print(std::ostream &os) const
Print statement.
Abstract interface for a stream of input tokens.
Definition TokenStream.h:30
virtual Token readToken()=0
Read single token from stream.
Representation of a single input token.
Definition Token.h:32
bool isDel(char del) const
Test for delimiter.
Definition Token.cpp:81
bool isEOF() const
Test for end of file.
Definition Token.cpp:89
bool isKey(const char *key) const
Test for keyword.
Definition Token.cpp:101
Statement * while_block
virtual void execute(const Parser &)
Execute.
virtual ~WhileStatement()
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
bool getBool(const Attribute &attr)
Return logical value.