Hormiga 1.0

/var/www/hormigaproject.com.ar/files/src/statement/statement.cpp

Go to the documentation of this file.
00001 
00002 /*
00003 # Hormiga - Software de cálculo programable.
00004 #
00005 # Copyright (C) 2008, 2009 Leonardo Román, Hugo J. Curti, Norma  Ercoli,
00006 # Universidad Nacional del Centro de la Provincia de Buenos Aires
00007 # (UNCPBA)
00008 #
00009 # Este programa es parte de Hormiga.
00010 #
00011 # Hormiga es software libre. Puede redistribuirlo y/o modificarlo
00012 # bajo los términos de la Licencia Pública General de GNU según es
00013 # publicada por la Free Software Foundation, versión 2. Vea el archivo
00014 # COPIA (español, no oficial) o COPYING (inglés, oficial) en directorio
00015 # raíz.
00016 #
00017 # Hormiga se distribuye con la esperanza de que sea útil, pero SIN
00018 # NINGUNA GARANTÍA, incluso sin la garantía MERCANTIL implícita o sin
00019 # garantizar la CONVENIENCIA PARA UN PROPÓSITO PARTICULAR. Véase la
00020 # Licencia Pública General de GNU para más detalles.
00021 #
00022 # Debería haber recibido una copia de la Licencia Pública General junto
00023 # con Hormiga. Si no ha sido así, escriba a la Free Software
00024 # Foundation, Inc., en 675 Mass Ave, Cambridge, MA 02139, EEUU.
00025 # Añada también información sobre cómo contactar con usted mediante
00026 # correo electrónico y postal.
00027 #
00028 # This file is part of Hormiga.
00029 #
00030 # Hormiga is free software; you can redistribute it and/or modify
00031 # it under the terms of the GNU General Public License version 2
00032 # as published by the Free Software Foundation; see the file COPYING
00033 # in the top directory for details.
00034 #
00035 # Hormiga is distributed in the hope that it will be useful,
00036 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00037 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00038 # GNU General Public License for more details.
00039 #
00040 # You should have received a copy of the GNU General Public License
00041 # along with Hormiga; if not, write to the Free Software
00042 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00043 # USA
00044 */
00045 
00046 #include "statement.h"
00047 
00049 T_STRING OperatorAsig::symbol = ":=" ;
00050 
00051 T_STRING OperatorAsig::getSymbol() const
00052 {
00053     return ( OperatorAsig::symbol );
00054 }
00055 
00056 T_STRING OperatorAsig::toString() const
00057 {
00058     return ( BinaryOperator::toString() ) ;
00059 }
00060 
00061 T_MAGNITUDE OperatorAsig::getValue() const
00062 {
00063     Seteable *s = (Seteable*)(VarValue *) this->op1 ;
00064     return ( s->setValue( this->op2->getValue() ) ) ;
00065 }
00066 
00067 bool OperatorAsig::exec()
00068 {
00069     this->getValue() ;
00070     return ( true ) ;
00071 }
00072 
00073 OperatorAsig::OperatorAsig( Operand *op1 , Operand *op2 )
00074     : BinaryOperator::BinaryOperator ( op1 , op2 )
00075 {}
00076 
00077 
00079 bool Statements::exec()
00080 {
00081     for ( int i = 0; i < this->block->count() ; i++ )
00082     {
00083         Statement *o = this->block->at( i ) ;
00084         if ( ! o->exec() )
00085             return ( false ) ;
00086     }
00087     return ( true ) ;
00088 }
00089 
00090 T_STRING Statements::toString() const
00091 {
00092     T_STRING toRet = "" ;
00093     for ( int i = 0; i < this->block->count() ; i++ )
00094     {
00095         Statement *o = this->block->at( i ) ;
00096         toRet += QString("%1 %2\n")
00097                  .arg( o->toString() )
00098                  .arg( ';' ) ;
00099     }
00100     toRet += "" ;
00101     return ( toRet ) ;
00102 }
00103 
00104 Statements::Statements( T_STMT_BLOCK *block )
00105 {
00106     this->block = block ;
00107 }
00108 
00109 Statements::~Statements()
00110 {
00111     for ( int i = 0; i < this->block->count() ; i++ )
00112     {
00113         Statement *o = this->block->at( i ) ;
00114         delete o ;
00115     }
00116     delete this->block ;
00117 }
00118 
00119 
00120 
00122 bool For::exec ()
00123 {
00124     for ( T_MAGNITUDE i = this->interval->getFirst() ;
00125         i <= this->interval->getLast() ; i += this->interval->getStep() )
00126     {
00127         this->var->setValue( i ) ;
00128         this->block->exec() ;
00129     }
00130     return ( true ) ;
00131 }
00132 
00133 T_STRING For::toString() const
00134 {
00135     return QString("FOR %1 := %2 DO\n%3END FOR")
00136             .arg( this->var->getSymbol() )
00137             .arg( this->interval->toString() )
00138             .arg( this->block->toString() ) ;
00139 }
00140 
00141 For::For ( VarValue* var , Sequence *interval , Statement *block )
00142 {
00143     this->var = var ;
00144     this->interval = interval ;
00145     this->block = block ;
00146 }
00147 
00148 For::~For ()
00149 {
00150     /* Var should BE SHAREABLE.... don't delete */
00151     if ( interval != NULL ) delete interval ;
00152     if ( block != NULL ) delete block ;
00153 }
00154 
00155 
00157 bool If::exec ()
00158 {
00159     if ( this->condition->getValue() == TRUE )
00160         this->blockTrue->exec() ;
00161     else if ( this->blockFalse != NULL )
00162         this->blockFalse->exec() ;
00163     return ( true ) ;
00164 }
00165 
00166 T_STRING If::toString() const
00167 {
00168     if ( this->blockFalse != NULL )
00169         return QString("IF %1 THEN\n%2ELSE\n%3END IF")
00170             .arg( this->condition->toString() )
00171             .arg( this->blockTrue->toString() )
00172             .arg( this->blockFalse->toString() ) ;
00173     else
00174         return QString("IF %1 THEN\n%2END IF")
00175             .arg( this->condition->toString() )
00176             .arg( this->blockTrue->toString() ) ;
00177 }
00178 
00179 If::If ( Operand* condition , Statement *blockTrue , Statement *blockFalse )
00180 {
00181     this->condition = condition ;
00182     this->blockTrue = blockTrue ;
00183     this->blockFalse = blockFalse ;
00184 }
00185 
00186 If::~If ()
00187 {
00188     if ( (condition != NULL) && !condition->isShareable() ) delete condition ;
00189     if ( blockTrue != NULL ) delete blockTrue ;
00190     if ( blockFalse != NULL ) delete blockFalse ;
00191 }
00192 
00193 
00195 bool While::exec ()
00196 {
00197     while ( this->condition->getValue() == TRUE )
00198         this->blockTrue->exec() ;
00199     return ( true ) ;
00200 }
00201 
00202 T_STRING While::toString() const
00203 {
00204         return QString("WHILE %1 DO\n%2END WHILE")
00205             .arg( this->condition->toString() )
00206             .arg( this->blockTrue->toString() ) ;
00207 }
00208 
00209 While::While ( Operand* condition , Statement *blockTrue )
00210 {
00211     this->condition = condition ;
00212     this->blockTrue = blockTrue ;
00213 }
00214 
00215 While::~While ()
00216 {
00217     if ( (condition != NULL) && !condition->isShareable() ) delete condition ;
00218     if ( blockTrue != NULL ) delete blockTrue ;
00219 }