ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
select_case.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
60/* <usermanual>
61@keyword SELECT CASE...CASE...CASE ELSE...ENDSELECT
62
63@english
64
65The ''SELECT CASE'' command is part of the ''SELECT CASE...END SELECT'' structure.
66It allows you to execute different blocks of code depending on the value of a
67variable or expression. In practice, it is like having a series of "cases" and
68the program executes the code corresponding to the case that occurs.
69
70The ''expression'' is evaluated at the beginning of the control block,
71and its value is compared to the values specified in the cases.
72Each ''CASE'' represents a possible value or a range of values
73of the expression. Inside each case, you insert the instructions that
74will be executed if the value of the expression matches that case.
75The ''CASE ELSE'' is optional, and it is executed if no previous
76case is true. In 8-bit computers, the semantics of ''SELECT CASE'' are
77closely tied to data representation. Because registers and variables were
78often limited to 8 bits, the values that could be compared in cases are
79integers between 0 and 255, also if ugBASIC supports any integer type
80Anyway, it allows the expression to be compared to any integer value but,
81due to the 8-bit limitation, the range of values that could be
82compared should be limited, to be effective, in terms of performances.
83
84Using ''SELECT CASE'' makes code clearer and easier to understand
85than a series of nested ''IF...THEN...ELSE'' statements. It can
86be more efficient than a series of ''IF...THEN...ELSE'' statements.
87
88@italian
89
90Il comando ''SELECT CASE'' fa parte della struttura
91''SELECT CASE...END SELECT''. Consente di eseguire diversi blocchi di
92codice a seconda del valore di una variabile o espressione. In pratica,
93è come avere una serie di "casi" e il programma esegue il codice
94corrispondente al caso che si verifica.
95
96L'espressione viene valutata all'inizio del blocco di controllo e
97il suo valore viene confrontato con i valori specificati nei casi.
98Ogni ''CASE'' rappresenta un possibile valore o un intervallo di
99valori dell'espressione. All'interno di ogni caso, inserisci le
100istruzioni che verranno eseguite se il valore dell'espressione
101corrisponde a quel caso.
102
103Il ''CASE ELSE'' è facoltativo e viene eseguito se nessun caso
104precedente è vero. Nei computer a 8 bit, la semantica di
105''SELECT CASE'' è strettamente legata alla rappresentazione dei
106dati. Poiché registri e variabili erano spesso limitati a 8 bit,
107i valori che potevano essere confrontati in casi sono interi tra
1080 e 255, anche se ugBASIC supporta qualsiasi tipo di intero. In
109ogni caso, consente di confrontare l'espressione con qualsiasi
110valore intero ma, a causa della limitazione a 8 bit, l'intervallo
111di valori che potevano essere confrontati dovrebbe essere limitato,
112per essere efficace, in termini di prestazioni.
113
114L'utilizzo di ''SELECT CASE'' rende il codice più chiaro e
115facile da capire rispetto a una serie di istruzioni ''IF...THEN...ELSE''
116annidate. Può essere più efficiente di una serie di istruzioni ''IF...THEN...ELSE''.
117
118@syntax SELECT CASE expression
119@syntax CASE value1:
120@syntax ...
121@syntax CASE value2:
122@syntax ...
123@syntax ...
124@syntax [ CASE ELSE ]
125@syntax ...
126@syntax ENDSELECT
127
128@example SELECT CASE number
129@example CASE 1
130@example PRINT "one!"
131@example CASE 2
132@example PRINT "two!"
133@example CASE ELSE
134@example PRINT "neither!"
135@example ENDSELCT
136
137@usedInExample control_case_01.bas
138
139@seeAlso CASE
140@seeAlso CASE ELSE
141@seeAlso ENDSELECT
142
143@target all
144</usermanual> */
145void select_case( Environment * _environment, char * _expression ) {
146
148
149 Variable * expression = variable_retrieve_or_define( _environment, _expression, VT_BYTE, 0 );
150
151 Conditional * conditional = malloc( sizeof( Conditional ) );
152 memset( conditional, 0, sizeof( Conditional ) );
153 conditional->label = strdup( label );
154 conditional->type = CT_SELECT_CASE;
155
156 conditional->expression = variable_cast( _environment, expression->name, expression->type );
157 conditional->expression->locked = 1;
158 conditional->next = _environment->conditionals;
159 _environment->conditionals = conditional;
160
161}
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 select_case(Environment *_environment, char *_expression)
Emit ASM code for SELECT CASE ... {PASSING}.
struct _Conditional * next
Definition ugbc.h:1378
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)
@ CT_SELECT_CASE
Definition ugbc.h:1348
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_BYTE
Definition ugbc.h:450
struct _Conditional Conditional
Structure of a single conditional jump.
#define MAKE_LABEL
Definition ugbc.h:3351