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