ugBASIC 1.18
An isomorphic BASIC language compiler for retrocomputers
Loading...
Searching...
No Matches
field_type.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
41void field_type( Environment * _environment, char * _name, VariableType _datatype ) {
42
43 if ( !_environment->currentType ) {
45 }
46
47 int dataTypefieldOffset = 0;
48 switch( VT_BITWIDTH( _datatype ) ) {
49 case 32:
50 dataTypefieldOffset = 4;
51 break;
52 case 16:
53 dataTypefieldOffset = 2;
54 break;
55 case 8:
56 dataTypefieldOffset = 1;
57 break;
58 case 1:
59 case 0: {
60 switch( _datatype ) {
61 case VT_DSTRING:
62 case VT_SPRITE:
63 dataTypefieldOffset = 1;
64 break;
65 case VT_MSPRITE:
66 dataTypefieldOffset = 2;
67 break;
68 default:
70 }
71 }
72 }
73
74 Field * field = malloc( sizeof( Field ) );
75 memset( field, 0, sizeof( Field ) );
76
77 int currentOffset = 0;
78 Field * current = _environment->currentType->first;
79 Field * last = NULL;
80 int fieldOffset;
81 if ( current ) {
82 while( current ) {
83 switch( VT_BITWIDTH( current->type ) ) {
84 case 32:
85 fieldOffset = 4;
86 break;
87 case 16:
88 fieldOffset = 2;
89 break;
90 case 8:
91 fieldOffset = 1;
92 break;
93 case 1:
94 case 0: {
95 switch( current->type ) {
96 case VT_DSTRING:
97 case VT_SPRITE:
98 fieldOffset = 1;
99 break;
100 case VT_MSPRITE:
101 fieldOffset = 2;
102 break;
103 default:
105 }
106 }
107 }
108 currentOffset = current->offset + fieldOffset;
109 last = current;
110 current = current->next;
111 }
112 }
113
114 field->name = strdup( _name );
115 field->type = _datatype;
116 field->offset = currentOffset;
117
118 _environment->currentType->size = currentOffset + dataTypefieldOffset;
119
120 if ( last ) {
121 last->next = field;
122 } else {
123 _environment->currentType->first = field;
124 }
125
126}
void field_type(Environment *_environment, char *_name, VariableType _datatype)
Definition field_type.c:41
Type * currentType
Definition ugbc.h:2540
char * name
Definition ugbc.h:1231
VariableType type
Definition ugbc.h:1233
struct _Field * next
Definition ugbc.h:1237
int offset
Definition ugbc.h:1235
int size
Definition ugbc.h:1245
struct _Field * first
Definition ugbc.h:1247
void * malloc(YYSIZE_T)
#define CRITICAL_CANNOT_DEFINE_OUTSIDE_TYPE(n)
Definition ugbc.h:3829
#define CRITICAL_CANNOT_USE_DATATYPE_IN_TYPE(n)
Definition ugbc.h:3830
struct _Field Field
struct _Environment Environment
Structure of compilation environment.
@ VT_MSPRITE
Definition ugbc.h:531
@ VT_SPRITE
Definition ugbc.h:501
@ VT_DSTRING
Definition ugbc.h:483
enum _VariableType VariableType
Type of variables.
#define VT_BITWIDTH(t)
Definition ugbc.h:595