LCOV - code coverage report
Current view: top level - basic/source/comp - loops.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 182 307 59.3 %
Date: 2014-04-11 Functions: 8 13 61.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include "sbcomp.hxx"
      22             : 
      23             : // Single-line IF and Multiline IF
      24             : 
      25         374 : void SbiParser::If()
      26             : {
      27             :     sal_uInt32 nEndLbl;
      28         374 :     SbiToken eTok = NIL;
      29             :     // ignore end-tokens
      30         374 :     SbiExpression aCond( this );
      31         374 :     aCond.Gen();
      32         374 :     TestToken( THEN );
      33         374 :     if( IsEoln( Next() ) )
      34             :     {
      35             :         // At the end of each block a jump to ENDIF must be inserted,
      36             :         // so that the condition is not evaluated again at ELSEIF.
      37             :         // The table collects all jump points.
      38             : #define JMP_TABLE_SIZE 100
      39             :         sal_uInt32 pnJmpToEndLbl[JMP_TABLE_SIZE];   // 100 ELSEIFs allowed
      40         264 :         sal_uInt16 iJmp = 0;                        // current table index
      41             : 
      42             :         // multiline IF
      43         264 :         nEndLbl = aGen.Gen( _JUMPF, 0 );
      44         264 :         eTok = Peek();
      45        3168 :         while( !( eTok == ELSEIF || eTok == ELSE || eTok == ENDIF ) &&
      46        2640 :                 !bAbort && Parse() )
      47             :         {
      48         792 :             eTok = Peek();
      49         792 :             if( IsEof() )
      50             :             {
      51           0 :                 Error( SbERR_BAD_BLOCK, IF ); bAbort = true; return;
      52             :             }
      53             :         }
      54         528 :         while( eTok == ELSEIF )
      55             :         {
      56             :             // jump to ENDIF in case of a successful IF/ELSEIF
      57           0 :             if( iJmp >= JMP_TABLE_SIZE )
      58             :             {
      59           0 :                 Error( SbERR_PROG_TOO_LARGE );  bAbort = true;  return;
      60             :             }
      61           0 :             pnJmpToEndLbl[iJmp++] = aGen.Gen( _JUMP, 0 );
      62             : 
      63           0 :             Next();
      64           0 :             aGen.BackChain( nEndLbl );
      65             : 
      66           0 :             aGen.Statement();
      67           0 :             SbiExpression* pCond = new SbiExpression( this );
      68           0 :             pCond->Gen();
      69           0 :             nEndLbl = aGen.Gen( _JUMPF, 0 );
      70           0 :             delete pCond;
      71           0 :             TestToken( THEN );
      72           0 :             eTok = Peek();
      73           0 :             while( !( eTok == ELSEIF || eTok == ELSE || eTok == ENDIF ) &&
      74           0 :                     !bAbort && Parse() )
      75             :             {
      76           0 :                 eTok = Peek();
      77           0 :                 if( IsEof() )
      78             :                 {
      79           0 :                     Error( SbERR_BAD_BLOCK, ELSEIF );  bAbort = true; return;
      80             :                 }
      81             :             }
      82             :         }
      83         264 :         if( eTok == ELSE )
      84             :         {
      85         116 :             Next();
      86         116 :             sal_uInt32 nElseLbl = nEndLbl;
      87         116 :             nEndLbl = aGen.Gen( _JUMP, 0 );
      88         116 :             aGen.BackChain( nElseLbl );
      89             : 
      90         116 :             aGen.Statement();
      91         116 :             StmntBlock( ENDIF );
      92             :         }
      93         148 :         else if( eTok == ENDIF )
      94         148 :             Next();
      95             : 
      96             : 
      97         528 :         while( iJmp > 0 )
      98             :         {
      99           0 :             iJmp--;
     100           0 :             aGen.BackChain( pnJmpToEndLbl[iJmp] );
     101             :         }
     102             :     }
     103             :     else
     104             :     {
     105             :         // single line IF
     106         110 :         bSingleLineIf = true;
     107         110 :         nEndLbl = aGen.Gen( _JUMPF, 0 );
     108         110 :         Push( eCurTok );
     109         220 :         while( !bAbort )
     110             :         {
     111         110 :             if( !Parse() ) break;
     112         110 :             eTok = Peek();
     113         110 :             if( eTok == ELSE || eTok == EOLN || eTok == REM )
     114             :                 break;
     115             :         }
     116         110 :         if( eTok == ELSE )
     117             :         {
     118           0 :             Next();
     119           0 :             sal_uInt32 nElseLbl = nEndLbl;
     120           0 :             nEndLbl = aGen.Gen( _JUMP, 0 );
     121           0 :             aGen.BackChain( nElseLbl );
     122           0 :             while( !bAbort )
     123             :             {
     124           0 :                 if( !Parse() ) break;
     125           0 :                 eTok = Peek();
     126           0 :                 if( eTok == EOLN )
     127           0 :                     break;
     128             :             }
     129             :         }
     130         110 :         bSingleLineIf = false;
     131             :     }
     132         374 :     aGen.BackChain( nEndLbl );
     133             : }
     134             : 
     135             : // ELSE/ELSEIF/ENDIF without IF
     136             : 
     137           0 : void SbiParser::NoIf()
     138             : {
     139           0 :     Error( SbERR_NO_IF );
     140           0 :     StmntBlock( ENDIF );
     141           0 : }
     142             : 
     143             : // DO WHILE...LOOP
     144             : // DO ... LOOP WHILE
     145             : 
     146           1 : void SbiParser::DoLoop()
     147             : {
     148           1 :     sal_uInt32 nStartLbl = aGen.GetPC();
     149           1 :     OpenBlock( DO );
     150           1 :     SbiToken eTok = Next();
     151           1 :     if( IsEoln( eTok ) )
     152             :     {
     153             :         // DO ... LOOP [WHILE|UNTIL expr]
     154           0 :         StmntBlock( LOOP );
     155           0 :         eTok = Next();
     156           0 :         if( eTok == UNTIL || eTok == WHILE )
     157             :         {
     158           0 :             SbiExpression aExpr( this );
     159           0 :             aExpr.Gen();
     160           0 :             aGen.Gen( eTok == UNTIL ? _JUMPF : _JUMPT, nStartLbl );
     161             :         } else
     162           0 :             if (eTok == EOLN || eTok == REM)
     163           0 :                 aGen.Gen (_JUMP, nStartLbl);
     164             :             else
     165           0 :                 Error( SbERR_EXPECTED, WHILE );
     166             :     }
     167             :     else
     168             :     {
     169             :         // DO [WHILE|UNTIL expr] ... LOOP
     170           1 :         if( eTok == UNTIL || eTok == WHILE )
     171             :         {
     172           1 :             SbiExpression aCond( this );
     173           1 :             aCond.Gen();
     174             :         }
     175           1 :         sal_uInt32 nEndLbl = aGen.Gen( eTok == UNTIL ? _JUMPT : _JUMPF, 0 );
     176           1 :         StmntBlock( LOOP );
     177           1 :         TestEoln();
     178           1 :         aGen.Gen( _JUMP, nStartLbl );
     179           1 :         aGen.BackChain( nEndLbl );
     180             :     }
     181           1 :     CloseBlock();
     182           1 : }
     183             : 
     184             : // WHILE ... WEND
     185             : 
     186           0 : void SbiParser::While()
     187             : {
     188           0 :     SbiExpression aCond( this );
     189           0 :     sal_uInt32 nStartLbl = aGen.GetPC();
     190           0 :     aCond.Gen();
     191           0 :     sal_uInt32 nEndLbl = aGen.Gen( _JUMPF, 0 );
     192           0 :     StmntBlock( WEND );
     193           0 :     aGen.Gen( _JUMP, nStartLbl );
     194           0 :     aGen.BackChain( nEndLbl );
     195           0 : }
     196             : 
     197             : // FOR var = expr TO expr STEP
     198             : 
     199          40 : void SbiParser::For()
     200             : {
     201          40 :     bool bForEach = ( Peek() == EACH );
     202          40 :     if( bForEach )
     203           2 :         Next();
     204          40 :     SbiExpression aLvalue( this, SbOPERAND );
     205          40 :     aLvalue.Gen();      // variable on the Stack
     206             : 
     207          40 :     if( bForEach )
     208             :     {
     209           2 :         TestToken( _IN_ );
     210           2 :         SbiExpression aCollExpr( this, SbOPERAND );
     211           2 :         aCollExpr.Gen();    // Colletion var to for stack
     212           2 :         TestEoln();
     213           2 :         aGen.Gen( _INITFOREACH );
     214             :     }
     215             :     else
     216             :     {
     217          38 :         TestToken( EQ );
     218          38 :         SbiExpression aStartExpr( this );
     219          38 :         aStartExpr.Gen();
     220          38 :         TestToken( TO );
     221          76 :         SbiExpression aStopExpr( this );
     222          38 :         aStopExpr.Gen();
     223          38 :         if( Peek() == STEP )
     224             :         {
     225           0 :             Next();
     226           0 :             SbiExpression aStepExpr( this );
     227           0 :             aStepExpr.Gen();
     228             :         }
     229             :         else
     230             :         {
     231          38 :             SbiExpression aOne( this, 1, SbxINTEGER );
     232          38 :             aOne.Gen();
     233             :         }
     234          38 :         TestEoln();
     235             :         // The stack has all 4 elements now: variable, start, end, increment
     236             :         // bind start value
     237          76 :         aGen.Gen( _INITFOR );
     238             :     }
     239             : 
     240          40 :     sal_uInt32 nLoop = aGen.GetPC();
     241             :     // do tests, maybe free the stack
     242          40 :     sal_uInt32 nEndTarget = aGen.Gen( _TESTFOR, 0 );
     243          40 :     OpenBlock( FOR );
     244          40 :     StmntBlock( NEXT );
     245          40 :     aGen.Gen( _NEXT );
     246          40 :     aGen.Gen( _JUMP, nLoop );
     247             :     // are there variables after NEXT?
     248          40 :     if( Peek() == SYMBOL )
     249             :     {
     250          37 :         SbiExpression aVar( this, SbOPERAND );
     251          37 :         if( aVar.GetRealVar() != aLvalue.GetRealVar() )
     252           0 :             Error( SbERR_EXPECTED, aLvalue.GetRealVar()->GetName() );
     253             :     }
     254          40 :     aGen.BackChain( nEndTarget );
     255          40 :     CloseBlock();
     256          40 : }
     257             : 
     258             : // WITH .. END WITH
     259             : 
     260          14 : void SbiParser::With()
     261             : {
     262          14 :     SbiExpression aVar( this, SbOPERAND );
     263             : 
     264          14 :     SbiExprNode *pNode = aVar.GetExprNode()->GetRealNode();
     265          14 :     SbiSymDef* pDef = pNode->GetVar();
     266             :     // Variant, from 27.6.1997, #41090: empty -> must be Object
     267          14 :     if( pDef->GetType() == SbxVARIANT || pDef->GetType() == SbxEMPTY )
     268           0 :         pDef->SetType( SbxOBJECT );
     269          14 :     else if( pDef->GetType() != SbxOBJECT )
     270           0 :         Error( SbERR_NEEDS_OBJECT );
     271             : 
     272             : 
     273          14 :     pNode->SetType( SbxOBJECT );
     274             : 
     275          14 :     OpenBlock( NIL, aVar.GetExprNode() );
     276          14 :     StmntBlock( ENDWITH );
     277          14 :     CloseBlock();
     278          14 : }
     279             : 
     280             : // LOOP/NEXT/WEND without construct
     281             : 
     282           0 : void SbiParser::BadBlock()
     283             : {
     284           0 :     if( eEndTok )
     285           0 :         Error( SbERR_BAD_BLOCK, eEndTok );
     286             :     else
     287           0 :         Error( SbERR_BAD_BLOCK, "Loop/Next/Wend" );
     288           0 : }
     289             : 
     290             : // On expr Goto/Gosub n,n,n...
     291             : 
     292           0 : void SbiParser::OnGoto()
     293             : {
     294           0 :     SbiExpression aCond( this );
     295           0 :     aCond.Gen();
     296           0 :     sal_uInt32 nLabelsTarget = aGen.Gen( _ONJUMP, 0 );
     297           0 :     SbiToken eTok = Next();
     298           0 :     if( eTok != GOTO && eTok != GOSUB )
     299             :     {
     300           0 :         Error( SbERR_EXPECTED, "GoTo/GoSub" );
     301           0 :         eTok = GOTO;
     302             :     }
     303             : 
     304           0 :     sal_uInt32 nLbl = 0;
     305           0 :     do
     306             :     {
     307           0 :         Next(); // get label
     308           0 :         if( MayBeLabel() )
     309             :         {
     310           0 :             sal_uInt32 nOff = pProc->GetLabels().Reference( aSym );
     311           0 :             aGen.Gen( _JUMP, nOff );
     312           0 :             nLbl++;
     313             :         }
     314           0 :         else Error( SbERR_LABEL_EXPECTED );
     315             :     }
     316           0 :     while( !bAbort && TestComma() );
     317           0 :     if( eTok == GOSUB )
     318           0 :         nLbl |= 0x8000;
     319           0 :     aGen.Patch( nLabelsTarget, nLbl );
     320           0 : }
     321             : 
     322             : // GOTO/GOSUB
     323             : 
     324           3 : void SbiParser::Goto()
     325             : {
     326           3 :     SbiOpcode eOp = eCurTok == GOTO ? _JUMP : _GOSUB;
     327           3 :     Next();
     328           3 :     if( MayBeLabel() )
     329             :     {
     330           3 :         sal_uInt32 nOff = pProc->GetLabels().Reference( aSym );
     331           3 :         aGen.Gen( eOp, nOff );
     332             :     }
     333           0 :     else Error( SbERR_LABEL_EXPECTED );
     334           3 : }
     335             : 
     336             : // RETURN [label]
     337             : 
     338           0 : void SbiParser::Return()
     339             : {
     340           0 :     Next();
     341           0 :     if( MayBeLabel() )
     342             :     {
     343           0 :         sal_uInt32 nOff = pProc->GetLabels().Reference( aSym );
     344           0 :         aGen.Gen( _RETURN, nOff );
     345             :     }
     346           0 :     else aGen.Gen( _RETURN, 0 );
     347           0 : }
     348             : 
     349             : // SELECT CASE
     350             : 
     351           8 : void SbiParser::Select()
     352             : {
     353           8 :     TestToken( CASE );
     354           8 :     SbiExpression aCase( this );
     355           8 :     SbiToken eTok = NIL;
     356           8 :     aCase.Gen();
     357           8 :     aGen.Gen( _CASE );
     358           8 :     TestEoln();
     359           8 :     sal_uInt32 nNextTarget = 0;
     360           8 :     sal_uInt32 nDoneTarget = 0;
     361           8 :     bool bElse = false;
     362             : 
     363          81 :     while( !bAbort )
     364             :     {
     365          73 :         eTok = Next();
     366          73 :         if( eTok == CASE )
     367             :         {
     368          65 :             if( nNextTarget )
     369          57 :                 aGen.BackChain( nNextTarget ), nNextTarget = 0;
     370          65 :             aGen.Statement();
     371             : 
     372          65 :             bool bDone = false;
     373          65 :             sal_uInt32 nTrueTarget = 0;
     374          65 :             if( Peek() == ELSE )
     375             :             {
     376             :                 // CASE ELSE
     377           6 :                 Next();
     378           6 :                 bElse = true;
     379             :             }
     380         179 :             else while( !bDone )
     381             :             {
     382          61 :                 if( bElse )
     383           0 :                     Error( SbERR_SYNTAX );
     384          61 :                 SbiToken eTok2 = Peek();
     385          61 :                 if( eTok2 == IS || ( eTok2 >= EQ && eTok2 <= GE ) )
     386             :                 {   // CASE [IS] operator expr
     387           0 :                     if( eTok2 == IS )
     388           0 :                         Next();
     389           0 :                     eTok2 = Peek();
     390           0 :                     if( eTok2 < EQ || eTok2 > GE )
     391           0 :                         Error( SbERR_SYNTAX );
     392           0 :                     else Next();
     393           0 :                     SbiExpression aCompare( this );
     394           0 :                     aCompare.Gen();
     395             :                     nTrueTarget = aGen.Gen(
     396             :                         _CASEIS, nTrueTarget,
     397             :                         sal::static_int_cast< sal_uInt16 >(
     398           0 :                             SbxEQ + ( eTok2 - EQ ) ) );
     399             :                 }
     400             :                 else
     401             :                 {   // CASE expr | expr TO expr
     402          61 :                     SbiExpression aCase1( this );
     403          61 :                     aCase1.Gen();
     404          61 :                     if( Peek() == TO )
     405             :                     {
     406             :                         // CASE a TO b
     407           0 :                         Next();
     408           0 :                         SbiExpression aCase2( this );
     409           0 :                         aCase2.Gen();
     410           0 :                         nTrueTarget = aGen.Gen( _CASETO, nTrueTarget );
     411             :                     }
     412             :                     else
     413             :                         // CASE a
     414          61 :                         nTrueTarget = aGen.Gen( _CASEIS, nTrueTarget, SbxEQ );
     415             : 
     416             :                 }
     417          61 :                 if( Peek() == COMMA ) Next();
     418          59 :                 else TestEoln(), bDone = true;
     419             :             }
     420             : 
     421          65 :             if( !bElse )
     422             :             {
     423          59 :                 nNextTarget = aGen.Gen( _JUMP, nNextTarget );
     424          59 :                 aGen.BackChain( nTrueTarget );
     425             :             }
     426             :             // build the statement body
     427         206 :             while( !bAbort )
     428             :             {
     429         141 :                 eTok = Peek();
     430         141 :                 if( eTok == CASE || eTok == ENDSELECT )
     431             :                     break;
     432         141 :                 if( !Parse() ) goto done;
     433         141 :                 eTok = Peek();
     434         141 :                 if( eTok == CASE || eTok == ENDSELECT )
     435             :                     break;
     436             :             }
     437          65 :             if( !bElse )
     438          59 :                 nDoneTarget = aGen.Gen( _JUMP, nDoneTarget );
     439             :         }
     440           8 :         else if( !IsEoln( eTok ) )
     441           8 :             break;
     442             :     }
     443             : done:
     444           8 :     if( eTok != ENDSELECT )
     445           0 :         Error( SbERR_EXPECTED, ENDSELECT );
     446           8 :     if( nNextTarget )
     447           2 :         aGen.BackChain( nNextTarget );
     448           8 :     aGen.BackChain( nDoneTarget );
     449           8 :     aGen.Gen( _ENDCASE );
     450           8 : }
     451             : 
     452             : // ON Error/Variable
     453             : 
     454          52 : void SbiParser::On()
     455             : {
     456          52 :     SbiToken eTok = Peek();
     457          52 :     OUString aString = SbiTokenizer::Symbol(eTok);
     458          52 :     if (aString.equalsIgnoreAsciiCase("ERROR"))
     459             :     {
     460          46 :         eTok = _ERROR_; // Error comes as SYMBOL
     461             :     }
     462          52 :     if( eTok != _ERROR_ && eTok != LOCAL )
     463             :     {
     464           0 :         OnGoto();
     465             :     }
     466             :     else
     467             :     {
     468          52 :         if( eTok == LOCAL )
     469             :         {
     470           6 :             Next();
     471             :         }
     472          52 :         Next (); // no more TestToken, as there'd be an error otherwise
     473             : 
     474          52 :         Next(); // get token after error
     475          52 :         if( eCurTok == GOTO )
     476             :         {
     477             :             // ON ERROR GOTO label|0
     478          50 :             Next();
     479          50 :             bool bError_ = false;
     480          50 :             if( MayBeLabel() )
     481             :             {
     482          50 :                 if( eCurTok == NUMBER && !nVal )
     483             :                 {
     484           1 :                     aGen.Gen( _STDERROR );
     485             :                 }
     486             :                 else
     487             :                 {
     488          49 :                     sal_uInt32 nOff = pProc->GetLabels().Reference( aSym );
     489          49 :                     aGen.Gen( _ERRHDL, nOff );
     490             :                 }
     491             :             }
     492           0 :             else if( eCurTok == MINUS )
     493             :             {
     494           0 :                 Next();
     495           0 :                 if( eCurTok == NUMBER && nVal == 1 )
     496             :                 {
     497           0 :                     aGen.Gen( _STDERROR );
     498             :                 }
     499             :                 else
     500             :                 {
     501           0 :                     bError_ = true;
     502             :                 }
     503             :             }
     504          50 :             if( bError_ )
     505             :             {
     506           0 :                 Error( SbERR_LABEL_EXPECTED );
     507             :             }
     508             :         }
     509           2 :         else if( eCurTok == RESUME )
     510             :         {
     511           2 :             TestToken( NEXT );
     512           2 :             aGen.Gen( _NOERROR );
     513             :         }
     514           0 :         else Error( SbERR_EXPECTED, "GoTo/Resume" );
     515          52 :     }
     516          52 : }
     517             : 
     518             : // RESUME [0]|NEXT|label
     519             : 
     520           4 : void SbiParser::Resume()
     521             : {
     522             :     sal_uInt32 nLbl;
     523             : 
     524           4 :     switch( Next() )
     525             :     {
     526             :         case EOS:
     527             :         case EOLN:
     528           0 :             aGen.Gen( _RESUME, 0 );
     529           0 :             break;
     530             :         case NEXT:
     531           4 :             aGen.Gen( _RESUME, 1 );
     532           4 :             Next();
     533           4 :             break;
     534             :         case NUMBER:
     535           0 :             if( !nVal )
     536             :             {
     537           0 :                 aGen.Gen( _RESUME, 0 );
     538           0 :                 break;
     539             :             } // fall thru
     540             :         case SYMBOL:
     541           0 :             if( MayBeLabel() )
     542             :             {
     543           0 :                 nLbl = pProc->GetLabels().Reference( aSym );
     544           0 :                 aGen.Gen( _RESUME, nLbl );
     545           0 :                 Next();
     546           0 :                 break;
     547             :             } // fall thru
     548             :         default:
     549           0 :             Error( SbERR_LABEL_EXPECTED );
     550             :     }
     551           4 : }
     552             : 
     553             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10