ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
return_procedure.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
47/* <usermanual>
48@keyword RETURN (procedure)
49
50@english
51If you want to return a parameter from inside a procedure, that is to say,
52if you need to send back a value from a local variable and/or expression,
53you need a way of telling your main program where to find this local variable. T
54
55The ''RETURN'' instruction takes the result of an expression and put it into
56the ''PARAM'' variable and it returns the expression like the result of the calling
57procedure.
58
59@italian
60Per restituire un parametro dall'interno di una procedura, vale a dire,
61per restituire un valore da una variabile locale e/o da un'espressione,
62è necessario avere un modo per comunicare al programma principale
63dove trovare questo valore.
64
65L'istruzione ''RETURN'' prende il risultato di un'espressione e lo inserisce
66nella variabile ''PARAM'' e, contemporaneamente, lo restituisce come
67espressione come il risultato della chiamata.
68
69Un altro utilizzo di questo comando è per tornare da un ''GOSUB'':
70in questo caso, non è necessario alcun parametro.
71
72@syntax RETURN expression
73
74@example RETURN 42
75
76@usedInExample procedures_param_01.bas
77@usedInExample procedures_param_02.bas
78@usedInExample procedures_param_03.bas
79
80@seeAlso PROCEDURE
81
82</usermanual> */
83void return_procedure( Environment * _environment, char * _value ) {
84
85 if ( _environment->emptyProcedure ) {
86 return;
87 }
88
89 char paramName[MAX_TEMPORARY_STORAGE]; sprintf(paramName,"%s__PARAM", _environment->procedureName );
90 Variable * value;
91 if ( variable_exists( _environment, _value ) ) {
92 value = variable_retrieve( _environment, _value );
93 } else {
94 value = variable_retrieve_or_define( _environment, _value, _environment->defaultVariableType, 0 );
95 }
96 Variable * param = NULL;
97 if ( variable_exists( _environment, paramName ) ) {
98 param = variable_retrieve( _environment, paramName );
99 } else {
100 param = variable_define( _environment, paramName, value->type, 0 );
101 }
102 variable_move( _environment, value->name, param->name );
103 cpu_return( _environment );
104}
void cpu_return(Environment *_environment)
Definition 6309.c:4030
Variable * variable_retrieve(Environment *_environment, char *_name)
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
int variable_exists(Environment *_environment, char *_name)
Variable * variable_move(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable by converting it.
Variable * variable_define(Environment *_environment, char *_name, VariableType _type, int _value)
Define a variable for the program.
void return_procedure(Environment *_environment, char *_value)
Emit code for RETURN ....
VariableType defaultVariableType
Definition ugbc.h:2956
char * procedureName
Definition ugbc.h:2775
int emptyProcedure
Definition ugbc.h:2932
char * name
Definition ugbc.h:979
VariableType type
Definition ugbc.h:988
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.