| 1 | #include <functional>
|
|---|
| 2 | #include <algorithm>
|
|---|
| 3 | #include <iostream>
|
|---|
| 4 | #include <list>
|
|---|
| 5 | #include <cassert>
|
|---|
| 6 |
|
|---|
| 7 | #include "Statement.h"
|
|---|
| 8 | #include "Expression.h"
|
|---|
| 9 | #include "Declaration.h"
|
|---|
| 10 | #include "Common/SemanticError.h"
|
|---|
| 11 |
|
|---|
| 12 | using std::string;
|
|---|
| 13 | using std::endl;
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | // *** Statement
|
|---|
| 17 | Statement::Statement(std::list<Label> _labels):
|
|---|
| 18 | labels(_labels) {}
|
|---|
| 19 |
|
|---|
| 20 | void Statement::print(std::ostream &, int indent) {}
|
|---|
| 21 |
|
|---|
| 22 | Statement::~Statement() {}
|
|---|
| 23 |
|
|---|
| 24 | //*** ExprStmt
|
|---|
| 25 | ExprStmt::ExprStmt( std::list<Label> _labels, Expression *_expr ):
|
|---|
| 26 | Statement(_labels), expr(_expr) {}
|
|---|
| 27 |
|
|---|
| 28 | ExprStmt::~ExprStmt() {}
|
|---|
| 29 |
|
|---|
| 30 | void ExprStmt::print( std::ostream &os, int indent ) {
|
|---|
| 31 | os << "\r" << string(indent, ' ') << "Expression Statement:" << endl;
|
|---|
| 32 | expr->print(os, indent + 2);
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | //*** BranchStmt
|
|---|
| 36 | const char *BranchStmt::brType[] = { "Goto", "Break", "Continue" };
|
|---|
| 37 |
|
|---|
| 38 | BranchStmt::BranchStmt( std::list<Label> labels, Label _target, Type _type )
|
|---|
| 39 | throw (SemanticError) :
|
|---|
| 40 | Statement(labels), target(_target), type(_type)
|
|---|
| 41 | {
|
|---|
| 42 | //actually this is a syntactic error signaled by the parser
|
|---|
| 43 | if(type == BranchStmt::Goto && target.size() == 0)
|
|---|
| 44 | throw SemanticError("goto without target");
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | BranchStmt::BranchStmt( std::list<Label> labels, Expression *_computedTarget, Type _type)
|
|---|
| 48 | throw (SemanticError) :
|
|---|
| 49 | Statement(labels), computedTarget(_computedTarget), type(_type)
|
|---|
| 50 | {
|
|---|
| 51 | if(type != BranchStmt::Goto || computedTarget == 0)
|
|---|
| 52 | throw SemanticError("Computed target not valid in branch statement");
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | void BranchStmt::print( std::ostream &os, int indent ){
|
|---|
| 56 | os << "\r" << string(indent, ' ') << "Branch (" << brType[type] << ")" << endl ;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | //*** ReturnStmt
|
|---|
| 60 | ReturnStmt::ReturnStmt( std::list<Label> labels, Expression *_expr, bool throwP ) :
|
|---|
| 61 | Statement( labels ), expr( _expr ), isThrow( throwP ) {}
|
|---|
| 62 |
|
|---|
| 63 | ReturnStmt::~ReturnStmt() {
|
|---|
| 64 | delete expr;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | void ReturnStmt::print( std::ostream &os, int indent ){
|
|---|
| 68 | os << "\r" << std::string(indent, ' ') << string ( isThrow? "Throw":"Return" ) << " Statement, returning: ";
|
|---|
| 69 | if(expr != 0) expr->print(os);
|
|---|
| 70 | os << endl;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | // *** IfStmt
|
|---|
| 75 | IfStmt::IfStmt( std::list<Label> _labels, Expression *_condition, Statement *_thenPart, Statement *_elsePart ):
|
|---|
| 76 | Statement(_labels), condition(_condition), thenPart(_thenPart), elsePart(_elsePart) {}
|
|---|
| 77 |
|
|---|
| 78 | IfStmt::~IfStmt() {}
|
|---|
| 79 |
|
|---|
| 80 | void IfStmt::print( std::ostream &os, int indent ){
|
|---|
| 81 | os << "\r" << string(indent, ' ') << "If on condition: " << endl ;
|
|---|
| 82 | condition->print(os, indent + 4);
|
|---|
| 83 |
|
|---|
| 84 | os << string(indent, ' ') << ".... and branches: " << endl;
|
|---|
| 85 |
|
|---|
| 86 | thenPart->print(os, indent + 4);
|
|---|
| 87 |
|
|---|
| 88 | if(elsePart != 0){
|
|---|
| 89 | elsePart->print(os, indent + 4);
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | // *** SwitchStmt
|
|---|
| 94 | SwitchStmt::SwitchStmt(std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches):
|
|---|
| 95 | Statement(_labels), condition(_condition), branches(_branches)
|
|---|
| 96 | { }
|
|---|
| 97 |
|
|---|
| 98 | SwitchStmt::~SwitchStmt() {
|
|---|
| 99 | delete condition;
|
|---|
| 100 | // destroy branches
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | void SwitchStmt::add_case(CaseStmt *c) {}
|
|---|
| 104 |
|
|---|
| 105 | void SwitchStmt::print(std::ostream &os, int indent) {
|
|---|
| 106 | os << "\r" << string(indent, ' ') << "Switch on condition: ";
|
|---|
| 107 | condition->print(os);
|
|---|
| 108 | os << endl;
|
|---|
| 109 |
|
|---|
| 110 | // branches
|
|---|
| 111 | std::list<Statement *>::iterator i;
|
|---|
| 112 | for(i = branches.begin(); i != branches.end(); i++)
|
|---|
| 113 | (*i)->print(os, indent + 4);
|
|---|
| 114 |
|
|---|
| 115 | //for_each(branches.begin(), branches.end(), mem_fun(bind1st(&Statement::print), os));
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | // *** CaseStmt
|
|---|
| 119 | CaseStmt::CaseStmt( std::list<Label> _labels, Expression *_condition,
|
|---|
| 120 | std::list<Statement *> &_statements, bool deflt )
|
|---|
| 121 | throw (SemanticError) :
|
|---|
| 122 | Statement(_labels), condition(_condition), stmts(_statements), _isDefault(deflt)
|
|---|
| 123 | {
|
|---|
| 124 | if(isDefault() && condition != 0)
|
|---|
| 125 | throw SemanticError("default with conditions");
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | CaseStmt::~CaseStmt() {
|
|---|
| 129 | delete condition;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | void CaseStmt::print(std::ostream &os, int indent) {
|
|---|
| 133 | os << "\r" << string(indent, ' ');
|
|---|
| 134 |
|
|---|
| 135 | if(isDefault())
|
|---|
| 136 | os << "Default ";
|
|---|
| 137 | else {
|
|---|
| 138 | os << "Case ";
|
|---|
| 139 | condition->print(os);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | os << endl;
|
|---|
| 143 |
|
|---|
| 144 | std::list<Statement *>::iterator i;
|
|---|
| 145 | for(i = stmts.begin(); i != stmts.end(); i++)
|
|---|
| 146 | (*i)->print(os, indent + 4);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | //*** ChooseStmt
|
|---|
| 150 | //ChooseStmt::ChooseStmt( std::list<Label> labels, Expression *condition, Statement *body ) {}
|
|---|
| 151 | ChooseStmt::ChooseStmt(std::list<Label> _labels, Expression * _condition, std::list<Statement *> &_branches):
|
|---|
| 152 | Statement(_labels), condition(_condition), branches(_branches)
|
|---|
| 153 | { }
|
|---|
| 154 |
|
|---|
| 155 | ChooseStmt::~ChooseStmt() {
|
|---|
| 156 | delete condition;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | void ChooseStmt::add_case(CaseStmt *c) {}
|
|---|
| 160 |
|
|---|
| 161 | void ChooseStmt::print(std::ostream &os, int indent) {
|
|---|
| 162 | os << "\r" << string(indent, ' ') << "Choose on condition: ";
|
|---|
| 163 | condition->print(os);
|
|---|
| 164 | os << endl;
|
|---|
| 165 |
|
|---|
| 166 | // branches
|
|---|
| 167 | std::list<Statement *>::iterator i;
|
|---|
| 168 | for(i = branches.begin(); i != branches.end(); i++)
|
|---|
| 169 | (*i)->print(os, indent + 4);
|
|---|
| 170 |
|
|---|
| 171 | //for_each(branches.begin(), branches.end(), mem_fun(bind1st(&Statement::print), os));
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | //*** FallthruStmt
|
|---|
| 175 | void FallthruStmt::print(std::ostream &os, int indent) {
|
|---|
| 176 | os << "\r" << string(indent, ' ') << "Fall-through statement" << endl;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | //*** WhileStmt
|
|---|
| 180 | WhileStmt::WhileStmt( std::list<Label> labels, Expression *condition_,
|
|---|
| 181 | Statement *body_, bool isDoWhile_ ):
|
|---|
| 182 | Statement( labels ), condition(condition_), body(body_), isDoWhile(isDoWhile_)
|
|---|
| 183 | {}
|
|---|
| 184 |
|
|---|
| 185 | WhileStmt::~WhileStmt(){
|
|---|
| 186 | delete body;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | void WhileStmt::print( std::ostream &os, int indent ){
|
|---|
| 190 | os << "\r" << string(indent, ' ') << "While on condition: " << endl ;
|
|---|
| 191 | condition->print(os, indent + 4);
|
|---|
| 192 |
|
|---|
| 193 | os << string(indent, ' ') << ".... with body: " << endl;
|
|---|
| 194 |
|
|---|
| 195 | if(body != 0) body->print(os, indent + 4);
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | //*** ForStmt
|
|---|
| 199 | ForStmt::ForStmt( std::list<Label> labels, Statement *initialization_,
|
|---|
| 200 | Expression *condition_, Expression *increment_, Statement *body_ ):
|
|---|
| 201 | Statement( labels ), initialization( initialization_ ),
|
|---|
| 202 | condition( condition_ ), increment( increment_ ), body( body_ )
|
|---|
| 203 | { }
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 | ForStmt::~ForStmt() {
|
|---|
| 207 | delete initialization;
|
|---|
| 208 | delete condition;
|
|---|
| 209 | delete increment;
|
|---|
| 210 | delete body;
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | void ForStmt::print( std::ostream &os, int indent ){
|
|---|
| 214 | os << "\r" << string(indent, ' ') << "For Statement" << endl ;
|
|---|
| 215 |
|
|---|
| 216 | os << "\r" << string(indent + 2, ' ') << "initialization: \n";
|
|---|
| 217 | if (initialization != 0)
|
|---|
| 218 | initialization->print(os, indent + 4);
|
|---|
| 219 |
|
|---|
| 220 | os << "\n\r" << string(indent + 2, ' ') << "condition: \n";
|
|---|
| 221 | if (condition != 0)
|
|---|
| 222 | condition->print(os, indent + 4);
|
|---|
| 223 |
|
|---|
| 224 | os << "\n\r" << string(indent + 2, ' ') << "increment: \n";
|
|---|
| 225 | if (increment != 0)
|
|---|
| 226 | increment->print(os, indent + 4);
|
|---|
| 227 |
|
|---|
| 228 | os << "\n\r" << string(indent + 2, ' ') << "statement block: \n";
|
|---|
| 229 | if(body != 0)
|
|---|
| 230 | body->print(os, indent + 4);
|
|---|
| 231 |
|
|---|
| 232 | os << endl;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | //*** TryStmt
|
|---|
| 236 | TryStmt::TryStmt( std::list<Label> labels, CompoundStmt *tryBlock, std::list<Statement *> &_handlers, FinallyStmt *_finallyBlock ) :
|
|---|
| 237 | Statement( labels ), block( tryBlock ), handlers( _handlers ), finallyBlock( _finallyBlock )
|
|---|
| 238 | { }
|
|---|
| 239 |
|
|---|
| 240 | TryStmt::TryStmt( const TryStmt &other) : Statement( other.labels ) {
|
|---|
| 241 | block = other.block;
|
|---|
| 242 | std::copy( other.handlers.begin(), other.handlers.end(), back_inserter(handlers) );
|
|---|
| 243 | finallyBlock = other.finallyBlock;
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | TryStmt::~TryStmt(){
|
|---|
| 247 | delete block;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | void TryStmt::print( std::ostream &os, int indent ) {
|
|---|
| 251 | os << "\r" << string(indent, ' ') << "Try Statement" << endl;
|
|---|
| 252 | os << string(indent + 2, ' ') << "with block: " << endl;
|
|---|
| 253 | block->print(os, indent + 4);
|
|---|
| 254 |
|
|---|
| 255 | // handlers
|
|---|
| 256 | os << string(indent + 2, ' ') << "and handlers: " << endl;
|
|---|
| 257 | std::list<Statement *>::iterator i;
|
|---|
| 258 | for(i = handlers.begin(); i != handlers.end(); i++)
|
|---|
| 259 | (*i)->print(os, indent + 4);
|
|---|
| 260 |
|
|---|
| 261 | // finally block
|
|---|
| 262 | if ( finallyBlock != 0 ) {
|
|---|
| 263 | os << string(indent + 2, ' ') << "Finally block: " << endl;
|
|---|
| 264 | finallyBlock->print(os, indent + 4 );
|
|---|
| 265 | }
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | //*** CatchStmt
|
|---|
| 269 | CatchStmt::CatchStmt( std::list<Label> labels, Declaration *_decl, Statement *_body, bool isCatchRest ) :
|
|---|
| 270 | Statement( labels ), decl ( _decl ), body( _body ), catchRest ( isCatchRest )
|
|---|
| 271 | { }
|
|---|
| 272 |
|
|---|
| 273 | CatchStmt::~CatchStmt(){
|
|---|
| 274 | delete decl;
|
|---|
| 275 | delete body;
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | void CatchStmt::print( std::ostream &os, int indent ) {
|
|---|
| 279 | os << "\r" << string(indent, ' ') << "Catch Statement" << endl;
|
|---|
| 280 |
|
|---|
| 281 | os << "\r" << string(indent, ' ') << "... catching" << endl;
|
|---|
| 282 | if( decl ) {
|
|---|
| 283 | decl->printShort( os, indent + 4 );
|
|---|
| 284 | os << endl;
|
|---|
| 285 | } else if ( catchRest )
|
|---|
| 286 | os << "\r" << string(indent + 4 , ' ') << "the rest" << endl;
|
|---|
| 287 | else
|
|---|
| 288 | os << "\r" << string(indent + 4 , ' ') << ">>> Error: this catch clause must have a declaration <<<" << endl;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 | //*** FinallyStmt
|
|---|
| 293 | FinallyStmt::FinallyStmt( std::list<Label> labels, CompoundStmt *_block ) :
|
|---|
| 294 | Statement( labels ), block( _block )
|
|---|
| 295 | {
|
|---|
| 296 | assert( labels.empty() ); // finally statement cannot be labeled
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | FinallyStmt::~FinallyStmt(){
|
|---|
| 300 | delete block;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | void FinallyStmt::print( std::ostream &os, int indent ) {
|
|---|
| 304 | os << "\r" << string(indent, ' ') << "Finally Statement" << endl;
|
|---|
| 305 | os << string(indent + 2, ' ') << "with block: " << endl;
|
|---|
| 306 | block->print(os, indent + 4);
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | //*** NullStmt
|
|---|
| 310 | NullStmt::NullStmt( std::list<Label> labels ) : CompoundStmt( labels ) {}
|
|---|
| 311 | NullStmt::NullStmt() : CompoundStmt( std::list<Label>() ) {}
|
|---|
| 312 | NullStmt::~NullStmt() {}
|
|---|
| 313 |
|
|---|
| 314 | void NullStmt::print( std::ostream &os, int indent ) {
|
|---|
| 315 | os << "\r" << string(indent, ' ') << "Null Statement" << endl ;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 | /*
|
|---|
| 321 | Local Variables:
|
|---|
| 322 | compile-command: "cd ..; gmake"
|
|---|
| 323 | End:
|
|---|
| 324 | */
|
|---|