ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
case_equals.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
41/* <usermanual>
42@keyword CASE
43
44@english
45
46The ''CASE'' command is part of ''SELECT...ENDSELECT'' construct. Each ''CASE''
47represents a possible value of the expression. After each case, you can insert
48the statements that will be executed if the value of the expression matches that case.
49The code will be executed up to the ''ENDSELECT'' instruction, another ''CASE''
50instruction and, finally, when a ''CASE ELSE'' is matched.
51
52@italian
53
54Il comando ''CASE'' fa parte del costrutto ''SELECT...ENDSELECT''. Ogni ''CASE''
55rappresenta un possibile valore dell'espressione. Dopo ogni caso, puoi inserire
56le istruzioni che verranno eseguite se il valore dell'espressione corrisponde
57a quel caso. Il codice verrà eseguito fino all'istruzione ''ENDSELECT'',
58un'altra istruzione ''CASE'' e, infine, quando viene trovato un ''CASE ELSE''.
59
60@syntax CASE value
61
62@example SELECT CASE answer
63@example CASE 42
64@example PRINT "The answer!"
65@example CASE ELSE
66@example PRINT "I am still thinking..."
67@example ENDSELECT
68
69@seeAlso SELECT CASE
70@seeAlso CASE ELSE
71@seeAlso ENDSELECT
72
73</usermanual> */
74void case_equals_label( Environment * _environment ) {
75
76 Conditional * conditional = _environment->conditionals;
77
78 if ( ! conditional ) {
80 }
81
82 if ( conditional->type != CT_SELECT_CASE ) {
84 }
85
86 char endselectLabel[MAX_TEMPORARY_STORAGE]; sprintf(endselectLabel, "%sf", conditional->label );
87 char elseLabel[MAX_TEMPORARY_STORAGE]; sprintf(elseLabel, "%se%d", conditional->label, conditional->index );
88
89 if ( conditional->index > 0 ) {
90 cpu_jump( _environment, endselectLabel );
91 }
92
93 cpu_label( _environment, elseLabel );
94
95}
96
106void case_equals_var( Environment * _environment, char * _value ) {
107
108 Conditional * conditional = _environment->conditionals;
109
110 if ( ! conditional ) {
112 }
113
114 if ( conditional->type != CT_SELECT_CASE ) {
116 }
117
118 Variable * value = variable_retrieve_or_define( _environment, _value, VT_BYTE, 0 );
119
120 char thenLabel[MAX_TEMPORARY_STORAGE]; sprintf(thenLabel, "%st%d", conditional->label, conditional->index );
121 char elseLabel[MAX_TEMPORARY_STORAGE]; sprintf(elseLabel, "%se%d", conditional->label, conditional->index );
122
123 ++conditional->index;
124 sprintf(elseLabel, "%se%d", conditional->label, conditional->index );
125
126 Variable * result;
127 if ( value->initializedByConstant ) {
128 result = variable_compare_const( _environment, conditional->expression->name, value->value );
129 } else {
130 result = variable_compare( _environment, conditional->expression->name, value->name );
131 }
132
133 cpu_bveq( _environment, result->realName, elseLabel );
134
135 cpu_label( _environment, thenLabel );
136
137}
138
148void case_equals( Environment * _environment, int _value ) {
149
150 Conditional * conditional = _environment->conditionals;
151
152 if ( ! conditional ) {
154 }
155
156 if ( conditional->type != CT_SELECT_CASE ) {
158 }
159
160 char thenLabel[MAX_TEMPORARY_STORAGE]; sprintf(thenLabel, "%st%d", conditional->label, conditional->index );
161 char elseLabel[MAX_TEMPORARY_STORAGE]; sprintf(elseLabel, "%se%d", conditional->label, conditional->index );
162
163 Variable * result = variable_temporary( _environment, VT_BYTE, "(comparing)");
164
165 ++conditional->index;
166 sprintf(elseLabel, "%se%d", conditional->label, conditional->index );
167
168 switch( VT_BITWIDTH( conditional->expression->type ) ) {
169 case 8:
170 cpu_compare_and_branch_8bit_const( _environment, conditional->expression->realName, _value, elseLabel, 0 );
171 break;
172 case 16:
173 cpu_compare_and_branch_16bit_const( _environment, conditional->expression->realName, _value, elseLabel, 0 );
174 break;
175 case 32:
176 cpu_compare_and_branch_32bit_const( _environment, conditional->expression->realName, _value, elseLabel, 0 );
177 break;
178 default:
180 }
181
182 cpu_label( _environment, thenLabel );
183
184}
void cpu_bveq(Environment *_environment, char *_value, char *_label)
Definition 6309.c:334
void cpu_compare_and_branch_16bit_const(Environment *_environment, char *_source, int _destination, char *_label, int _positive)
CPU 6309: emit code to compare two 8 bit values and jump if they are equal/different
Definition 6309.c:1578
void cpu_compare_and_branch_32bit_const(Environment *_environment, char *_source, int _destination, char *_label, int _positive)
CPU 6309: emit code to compare two 32 bit values and jump if they are equal/different
Definition 6309.c:2914
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
void cpu_compare_and_branch_8bit_const(Environment *_environment, char *_source, int _destination, char *_label, int _positive)
CPU 6309: emit code to compare two 8 bit values and jump if they are equal/different
Definition 6309.c:876
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * variable_compare(Environment *_environment, char *_source, char *_destination)
Compare two variable and return the result of comparation.
Variable * variable_compare_const(Environment *_environment, char *_source, int _destination)
Compare two variable and return the result of comparation.
void case_equals_var(Environment *_environment, char *_value)
Emit ASM code for CASE ....
void case_equals_label(Environment *_environment)
Definition case_equals.c:74
void case_equals(Environment *_environment, int _value)
Emit ASM code for CASE ....
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
char * name
Definition ugbc.h:979
VariableType type
Definition ugbc.h:988
int value
Definition ugbc.h:1025
int initializedByConstant
Definition ugbc.h:1036
char * realName
Definition ugbc.h:982
#define CRITICAL_CANNOT_COMPARE_WITH_CASE(d)
Definition ugbc.h:3545
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
@ 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
#define CRITICAL_CASE_WITHOUT_SELECT_CASE()
Definition ugbc.h:3596
struct _Conditional Conditional
Structure of a single conditional jump.
#define VT_BITWIDTH(t)
Definition ugbc.h:595