OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
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) break;
53 }
54
55 token = is.readToken();
56 }
57
58 then_block = parser.readStatement(&is);
59 token = is.readToken();
60
61 if (!token.isEOF() && token.isKey("ELSE")) {
62 else_block = parser.readStatement(&is);
63 } else {
64 is.putBack(token);
65 }
66 } else {
67 throw ParseError("IfStatement::IfStatement()", "Invalid \"IF\" statement.");
68 }
69}
70
72 delete then_block;
73 delete else_block;
74}
75
76void IfStatement::execute(const Parser& parser) {
77 start();
78 Attribute condition = Attributes::makeBool("IF()", "");
79
80 try {
81 condition.parse(*this, false);
83
84 if (Attributes::getBool(condition)) {
85 then_block->execute(parser);
86 } else if (else_block) {
87 else_block->execute(parser);
88 }
89 } catch (...) {
90 std::ostringstream oss;
91 this->print(oss);
92 *ippl::Error << "Invalid IF condition '" + oss.str() + "'";
93 throw;
94 }
95}
A representation of an Object attribute.
Definition Attribute.h:52
void parse(Statement &stat, bool eval)
Parse attribute.
Definition Attribute.cpp:76
virtual ~IfStatement()
Statement * else_block
Definition IfStatement.h:53
virtual void execute(const Parser &)
Execute.
Statement * then_block
Definition IfStatement.h:52
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
virtual void execute(const Parser &)=0
Execute.
virtual void print(std::ostream &os) const
Print statement.
void start()
Return to start.
Abstract interface for a stream of input tokens.
Definition TokenStream.h:30
void putBack(const Token &token)
Put token back to stream.
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
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
bool getBool(const Attribute &attr)
Return logical value.