ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
while.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
50/* <usermanual>
51@keyword WHILE...WEND
52
53@english
54This instruction define a conditional loop, or a list of statements that will be executed
55while an expression is true. ''WHILE'' acts as the starting position while ''WEND''
56as the ending one. This command provides a convenient way of making the program repeat
57a group of instructions all the time a particular condition is true. The condition is
58checked again at every turn of the loop, until it is no longer true.
59
60@italian
61Questa istruzione definisce un ciclo condizionale o un elenco di istruzioni che verranno eseguite
62mentre un'espressione è vera. ''WHILE'' funge da posizione di partenza del loop mentre ''WEND''
63come quello di fine. Questo comando fornisce un modo conveniente per ripetere un gruppo
64di istruzioni per tutto il tempo nel quale una particolare condizione è vera. La condizione è
65controllata di nuovo ad ogni ciclo, fino a quando non è più vera.
66
67@syntax WHILE expression
68@syntax ...
69@syntax WEND
70
71@example WHILE alive
72@example score = score + 1
73@example WEND
74
75@usedInExample control_loops_04.bas
76
77@seeAlso DO...LOOP
78@seeAlso REPEAT...UNTIL
79@seeAlso FOR...NEXT
80
81</usermanual> */
82
83void begin_while( Environment * _environment ) {
84
86
87 Loop * loop = malloc( sizeof( Loop ) );
88 loop->label = strdup( label );
89 loop->type = LT_WHILE;
90 loop->next = _environment->loops;
91 _environment->loops = loop;
92
93 unsigned char endWhile[MAX_TEMPORARY_STORAGE]; sprintf(endWhile, "%sbis", loop->label );
94
95 cpu_label( _environment, loop->label );
96
97}
98
99void begin_while_condition( Environment * _environment, char * _expression ) {
100
101 Loop * loop = _environment->loops;
102
103 if ( ! loop ) {
104 CRITICAL_INTERNAL_ERROR("begin_while_condition called out of order (1)");
105 }
106
107 if ( loop->type != LT_WHILE ) {
108 CRITICAL_INTERNAL_ERROR("begin_while_condition called out of order (1)");
109 }
110
111 Variable * expression = variable_retrieve_or_define( _environment, _expression, VT_BYTE, 0 );
112
113 unsigned char endWhile[MAX_TEMPORARY_STORAGE]; sprintf(endWhile, "%sbis", loop->label );
114
115 cpu_bveq( _environment, expression->realName, endWhile );
116
117}
118
127void end_while( Environment * _environment ) {
128
129 Loop * loop = _environment->loops;
130
131 if ( ! loop ) {
133 }
134
135 if ( loop->type != LT_WHILE ) {
137 }
138
139 if ( _environment->procedureName && _environment->protothread && ! _environment->protothreadForbid ) {
140 yield( _environment );
141 }
142
143 cpu_jump( _environment, loop->label );
144
145 unsigned char endWhile[MAX_TEMPORARY_STORAGE]; sprintf(endWhile, "%sbis", loop->label );
146
147 cpu_label( _environment, endWhile );
148
149 _environment->loops = _environment->loops->next;
150
151};
void cpu_bveq(Environment *_environment, char *_value, char *_label)
Definition 6309.c:334
void cpu_label(Environment *_environment, char *_label)
Definition 6309.c:356
void cpu_jump(Environment *_environment, char *_label)
Definition 6309.c:3739
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
int protothread
Definition ugbc.h:2830
char * procedureName
Definition ugbc.h:2775
int protothreadForbid
Definition ugbc.h:2845
Loop * loops
Definition ugbc.h:2669
struct _Loop * next
Definition ugbc.h:1452
char * realName
Definition ugbc.h:982
void * malloc(YYSIZE_T)
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
void loop(Environment *_environment, char *_label)
struct _Variable Variable
Structure of a single variable.
#define CRITICAL_INTERNAL_ERROR(v)
Definition ugbc.h:3443
struct _Environment Environment
Structure of compilation environment.
@ VT_BYTE
Definition ugbc.h:450
@ LT_WHILE
Definition ugbc.h:1390
struct _Loop Loop
Structure of a single loop.
#define CRITICAL_WEND_WITHOUT_WHILE()
Definition ugbc.h:3587
#define MAKE_LABEL
Definition ugbc.h:3351
void begin_while_condition(Environment *_environment, char *_expression)
Definition while.c:99
void begin_while(Environment *_environment)
Emit ASM code for WHILE ....
Definition while.c:83
void end_while(Environment *_environment)
Emit ASM code for ... WEND.
Definition while.c:127
void yield(Environment *_environment)
Emit code for YIELD.
Definition yield.c:63