ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
if_then.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
58/* <usermanual>
59@keyword IF...THEN...ELSE...ELSEIF...ENDIF
60
61@english
62
63Implement a conditional jump. This implementation assumes that
64an expression passed as a parameter is 0 (for false) and not
65zero (for true). In this case, if the expression is zero, it
66jumps directly to the statement following the corresponding
67''ENDIF'' (or ''ELSE'', if present). Otherwise, the following
68code will be executed (up to ''ENDIF'' or ''ELSE'').
69
70The compiler also accepts a number of variants to the command,
71which were however widespread in other BASIC dialects. The first
72variant is the one for which the action to be performed is
73unique, and only an unconditional jump via ''GOTO'' or even as a number
74immediately after the ''THEN''. There is also a variant that
75directly accepts a single command after the THEN.
76
77@italian
78Implementa il salto condizionato. Questa implementazione presuppone che
79un'espressione passata come parametro è 0 (per falso) e non
80zero (per vero). In questo caso, se l'espressione è zero, esso
81salta direttamente all'istruzione che segue il corrispondente
82''ENDIF'' (oppure ''ELSE'', se presente). In caso contrario,
83verrà eseguito il codice seguente (fino a ''ENDIF'').
84
85Il compilatore accetta anche una serie di varianti del comando, che erano
86tuttavia diffuse in altri dialetti BASIC. La prima variante è quella per
87cui l'azione da eseguire è unica, e solo un salto incondizionato tramite
88''GOTO'' o anche come numero immediatamente dopo ''THEN''. Vi è anche una
89variante che accetta direttamente un singolo comando dopo il THEN.
90
91@syntax IF expression THEN
92@syntax ...
93@syntax ELSE
94@syntax ...
95@syntax ENDIF
96@syntax IF expression THEN
97@syntax ...
98@syntax ELSEIF expression2 THEN
99@syntax ...
100@syntax ...
101@syntax ELSE
102@syntax ...
103@syntax ENDIF
104@syntax IF expression THEN GOTO number
105@syntax IF expression THEN number [ELSE number]
106@syntax IF expression THEN statement
107
108@example IF ( x == 42 ) THEN : x = 0 : ELSE : x = 1 : ENDIF
109@example IF ( x == 42 ) THEN : x = 0 : ELSE IF y == 0 THEN : y = 42 : ELSE : x = 1 : ENDIF
110@example IF ( x == 42 ) THEN
111@example x = 0
112@example ELSE IF y == 0 THEN
113@example y = 42
114@example ELSE
115@example x = 1
116@example ENDIF
117
118@usedInExample control_returning_01.bas
119@usedInExample control_returning_02.bas
120@usedInExample control_popping_91.bas
121
122</usermanual> */
123void if_then( Environment * _environment, char * _expression ) {
124
126
127 Variable * expression = variable_retrieve_or_define( _environment, _expression, VT_SBYTE, 0 );
128
129 Conditional * conditional = malloc( sizeof( Conditional ) );
130 memset( conditional, 0, sizeof( Conditional ) );
131 conditional->label = strdup( label );
132 conditional->type = CT_IF;
133
134 conditional->expression = variable_cast( _environment, expression->name, expression->type );
135 conditional->expression->locked = 1;
136 conditional->next = _environment->conditionals;
137 _environment->conditionals = conditional;
138
139 char thenLabel[MAX_TEMPORARY_STORAGE]; sprintf(thenLabel, "%st", label );
140 char elseLabel[MAX_TEMPORARY_STORAGE]; sprintf(elseLabel, "%se%d", conditional->label, conditional->index );
141
142 cpu_bveq( _environment, expression->realName, elseLabel );
143
144 cpu_label( _environment, thenLabel );
145
146}
void cpu_bveq(Environment *_environment, char *_value, char *_label)
Definition 6309.c:334
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
Variable * variable_cast(Environment *_environment, char *_source, VariableType _type)
Cast a variable from a type to another.
void if_then(Environment *_environment, char *_expression)
Emit ASM code for IF ... THEN ....
Definition if_then.c:123
struct _Conditional * next
Definition ugbc.h:1378
int index
Definition ugbc.h:1372
ConditionalType type
Definition ugbc.h:1363
char * label
Definition ugbc.h:1366
Variable * expression
Definition ugbc.h:1369
Conditional * conditionals
Definition ugbc.h:2664
int locked
Definition ugbc.h:1009
void * malloc(YYSIZE_T)
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
@ CT_IF
Definition ugbc.h:1336
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_SBYTE
Definition ugbc.h:452
struct _Conditional Conditional
Structure of a single conditional jump.
#define MAKE_LABEL
Definition ugbc.h:3351