OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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 
30 // class IfStatement
31 // Statement of the form "IF ( <condition> ) <statement>".
32 // ------------------------------------------------------------------------
33 
35  Statement("", 0), then_block(0), else_block(0) {
36  Token key = is.readToken();
37  Token token = is.readToken();
38 
39  if(key.isKey("IF") && token.isDel('(')) {
40  append(token);
41  token = is.readToken();
42  int level = 1;
43 
44  while(! token.isEOF()) {
45  append(token);
46 
47  if(token.isDel('(')) {
48  level++;
49  } else if(token.isDel(')')) {
50  level--;
51  if(level == 0) break;
52  }
53 
54  token = is.readToken();
55  }
56 
57  then_block = parser.readStatement(&is);
58  token = is.readToken();
59 
60  if(! token.isEOF() && token.isKey("ELSE")) {
61  else_block = parser.readStatement(&is);
62  } else {
63  is.putBack(token);
64  }
65  } else {
66  throw ParseError("IfStatement::IfStatement()",
67  "Invalid \"IF\" statement.");
68  }
69 }
70 
71 
73  delete then_block;
74  delete else_block;
75 }
76 
77 
78 void IfStatement::execute(const Parser &parser) {
79  start();
80  Attribute condition = Attributes::makeBool("IF()", "");
81 
82  try {
83  condition.parse(*this, false);
85 
86  if(Attributes::getBool(condition)) {
87  then_block->execute(parser);
88  } else if(else_block) {
89  else_block->execute(parser);
90  }
91  } catch(...) {
92  std::ostringstream oss;
93  this->print(oss);
94  ERRORMSG("Invalid IF condition '" + oss.str() + "'");
95  throw;
96  }
97 }
bool isEOF() const
Test for end of file.
Definition: Token.cpp:107
Parse exception.
Definition: ParseError.h:32
virtual void execute(const Parser &)
Execute.
Definition: IfStatement.cpp:78
Abstract interface for a stream of input tokens.
Definition: TokenStream.h:33
#define ERRORMSG(msg)
Definition: IpplInfo.h:399
void update()
Update all objects.
Definition: OpalData.cpp:723
virtual Statement * readStatement(TokenStream *ts) const =0
Read complete statement from token stream.
bool getBool(const Attribute &attr)
Return logical value.
Definition: Attributes.cpp:66
Interface for abstract language parser.
Definition: Parser.h:31
static OpalData * getInstance()
Definition: OpalData.cpp:209
virtual Token readToken()=0
Read single token from stream.
A representation of an Object attribute.
Definition: Attribute.h:55
Interface for statements.
Definition: Statement.h:38
Representation of a single input token.
Definition: Token.h:33
virtual ~IfStatement()
Definition: IfStatement.cpp:72
void start()
Return to start.
Definition: Statement.cpp:182
virtual void print(std::ostream &os) const
Print statement.
Definition: Statement.cpp:206
Statement * else_block
Definition: IfStatement.h:57
void putBack(const Token &token)
Put token back to stream.
Definition: TokenStream.cpp:38
void append(const Token &)
Append a token.
Definition: Statement.cpp:47
Statement * then_block
Definition: IfStatement.h:56
virtual void execute(const Parser &)=0
Execute.
Attribute makeBool(const std::string &name, const std::string &help)
Make logical attribute.
Definition: Attributes.cpp:56
bool isKey(const char *key) const
Test for keyword.
Definition: Token.cpp:137
bool isDel(char del) const
Test for delimiter.
Definition: Token.cpp:92
void parse(Statement &stat, bool eval)
Parse attribute.
Definition: Attribute.cpp:125