ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
spawn.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 SPAWN
49
50@english
51This keyword will invoke a (parallel) procedure. The procedure can be
52invoked in an "halted" state using the keyword ''HALTED'', so that a ''RESPAWN''
53command must be issued in order to "wake up" the procedure.
54
55@italian
56Questa parola chiave invoca una funzione affinché sia eseguita
57in parallelo. La procedura può essere invocata in uno stato "sospeso"
58con la parola chiave ''HALTED'', così che sia necessario usare
59il comando ''RESPAWN'' per "risvegliare" la procedura.
60
61@syntax [HALTED] SPAWN [identifier]
62
63@example handle = SPAWN moveShip
64@example handle = HALTED SPAWN moveArrow
65
66@target all
67</usermanual> */
68Variable * spawn_procedure( Environment * _environment, char * _name, int _halted ) {
69
70 Variable * threadId = variable_temporary( _environment, VT_THREAD, "(thread)");
71
72 Procedure * procedure = _environment->procedures;
73
74 while( procedure ) {
75 if ( strcmp( procedure->name, _name ) == 0 ) {
76 break;
77 }
78 procedure = procedure->next;
79 }
80
81 if ( !procedure ) {
83 }
84
85 if ( !procedure->protothread ) {
87 }
88
89 if ( _environment->parameters != procedure->parameters ) {
90 CRITICAL_PROCEDURE_PARAMETERS_MISMATCH(_name, procedure->parameters, _environment->parameters );
91 }
92
93 int i=0;
94 for( i=0; i<procedure->parameters; ++i ) {
95 if ( _environment->parametersEach[i] ) {
96 char parameterName[MAX_TEMPORARY_STORAGE]; sprintf( parameterName, "%s__%s", procedure->name, procedure->parametersEach[i] );
97 Variable * parameter = variable_retrieve_or_define( _environment, parameterName, procedure->parametersTypeEach[i], 0 );
98 Variable * value = variable_retrieve( _environment, _environment->parametersEach[i] );
99 variable_move( _environment, value->name, parameter->name );
100 } else {
101 char parameterName[MAX_TEMPORARY_STORAGE]; sprintf( parameterName, "%s__%s", procedure->name, procedure->parametersEach[i] );
102 Variable * parameter = variable_retrieve_or_define( _environment, parameterName, procedure->parametersTypeEach[i], 0 );
103 variable_store( _environment, parameter->name, _environment->parametersValueEach[i] );
104 }
105 }
106 _environment->parameters = 0;
107
108 _environment->anyProtothread = 1;
109
110 if ( _environment->protothreadForbid ) {
112 }
113
114 cpu_protothread_register( _environment, procedure->realName, threadId->realName );
116 cpu_protothread_save( _environment, threadId->realName, 0 );
117
118 return threadId;
119
120}
121
void cpu_protothread_save(Environment *_environment, char *_index, int _step)
Definition 6309.c:6203
void cpu_protothread_set_state(Environment *_environment, char *_index, int _state)
Definition 6309.c:6226
void cpu_protothread_register(Environment *_environment, char *_label, char *_index)
Definition 6309.c:6181
Variable * variable_retrieve(Environment *_environment, char *_name)
Variable * variable_retrieve_or_define(Environment *_environment, char *_name, VariableType _type, int _value)
Variable * variable_move(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable by converting it.
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * variable_store(Environment *_environment, char *_destination, unsigned int _value)
Store a direct value to a variable.
Variable * spawn_procedure(Environment *_environment, char *_name, int _halted)
Emit code for SPAWN ....
Definition spawn.c:68
char * parametersEach[MAX_PARAMETERS]
Definition ugbc.h:2790
int anyProtothread
Definition ugbc.h:2835
int parameters
Definition ugbc.h:2785
int parametersValueEach[MAX_PARAMETERS]
Definition ugbc.h:2805
Procedure * procedures
Definition ugbc.h:2621
int protothreadForbid
Definition ugbc.h:2845
char * parametersEach[MAX_PARAMETERS]
Definition ugbc.h:1269
struct _Procedure * next
Definition ugbc.h:1327
int protothread
Definition ugbc.h:1289
char * name
Definition ugbc.h:1256
VariableType parametersTypeEach[MAX_PARAMETERS]
Definition ugbc.h:1284
int parameters
Definition ugbc.h:1264
char * realName
Definition ugbc.h:1259
char * name
Definition ugbc.h:979
char * realName
Definition ugbc.h:982
#define PROTOTHREAD_STATUS_ENDED
Definition ugbc.h:4550
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
#define PROTOTHREAD_STATUS_WAITING
Definition ugbc.h:4546
struct _Variable Variable
Structure of a single variable.
#define CRITICAL_PROCEDURE_MISSING(n)
Definition ugbc.h:3483
struct _Environment Environment
Structure of compilation environment.
@ VT_THREAD
Definition ugbc.h:492
#define CRITICAL_MULTITASKING_FORBIDDEN()
Definition ugbc.h:3703
#define CRITICAL_PROCEDURE_CANNOT_BE_INVOKED(c)
Definition ugbc.h:3522
#define CRITICAL_PROCEDURE_PARAMETERS_MISMATCH(n, d1, d2)
Definition ugbc.h:3484
struct _Procedure Procedure