Debian Codegraph @

basic/source/comp/loops.cxx

raw
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 <parser.hxx>
22 #include <memory>
23
24 #include <basic/sberrors.hxx>
25 #include <basic/sbmod.hxx>
26
27 #include <rtl/ustrbuf.hxx>
28
29 static bool EndsIfBranch(SbiToken eTok)
30 {
31 return eTok == ELSEIF || eTok == ELSE || eTok == ENDIF;
32 }
33
34 // Single-line IF and Multiline IF
35
36 void SbiParser::If()
37 {
38 sal_uInt32 nEndLbl;
39 SbiToken eTok = NIL;
40 // ignore end-tokens
41 SbiExpression aCond( this );
42 aCond.Gen();
43 TestToken( THEN );
44 if( IsEoln( Next() ) )
45 {
46 // At the end of each block a jump to ENDIF must be inserted,
47 // so that the condition is not evaluated again at ELSEIF.
48 // The table collects all jump points.
49 constexpr sal_uInt16 JMP_TABLE_SIZE = 100;
50 sal_uInt32 pnJmpToEndLbl[JMP_TABLE_SIZE]; // 100 ELSEIFs allowed
51 sal_uInt16 iJmp = 0; // current table index
52
53 // multiline IF
54 nEndLbl = aGen.Gen( SbiOpcode::JUMPF_, 0 );
55 eTok = Peek();
56 while (!EndsIfBranch(eTok) && !bAbort && Parse())
57 {
58 eTok = Peek();
59 if( IsEof() )
60 {
61 Error( ERRCODE_BASIC_BAD_BLOCK, ENDIF ); bAbort = true; return;
62 }
63 }
64 while( eTok == ELSEIF )
65 {
66 // jump to ENDIF in case of a successful IF/ELSEIF
67 if( iJmp >= JMP_TABLE_SIZE )
68 {
69 Error( ERRCODE_BASIC_PROG_TOO_LARGE ); bAbort = true; return;
70 }
71 pnJmpToEndLbl[iJmp++] = aGen.Gen( SbiOpcode::JUMP_, 0 );
72
73 Next();
74 aGen.BackChain( nEndLbl );
75
76 aGen.Statement();
77 auto pCond = std::make_unique<SbiExpression>( this );
78 pCond->Gen();
79 nEndLbl = aGen.Gen( SbiOpcode::JUMPF_, 0 );
80 pCond.reset();
81 TestToken( THEN );
82 eTok = Peek();
83 while (!EndsIfBranch(eTok) && !bAbort && Parse())
84 {
85 eTok = Peek();
86 if( IsEof() )
87 {
88 Error( ERRCODE_BASIC_BAD_BLOCK, ENDIF ); bAbort = true; return;
89 }
90 }
91 }
92 if( eTok == ELSE )
93 {
94 Next();
95 sal_uInt32 nElseLbl = nEndLbl;
96 nEndLbl = aGen.Gen( SbiOpcode::JUMP_, 0 );
97 aGen.BackChain( nElseLbl );
98
99 aGen.Statement();
100 StmntBlock( ENDIF );
101 }
102 else if( eTok == ENDIF )
103 Next();
104 else
105 {
106 Error(ERRCODE_BASIC_BAD_BLOCK, ENDIF);
107 bAbort = true;
108 return;
109 }
110
111 while( iJmp > 0 )
112 {
113 iJmp--;
114 aGen.BackChain( pnJmpToEndLbl[iJmp] );
115 }
116 }
117 else
118 {
119 // single line IF
120 bSingleLineIf = true;
121 nEndLbl = aGen.Gen( SbiOpcode::JUMPF_, 0 );
122 Push( eCurTok );
123 // tdf#128263: update push positions to correctly restore in Next()
124 nPLine = nLine;
125 nPCol1 = nCol1;
126 nPCol2 = nCol2;
127
128 while( !bAbort )
129 {
130 if( !Parse() ) break;
131 eTok = Peek();
132 if( eTok == ELSE || eTok == EOLN || eTok == REM )
133 break;
134 }
135 if( eTok == ELSE )
136 {
137 Next();
138 sal_uInt32 nElseLbl = nEndLbl;
139 nEndLbl = aGen.Gen( SbiOpcode::JUMP_, 0 );
140 aGen.BackChain( nElseLbl );
141 while( !bAbort )
142 {
143 if( !Parse() ) break;
144 eTok = Peek();
145 if( eTok == EOLN || eTok == REM )
146 break;
147 }
148 }
149 bSingleLineIf = false;
150 }
151 aGen.BackChain( nEndLbl );
152 }
153
154 // ELSE/ELSEIF/ENDIF without IF
155
156 void SbiParser::NoIf()
157 {
158 Error( ERRCODE_BASIC_NO_IF );
159 StmntBlock( ENDIF );
160 }
161
162 // DO WHILE...LOOP
163 // DO ... LOOP WHILE
164
165 void SbiParser::DoLoop()
166 {
167 sal_uInt32 nStartLbl = aGen.GetPC();
168 OpenBlock( DO );
169 SbiToken eTok = Next();
170 if( IsEoln( eTok ) )
171 {
172 // DO ... LOOP [WHILE|UNTIL expr]
173 StmntBlock( LOOP );
174 eTok = Next();
175 if( eTok == UNTIL || eTok == WHILE )
176 {
177 SbiExpression aExpr( this );
178 aExpr.Gen();
179 aGen.Gen( eTok == UNTIL ? SbiOpcode::JUMPF_ : SbiOpcode::JUMPT_, nStartLbl );
180 } else
181 if (eTok == EOLN || eTok == REM)
182 aGen.Gen (SbiOpcode::JUMP_, nStartLbl);
183 else
184 Error( ERRCODE_BASIC_EXPECTED, WHILE );
185 }
186 else
187 {
188 // DO [WHILE|UNTIL expr] ... LOOP
189 if( eTok == UNTIL || eTok == WHILE )
190 {
191 SbiExpression aCond( this );
192 aCond.Gen();
193 }
194 sal_uInt32 nEndLbl = aGen.Gen( eTok == UNTIL ? SbiOpcode::JUMPT_ : SbiOpcode::JUMPF_, 0 );
195 StmntBlock( LOOP );
196 TestEoln();
197 aGen.Gen( SbiOpcode::JUMP_, nStartLbl );
198 aGen.BackChain( nEndLbl );
199 }
200 CloseBlock();
201 }
202
203 // WHILE ... WEND
204
205 void SbiParser::While()
206 {
207 SbiExpression aCond( this );
208 sal_uInt32 nStartLbl = aGen.GetPC();
209 aCond.Gen();
210 sal_uInt32 nEndLbl = aGen.Gen( SbiOpcode::JUMPF_, 0 );
211 StmntBlock( WEND );
212 aGen.Gen( SbiOpcode::JUMP_, nStartLbl );
213 aGen.BackChain( nEndLbl );
214 }
215
216 // FOR var = expr TO expr STEP
217
218 void SbiParser::For()
219 {
220 bool bForEach = ( Peek() == EACH );
221 if( bForEach )
222 Next();
223 SbiExpression aLvalue( this, SbOPERAND );
224 if (!aLvalue.IsVariable())
225 {
226 bAbort = true;
227 return; // the error is already set in SbiExpression ctor
228 }
229 aLvalue.Gen(); // variable on the Stack
230
231 if( bForEach )
232 {
233 TestToken( IN_ );
234 SbiExpression aCollExpr( this, SbOPERAND );
235 aCollExpr.Gen(); // Collection var to for stack
236 TestEoln();
237 aGen.Gen( SbiOpcode::INITFOREACH_ );
238 }
239 else
240 {
241 TestToken( EQ );
242 SbiExpression aStartExpr( this );
243 aStartExpr.Gen();
244 TestToken( TO );
245 SbiExpression aStopExpr( this );
246 aStopExpr.Gen();
247 if( Peek() == STEP )
248 {
249 Next();
250 SbiExpression aStepExpr( this );
251 aStepExpr.Gen();
252 }
253 else
254 {
255 SbiExpression aOne( this, 1, SbxINTEGER );
256 aOne.Gen();
257 }
258 TestEoln();
259 // The stack has all 4 elements now: variable, start, end, increment
260 // bind start value
261 aGen.Gen( SbiOpcode::INITFOR_ );
262 }
263
264 sal_uInt32 nLoop = aGen.GetPC();
265 // do tests, maybe free the stack
266 sal_uInt32 nEndTarget = aGen.Gen( SbiOpcode::TESTFOR_, 0 );
267 OpenBlock( FOR );
268 StmntBlock( NEXT );
269 aGen.Gen( SbiOpcode::NEXT_ );
270 aGen.Gen( SbiOpcode::JUMP_, nLoop );
271 // are there variables after NEXT?
272 if( Peek() == SYMBOL )
273 {
274 SbiExpression aVar( this, SbOPERAND );
275 if( aVar.GetRealVar() != aLvalue.GetRealVar() )
276 Error( ERRCODE_BASIC_EXPECTED, aLvalue.GetRealVar()->GetName() );
277 }
278 aGen.BackChain( nEndTarget );
279 CloseBlock();
280 }
281
282 // WITH .. END WITH
283
284 namespace
285 {
286 // Generate a '{_with_library.module_offset} = rVar'
287 // Use the {_with_library.module_offset} in OpenBlock
288 // The name of the variable can't be used by user: a name like [{_with_library.module_offset}]
289 // is valid, but not without the square brackets
290 struct WithLocalVar
291 {
292 WithLocalVar(SbiParser& rParser, SbiExpression& rVar)
293 : m_rParser(rParser)
294 , m_aWithParent(createLocalVar(rParser))
295 {
296 // Assignment
297 m_aWithParent.Gen();
298 rVar.Gen();
299 m_rParser.aGen.Gen(SbiOpcode::PUTC_);
300 }
301
302 ~WithLocalVar()
303 {
304 // {_with_library.module_offset} = Nothing
305 m_aWithParent.Gen();
306 m_rParser.aGen.Gen(SbiOpcode::RTL_, m_rParser.aGblStrings.Add(u"Nothing"_ustr), SbxOBJECT);
307 m_rParser.aGen.Gen(SbiOpcode::PUTC_);
308 }
309
310 static SbiExpression createLocalVar(SbiParser& rParser)
311 {
312 // Create the unique name
313 OUStringBuffer moduleName(rParser.aGen.GetModule().GetName());
314 for (auto parent = rParser.aGen.GetModule().GetParent(); parent;
315 parent = parent->GetParent())
316 moduleName.insert(0, parent->GetName() + ".");
317
318 OUString uniqueName
319 = "{_with_" + moduleName + "_" + OUString::number(rParser.aGen.GetOffset()) + "}";
320 while (rParser.pPool->Find(uniqueName) != nullptr)
321 {
322 static sal_Int64 unique_suffix;
323 uniqueName = "{_with_" + moduleName + "_" + OUString::number(rParser.aGen.GetOffset())
324 + "_" + OUString::number(unique_suffix++) + "}";
325 }
326 SbiSymDef* pWithParentDef = new SbiSymDef(uniqueName);
327 pWithParentDef->SetType(SbxOBJECT);
328 rParser.pPool->Add(pWithParentDef);
329
330 // DIM local variable: work with Option Explicit
331 rParser.aGen.Gen(SbiOpcode::LOCAL_, pWithParentDef->GetId(), pWithParentDef->GetType());
332
333 return SbiExpression(&rParser, *pWithParentDef);
334 }
335
336 SbiParser& m_rParser;
337 SbiExpression m_aWithParent;
338 };
339 }
340
341 void SbiParser::With()
342 {
343 SbiExpression aVar( this, SbOPERAND );
344
345 SbiExprNode *pNode = aVar.GetExprNode()->GetRealNode();
346 if (!pNode)
347 return;
348 SbiSymDef* pDef = pNode->GetVar();
349 // Variant, from 27.6.1997, #41090: empty -> must be Object
350 if( pDef->GetType() == SbxVARIANT || pDef->GetType() == SbxEMPTY )
351 pDef->SetType( SbxOBJECT );
352 else if( pDef->GetType() != SbxOBJECT )
353 Error( ERRCODE_BASIC_NEEDS_OBJECT );
354
355 pNode->SetType( SbxOBJECT );
356
357 std::optional<WithLocalVar> oLocalVar;
358 if (pDef->GetProcDef())
359 oLocalVar.emplace(*this, aVar);
360
361 OpenBlock(NIL, oLocalVar ? oLocalVar->m_aWithParent.GetExprNode() : aVar.GetExprNode());
362 StmntBlock( ENDWITH );
363 CloseBlock();
364 }
365
366 // On expr Goto/Gosub n,n,n...
367
368 void SbiParser::OnGoto()
369 {
370 SbiExpression aCond( this );
371 aCond.Gen();
372 sal_uInt32 nLabelsTarget = aGen.Gen( SbiOpcode::ONJUMP_, 0 );
373 SbiToken eTok = Next();
374 if( eTok != GOTO && eTok != GOSUB )
375 {
376 Error( ERRCODE_BASIC_EXPECTED, u"GoTo/GoSub"_ustr );
377 eTok = GOTO;
378 }
379
380 sal_uInt32 nLbl = 0;
381 do
382 {
383 Next(); // get label
384 if( MayBeLabel() )
385 {
386 sal_uInt32 nOff = pProc->GetLabels().Reference( aSym );
387 aGen.Gen( SbiOpcode::JUMP_, nOff );
388 nLbl++;
389 }
390 else Error( ERRCODE_BASIC_LABEL_EXPECTED );
391 }
392 while( !bAbort && TestComma() );
393 if( eTok == GOSUB )
394 nLbl |= 0x8000;
395 aGen.Patch( nLabelsTarget, nLbl );
396 }
397
398 // GOTO/GOSUB
399
400 void SbiParser::Goto()
401 {
402 SbiOpcode eOp = eCurTok == GOTO ? SbiOpcode::JUMP_ : SbiOpcode::GOSUB_;
403 Next();
404 if( MayBeLabel() )
405 {
406 sal_uInt32 nOff = pProc->GetLabels().Reference( aSym );
407 aGen.Gen( eOp, nOff );
408 }
409 else Error( ERRCODE_BASIC_LABEL_EXPECTED );
410 }
411
412 // RETURN [label]
413
414 void SbiParser::Return()
415 {
416 Next();
417 if( MayBeLabel() )
418 {
419 sal_uInt32 nOff = pProc->GetLabels().Reference( aSym );
420 aGen.Gen( SbiOpcode::RETURN_, nOff );
421 }
422 else aGen.Gen( SbiOpcode::RETURN_, 0 );
423 }
424
425 // SELECT CASE
426
427 void SbiParser::Select()
428 {
429 TestToken( CASE );
430 SbiExpression aCase( this );
431 SbiToken eTok = NIL;
432 aCase.Gen();
433 aGen.Gen( SbiOpcode::CASE_ );
434 TestEoln();
435 sal_uInt32 nNextTarget = 0;
436 sal_uInt32 nDoneTarget = 0;
437 bool bElse = false;
438
439 while( !bAbort )
440 {
441 eTok = Next();
442 if( eTok == CASE )
443 {
444 if( nNextTarget )
445 {
446 aGen.BackChain( nNextTarget );
447 nNextTarget = 0;
448 }
449 aGen.Statement();
450
451 bool bDone = false;
452 sal_uInt32 nTrueTarget = 0;
453 if( Peek() == ELSE )
454 {
455 // CASE ELSE
456 Next();
457 bElse = true;
458 }
459 else while( !bDone )
460 {
461 if( bElse )
462 Error( ERRCODE_BASIC_SYNTAX );
463 SbiToken eTok2 = Peek();
464 if( eTok2 == IS || ( eTok2 >= EQ && eTok2 <= GE ) )
465 { // CASE [IS] operator expr
466 if( eTok2 == IS )
467 Next();
468 eTok2 = Peek();
469 if( eTok2 < EQ || eTok2 > GE )
470 Error( ERRCODE_BASIC_SYNTAX );
471 else Next();
472 SbiExpression aCompare( this );
473 aCompare.Gen();
474 nTrueTarget = aGen.Gen(
475 SbiOpcode::CASEIS_, nTrueTarget,
476 sal::static_int_cast< sal_uInt16 >(
477 SbxEQ + ( eTok2 - EQ ) ) );
478 }
479 else
480 { // CASE expr | expr TO expr
481 SbiExpression aCase1( this );
482 aCase1.Gen();
483 if( Peek() == TO )
484 {
485 // CASE a TO b
486 Next();
487 SbiExpression aCase2( this );
488 aCase2.Gen();
489 nTrueTarget = aGen.Gen( SbiOpcode::CASETO_, nTrueTarget );
490 }
491 else
492 // CASE a
493 nTrueTarget = aGen.Gen( SbiOpcode::CASEIS_, nTrueTarget, SbxEQ );
494
495 }
496 if( Peek() == COMMA ) Next();
497 else
498 {
499 TestEoln();
500 bDone = true;
501 }
502 }
503
504 if( !bElse )
505 {
506 nNextTarget = aGen.Gen( SbiOpcode::JUMP_, nNextTarget );
507 aGen.BackChain( nTrueTarget );
508 }
509 // build the statement body
510 while( !bAbort )
511 {
512 eTok = Peek();
513 if( eTok == CASE || eTok == ENDSELECT )
514 break;
515 if( !Parse() ) goto done;
516 eTok = Peek();
517 if( eTok == CASE || eTok == ENDSELECT )
518 break;
519 }
520 if( !bElse )
521 nDoneTarget = aGen.Gen( SbiOpcode::JUMP_, nDoneTarget );
522 }
523 else if( !IsEoln( eTok ) )
524 break;
525 }
526 done:
527 if( eTok != ENDSELECT )
528 Error( ERRCODE_BASIC_EXPECTED, ENDSELECT );
529 if( nNextTarget )
530 aGen.BackChain( nNextTarget );
531 aGen.BackChain( nDoneTarget );
532 aGen.Gen( SbiOpcode::ENDCASE_ );
533 }
534
535 // ON Error/Variable
536
537 void SbiParser::On()
538 {
539 SbiToken eTok = Peek();
540 OUString aString = SbiTokenizer::Symbol(eTok);
541 if (aString.equalsIgnoreAsciiCase("ERROR"))
542 {
543 eTok = ERROR_; // Error comes as SYMBOL
544 }
545 if( eTok != ERROR_ && eTok != LOCAL )
546 {
547 OnGoto();
548 }
549 else
550 {
551 if( eTok == LOCAL )
552 {
553 Next();
554 }
555 Next (); // no more TestToken, as there'd be an error otherwise
556
557 Next(); // get token after error
558 if( eCurTok == GOTO )
559 {
560 // ON ERROR GOTO label|0
561 Next();
562 bool bError_ = false;
563 if( MayBeLabel() )
564 {
565 if( eCurTok == NUMBER && !nVal )
566 {
567 aGen.Gen( SbiOpcode::STDERROR_ );
568 }
569 else
570 {
571 sal_uInt32 nOff = pProc->GetLabels().Reference( aSym );
572 aGen.Gen( SbiOpcode::ERRHDL_, nOff );
573 }
574 }
575 else if( eCurTok == MINUS )
576 {
577 Next();
578 if( eCurTok == NUMBER && nVal == 1 )
579 {
580 aGen.Gen( SbiOpcode::STDERROR_ );
581 }
582 else
583 {
584 bError_ = true;
585 }
586 }
587 if( bError_ )
588 {
589 Error( ERRCODE_BASIC_LABEL_EXPECTED );
590 }
591 }
592 else if( eCurTok == RESUME )
593 {
594 TestToken( NEXT );
595 aGen.Gen( SbiOpcode::NOERROR_ );
596 }
597 else Error( ERRCODE_BASIC_EXPECTED, u"GoTo/Resume"_ustr );
598 }
599 }
600
601 // RESUME [0]|NEXT|label
602
603 void SbiParser::Resume()
604 {
605 sal_uInt32 nLbl;
606
607 switch( Next() )
608 {
609 case EOS:
610 case EOLN:
611 aGen.Gen( SbiOpcode::RESUME_, 0 );
612 break;
613 case NEXT:
614 aGen.Gen( SbiOpcode::RESUME_, 1 );
615 Next();
616 break;
617 case NUMBER:
618 if( !nVal )
619 {
620 aGen.Gen( SbiOpcode::RESUME_, 0 );
621 break;
622 }
623 [[fallthrough]];
624 case SYMBOL:
625 if( MayBeLabel() )
626 {
627 nLbl = pProc->GetLabels().Reference( aSym );
628 aGen.Gen( SbiOpcode::RESUME_, nLbl );
629 Next();
630 break;
631 }
632 [[fallthrough]];
633 default:
634 Error( ERRCODE_BASIC_LABEL_EXPECTED );
635 }
636 }
637
638 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */