ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
gosub.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 GOSUB
53
54@english
55
56The ''GOSUB'' command allows you to "jump" to a specific part of the program,
57execute a set of instructions and then return exactly to the point where
58you started.
59
60It allows you to break a program into smaller, more manageable
61blocks of code, improving readability and maintenance. A subroutine can be called
62multiple times from different parts of the program, avoiding rewriting the same
63code multiple times. It helps organize the flow of the program, making it clearer
64and easier to follow.
65
66While ''GOTO'' allows an unconditional jump to any line of the program,
67''GOSUB'' is more structured and allows a return to the starting point.
68In general, ''GOSUB'' is considered a more powerful and flexible tool than
69''GOTO'', as it allows for better organization of the code.
70
71It is possible to nest subroutines, but it is important to make sure that
72each ''GOSUB'' has its corresponding ''RETURN''. So, a common mistake is to
73forget to put ''RETURN'' at the end of a subroutine, causing unpredictable
74behavior of the program.
75
76Subroutines are often implemented through functions and procedures, which
77offer more advanced functionality and more rigorous management of
78variable scope.
79
80@italian
81
82Il comando ''GOSUB'' consente di "saltare" a una parte specifica del programma,
83eseguire una serie di istruzioni e quindi tornare esattamente al punto di partenza.
84
85Consente di suddividere un programma in blocchi di codice più piccoli e gestibili,
86migliorandone la leggibilità e la manutenzione. Una subroutine può essere chiamata
87più volte da diverse parti del programma, evitando di riscrivere lo stesso codice
88più volte. Aiuta a organizzare il flusso del programma, rendendolo più chiaro e
89facile da seguire.
90
91Mentre ''GOTO'' consente un salto incondizionato a qualsiasi riga del programma,
92''GOSUB'' è più strutturato e consente di tornare al punto di partenza.
93In generale, ''GOSUB'' è considerato uno strumento più potente e flessibile
94di ''GOTO'', poiché consente una migliore organizzazione del codice.
95
96È possibile annidare le subroutine, ma è importante assicurarsi che ogni ''GOSUB''
97abbia il suo ''RETURN'' corrispondente. Quindi, un errore comune è dimenticare
98di mettere ''RETURN'' alla fine di una subroutine, causando un comportamento
99imprevedibile del programma.
100
101Le subroutine sono spesso implementate tramite funzioni e procedure, che
102offrono funzionalità più avanzate e una gestione più rigorosa dell'ambito
103delle variabili.
104
105@syntax GOSUB label
106
107@example GOSUB leggiTasti
108
109@usedInExample control_returning_01.bas
110@usedInExample control_returning_02.bas
111
112@seeAlso RETURN
113@seeAlso POP
114
115</usermanual> */
116void gosub_label( Environment * _environment, char * _label ) {
117
118 char realLabel[MAX_TEMPORARY_STORAGE];
119 if (strcmp(_label, "q" ) == 0 && _environment->vestigialConfig.rchack_ostra_1172) {
120 sprintf( realLabel, "lbl%s", _label );
121 } else {
122 strcpy( realLabel, _label );
123 }
124 label_referred_define_named( _environment, realLabel );
125
126 cpu_call( _environment, realLabel );
127
128}
129
140void gosub_number( Environment * _environment, int _number ) {
141
142 label_referred_define_numeric( _environment, _number );
143
144 char label[MAX_TEMPORARY_STORAGE]; sprintf( label, "_linenumber%d", _number );
145
146 cpu_call( _environment, label );
147
148}
void cpu_call(Environment *_environment, char *_label)
Definition 6309.c:3755
void label_referred_define_named(Environment *_environment, char *_label)
void label_referred_define_numeric(Environment *_environment, int _label)
void gosub_number(Environment *_environment, int _number)
Emit ASM code for GOSUB [number].
Definition gosub.c:140
void gosub_label(Environment *_environment, char *_label)
Emit ASM code for GOSUB [label].
Definition gosub.c:116
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.