OPALX (Object Oriented Parallel Accelerator Library for Exascal) MINIorX
OPALX
IfStatement.cpp
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2// $RCSfile: IfStatement.cpp,v $
3// ------------------------------------------------------------------------
4// $Revision: 1.1.1.1 $
5// ------------------------------------------------------------------------
6// Copyright: see Copyright.readme
7// ------------------------------------------------------------------------
8//
9// Class: IfStatement
10// Representation for OPAL IF statements.
11//
12// ------------------------------------------------------------------------
13//
14// $Date: 2000/03/27 09:33:43 $
15// $Author: Andreas Adelmann $
16//
17// ------------------------------------------------------------------------
18
20
24#include "OpalParser/Parser.h"
25#include "OpalParser/Token.h"
28
29#include "Utility/IpplInfo.h"
30
31// class IfStatement
32// Statement of the form "IF ( <condition> ) <statement>".
33// ------------------------------------------------------------------------
34
36 : Statement("", 0), then_block(0), else_block(0) {
37 Token key = is.readToken();
38 Token token = is.readToken();
39
40 if (key.isKey("IF") && token.isDel('(')) {
41 append(token);
42 token = is.readToken();
43 int level = 1;
44
45 while (!token.isEOF()) {
46 append(token);
47
48 if (token.isDel('(')) {
49 level++;
50 } else if (token.isDel(')')) {
51 level--;
52 if (level == 0)
53 break;
54 }
55
56 token = is.readToken();
57 }
58
59 then_block = parser.readStatement(&is);
60 token = is.readToken();
61
62 if (!token.isEOF() && token.isKey("ELSE")) {
63 else_block = parser.readStatement(&is);
64 } else {
65 is.putBack(token);
66 }
67 } else {
68 throw ParseError("IfStatement::IfStatement()", "Invalid \"IF\" statement.");
69 }
70}
71
73 delete then_block;
74 delete else_block;
75}
76
77void IfStatement::execute(const Parser& parser) {
78 start();
79 Attribute condition = Attributes::makeBool("IF()", "");
80
81 try {
82 condition.parse(*this, false);
84
85 if (Attributes::getBool(condition)) {
86 then_block->execute(parser);
87 } else if (else_block) {
88 else_block->execute(parser);
89 }
90 } catch (...) {
91 std::ostringstream oss;
92 this->print(oss);
93 *ippl::Error << "Invalid IF condition '" + oss.str() + "'";
94 throw;
95 }
96}
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
virtual ~IfStatement()
Statement * else_block
Definition IfStatement.h:57
virtual void execute(const Parser &)
Execute.
Statement * then_block
Definition IfStatement.h:56
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
virtual void print(std::ostream &os) const
Print statement.
void start()
Return to start.
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