OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
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 
19 #include "OpalParser/IfStatement.h"
20 
23 #include "Attributes/Attributes.h"
24 #include "Parser/Parser.h"
25 #include "Parser/Token.h"
26 #include "Parser/TokenStream.h"
27 #include "Utilities/ParseError.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()",
68  "Invalid \"IF\" statement.");
69  }
70 }
71 
72 
74  delete then_block;
75  delete else_block;
76 }
77 
78 
79 void IfStatement::execute(const Parser &parser) {
80  start();
81  Attribute condition = Attributes::makeBool("IF()", "");
82 
83  try {
84  condition.parse(*this, false);
86 
87  if(Attributes::getBool(condition)) {
88  then_block->execute(parser);
89  } else if(else_block) {
90  else_block->execute(parser);
91  }
92  } catch(...) {
93  std::ostringstream oss;
94  this->print(oss);
95  ERRORMSG("Invalid IF condition '" + oss.str() + "'");
96  throw;
97  }
98 }
#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:674
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.
Interface for statements.
Definition: Statement.h:38
void append(const Token &)
Append a token.
Definition: Statement.cpp:45
virtual void execute(const Parser &)=0
Execute.
virtual void print(std::ostream &os) const
Print statement.
Definition: Statement.cpp:204
void start()
Return to start.
Definition: Statement.cpp:180
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
void putBack(const Token &token)
Put token back to stream.
Definition: TokenStream.cpp:38
virtual Token readToken()=0
Read single token from stream.
Parse exception.
Definition: ParseError.h:32
virtual ~IfStatement()
Definition: IfStatement.cpp:73
Statement * else_block
Definition: IfStatement.h:57
virtual void execute(const Parser &)
Execute.
Definition: IfStatement.cpp:79
Statement * then_block
Definition: IfStatement.h:56