ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
exit_loop.c
Go to the documentation of this file.
1/*****************************************************************************
2 * ugBASIC - an isomorphic BASIC language compiler for retrocomputers *
3 *****************************************************************************
4 * Copyright 2021-2026 Marco Spedaletti (asimov@mclink.it)
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *----------------------------------------------------------------------------
18 * Concesso in licenza secondo i termini della Licenza Apache, versione 2.0
19 * (la "Licenza"); è proibito usare questo file se non in conformità alla
20 * Licenza. Una copia della Licenza è disponibile all'indirizzo:
21 *
22 * http://www.apache.org/licenses/LICENSE-2.0
23 *
24 * Se non richiesto dalla legislazione vigente o concordato per iscritto,
25 * il software distribuito nei termini della Licenza è distribuito
26 * "COSÌ COM'È", SENZA GARANZIE O CONDIZIONI DI ALCUN TIPO, esplicite o
27 * implicite. Consultare la Licenza per il testo specifico che regola le
28 * autorizzazioni e le limitazioni previste dalla medesima.
29 ****************************************************************************/
30
31/****************************************************************************
32 * INCLUDE SECTION
33 ****************************************************************************/
34
35#include "../../ugbc.h"
36
37/****************************************************************************
38 * CODE SECTION
39 ****************************************************************************/
40
50/* <usermanual>
51@keyword EXIT
52
53@english
54
55The ''EXIT'' command is used to stop execution of a specific block of code
56and transfer control of the program to the first statement following that block.
57It is often used inside ''FOR...NEXT'', ''WHILE...WEND'', or ''DO...LOOP'' loops
58to terminate the loop prematurely when a certain condition is met. The optional parameter
59''number'' specifies the number of inner blocks you want to exit. If missing,
60the implicit value is 1.
61
62The command is useful to exit when an error or unexpected condition occurs, and it
63can be used to end the code block and handle the error appropriately. In some cases,
64using ''EXIT'' can make your code more efficient by avoiding executing unnecessary
65statements. ''EXIT'' allows you to create more complex control flows and make dynamic decisions
66during program execution. Excessive use of ''EXIT'', however,
67can make your code more difficult to read and maintain.
68
69@italian
70
71Il comando ''EXIT'' viene utilizzato per interrompere l'esecuzione di un blocco
72di codice specifico e trasferire il controllo del programma alla prima istruzione
73successiva a tale blocco.
74
75Viene spesso utilizzato all'interno dei cicli ''FOR...NEXT'', ''WHILE...WEND'' o
76''DO...LOOP'' per terminare il ciclo prematuramente quando viene soddisfatta
77una determinata condizione. Il parametro facoltativo ''number'' specifica il numero
78di blocchi interni da cui si desidera uscire. Se manca, il valore implicito è 1.
79Il comando è utile per uscire quando si verifica un errore o una condizione
80imprevista e può essere utilizzato per terminare il blocco di codice e gestire
81l'errore in modo appropriato. In alcuni casi, l'utilizzo di ''EXIT'' può rendere
82il codice più efficiente evitando di eseguire istruzioni non necessarie. ''EXIT''
83consente di creare flussi di controllo più complessi e di prendere decisioni
84dinamiche durante l'esecuzione del programma. L'uso eccessivo di ''EXIT''
85può rendere il codice più difficile da leggere e gestire.
86
87@syntax EXIT [number]
88
89@example EXIT
90@example EXIT 2
91
92@usedInExample control_loops_02.bas
93@usedInExample control_loops_03.bas
94
95@target all
96</usermanual> */
97void exit_loop( Environment * _environment, int _number ) {
98
99
100
101 Loop * loop = _environment->loops;
102
103 if ( ! loop ) {
105 }
106
107 if ( _number ) {
108 --_number;
109 }
110
111 while( _number-- ) {
112 loop = loop->next;
113
114 if ( ! loop ) {
116 }
117 }
118
119 unsigned char newLabel[MAX_TEMPORARY_STORAGE]; sprintf(newLabel, "%sbis", loop->label );
120
121 cpu_jump( _environment, newLabel );
122
123}
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
void exit_loop(Environment *_environment, int _number)
Emit ASM code for EXIT.
Definition exit_loop.c:97
Loop * loops
Definition ugbc.h:2669
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
void loop(Environment *_environment, char *_label)
#define CRITICAL_EXIT_WITHOUT_LOOP()
Definition ugbc.h:3588
#define CRITICAL_EXIT_WITHOUT_ENOUGH_LOOP()
Definition ugbc.h:3589
struct _Environment Environment
Structure of compilation environment.
struct _Loop Loop
Structure of a single loop.