ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
min.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
41extern char DATATYPE_AS_STRING[][16];
42
43
52/* <usermanual>
53@keyword MIN
54
55@english
56
57The ''MIN'' function allows you to identify the smaller of two values. If both terms
58are (numeric) constants, the evaluation is carried out at the time of compilation. Otherwise, the
59value is calculated at runtime.
60
61Where the data types are not identical, ugBASIC "promotes" the second term to the type
62of the first term. If the conversion is not possible, a specific error will be issued.
63The comparison of heterogeneous types is, in effect, a comparison of implicitly
64promoted types. In particular, static strings are always promoted to dynamic strings.
65
66There is also a variant of the function that accepts a single parameter. In this case,
67the parameter must be an array, and is a synonym for the ''ARRAY MIN'' command.
68
69@italian
70La funzione ''MIN'' consente di identificare il minore tra due valori. Se entrambi i termini
71sono costanti (numeriche), la valutazione viene effettuata al momento della compilazione.
72Altrimenti, il valore viene calcolato in fase di esecuzione.
73
74Laddove i tipi di dati non sono identici, ugBASIC "promuove" il secondo termine al tipo del
75primo termine. Se la conversione non è possibile, verrà emesso un errore specifico. Il
76confronto tra tipi eterogenei è, in effetti, un confronto tra tipi promossi implicitamente.
77In particolare, le stringhe statiche vengono sempre promosse a stringhe dinamiche.
78
79Esiste anche una variante della funzione che accetta un singolo parametro. In questo caso,
80il parametro deve essere un array ed è sinonimo del comando ''ARRAY MIN''.
81
82@syntax = MIN(#const1,#const2)
83@syntax = MIN(val1,val2)
84@syntax = MIN(string1,string2)
85@syntax = MIN(array)
86
87@example result = MIN( a, b )
88@example DIM v(21) AS INTEGER
89@example v(1) = 42: PRINT MIN( v ): ' it will be 21!
90
91@usedInExample maths_relative_02.bas
92
93</usermanual> */
94Variable * minimum( Environment * _environment, char * _source, char * _destination ) {
95
96 Variable * source = variable_retrieve( _environment, _source );
97 Variable * target = variable_retrieve( _environment, _destination );
98
99 if ( source->type == VT_STRING ) {
100 source = variable_cast( _environment, _source, VT_DSTRING );
101 }
102 if ( target->type == VT_STRING ) {
103 target = variable_cast( _environment, _destination, VT_DSTRING );
104 }
105
106 Variable * result = variable_temporary( _environment, source->type, "(result of MIN)");
107
109
110 char greaterThanLabel[MAX_TEMPORARY_STORAGE]; sprintf( greaterThanLabel, "%sl1", label );
111 char endLabel[MAX_TEMPORARY_STORAGE]; sprintf( endLabel, "%sl2", label );
112
113 Variable * compare = variable_less_than( _environment, source->name, target->name, 1 );
114
115 cpu_bveq( _environment, compare->realName, greaterThanLabel );
116
117 variable_move_naked( _environment, source->name, result->name );
118
119 cpu_jump( _environment, endLabel );
120
121 cpu_label( _environment, greaterThanLabel );
122
123 variable_move_naked( _environment, target->name, result->name );
124
125 cpu_label( _environment, endLabel );
126
127 return result;
128}
129
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(Environment *_environment, char *_name)
Variable * variable_less_than(Environment *_environment, char *_source, char *_destination, int _equal)
Compare two variable and return the result of comparation.
Variable * variable_move_naked(Environment *_environment, char *_source, char *_destination)
Store the value of a variable inside another variable without conversion.
Variable * variable_cast(Environment *_environment, char *_source, VariableType _type)
Cast a variable from a type to another.
Variable * variable_temporary(Environment *_environment, VariableType _type, char *_meaning)
Define a temporary variable.
Variable * minimum(Environment *_environment, char *_source, char *_destination)
Return the minimum value between two expressions.
Definition min.c:94
char * name
Definition ugbc.h:979
VariableType type
Definition ugbc.h:988
char * realName
Definition ugbc.h:982
#define MAX_TEMPORARY_STORAGE
Definition ugbc.h:563
struct _Variable Variable
Structure of a single variable.
struct _Environment Environment
Structure of compilation environment.
@ VT_STRING
Definition ugbc.h:474
@ VT_DSTRING
Definition ugbc.h:483
#define MAKE_LABEL
Definition ugbc.h:3351
char DATATYPE_AS_STRING[][16]