ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
goto.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
51/* <usermanual>
52@keyword GOTO
53
54@english
55
56The ''GOTO'' command is a statement that allows you to jump unconditionally
57to another part of the program. In practice, instead of executing the instructions
58in sequence, the program "jumps" directly to the line indicated by the ''GOTO'' command.
59
60Despite its simplicity, indiscriminate use of the ''GOTO'' command can lead to several
61problems. It can make the code very difficult to follow and understand, creating
62so-called "spaghetti code". If a program contains many unconditional jumps, it can
63become very difficult to find the source of an error. Frequent use of ''GOTO'' tends to
64create a poorly structured flow of control that is difficult to maintain.
65
66The ugBASIC offers more sophisticated and readable control structures, such as:
67''IF...THEN...ELSE'', ''FOR...NEXT'', ''DO...LOOP'', and so on. These structures make
68the code clearer and make it easier to manage the flow of control.
69
70In some specific cases, however, the use of ''GOTO'' can be justified, for example
71in very specific situations, a ''GOTO'' can be used to exit a nested loop more efficiently.
72In some cases, a ''GOTO'' can be used to handle errors in the most direct way.
73In general, it is advisable to avoid the use of ''GOTO'' and prefer more modern
74control structures. A well-structured and readable code is easier to maintain
75and to change over time.
76
77@italian
78
79Il comando ''GOTO'' è un'istruzione che consente di saltare incondizionatamente
80a un'altra parte del programma. In pratica, invece di eseguire le istruzioni
81in sequenza, il programma "salta" direttamente alla riga indicata dal comando
82''GOTO''.
83
84Nonostante la sua semplicità, l'uso indiscriminato del comando ''GOTO'' può
85portare a diversi problemi. Può rendere il codice molto difficile da seguire
86e comprendere, creando il cosiddetto "codice spaghetti". Se un programma contiene
87molti salti incondizionati, può diventare molto difficile trovare la fonte di
88un errore. L'uso frequente di ''GOTO'' tende a creare un flusso di controllo
89mal strutturato che è difficile da mantenere.
90
91L'ugBASIC offre strutture di controllo più sofisticate e leggibili, come:
92''IF...THEN...ELSE'', ''FOR...NEXT'', ''DO...LOOP'' e così via. Queste
93strutture rendono il codice più chiaro e facilitano la gestione del
94flusso di controllo.
95
96In alcuni casi specifici, tuttavia, l'uso di ''GOTO'' può essere giustificato,
97ad esempio in situazioni molto specifiche, un ''GOTO'' può essere utilizzato
98per uscire da un ciclo annidato in modo più efficiente.
99
100In alcuni casi, un ''GOTO'' può essere utilizzato per gestire gli errori
101nel modo più diretto. In generale, è consigliabile evitare l'uso di ''GOTO''
102e preferire strutture di controllo più moderne. Un codice ben strutturato e
103leggibile è più facile da mantenere e da modificare nel tempo.
104
105@syntax GOTO label
106
107@example GOTO nuovaEtichetta
108
109@usedInExample control_uncond_jumps_01.bas
110@usedInExample control_uncond_jumps_02.bas
111@usedInExample control_returning_01.bas
112@usedInExample control_returning_02.bas
113
114</usermanual> */
115void goto_label( Environment * _environment, char * _label ) {
116
117 char realLabel[MAX_TEMPORARY_STORAGE];
118 if (strcmp(_label, "q" ) == 0 && _environment->vestigialConfig.rchack_ostra_1172 ) {
119 sprintf( realLabel, "lbl%s", _label );
120 } else {
121 strcpy( realLabel, _label );
122 }
123 label_referred_define_named( _environment, realLabel );
124
125 cpu_jump( _environment, realLabel );
126
127}
128
141/* <usermanual>
142@keyword GOTO
143
144@syntax GOTO number
145
146@example GOTO 42
147</usermanual> */
148void goto_number( Environment * _environment, int _number ) {
149
150 label_referred_define_numeric( _environment, _number );
151
152 char label[MAX_TEMPORARY_STORAGE]; sprintf( label, "_linenumber%d", _number );
153
154 cpu_jump( _environment, label );
155
156}
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
void label_referred_define_named(Environment *_environment, char *_label)
void label_referred_define_numeric(Environment *_environment, int _label)
void goto_number(Environment *_environment, int _number)
Emit ASM code for GOTO [number].
Definition goto.c:148
void goto_label(Environment *_environment, char *_label)
Emit ASM code for GOTO [label].
Definition goto.c:115
VestigialConfig vestigialConfig
Definition ugbc.h:2442
char rchack_ostra_1172
Definition ugbc.h:2020
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
struct _Environment Environment
Structure of compilation environment.