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