Menu

[r5829]: / trunk / fuse-basic / explist.c  Maximize  Restore  History

Download this file

206 lines (148 with data), 4.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* explist.c: a list of expressions
Copyright (c) 2002-2004 Philip Kendall
$Id$
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include "explist.h"
static void free_item( gpointer data, gpointer user_data );
error_t
expression_new_numexp( struct expression **expression,
struct numexp *numexp )
{
*expression = malloc( sizeof **expression );
if( !(*expression) ) return BASIC_ERROR_MEMORY;
(*expression)->type = EXPRESSION_NUMEXP;
(*expression)->types.numexp = numexp;
return BASIC_ERROR_NONE;
}
error_t
expression_new_strexp( struct expression **expression,
struct strexp *strexp )
{
*expression = malloc( sizeof **expression );
if( !(*expression) ) return BASIC_ERROR_MEMORY;
(*expression)->type = EXPRESSION_STREXP;
(*expression)->types.strexp = strexp;
return BASIC_ERROR_NONE;
}
error_t explist_new( struct explist **list )
{
*list = malloc( sizeof **list );
if( *list == NULL ) return BASIC_ERROR_MEMORY;
(*list)->items = NULL;
return BASIC_ERROR_NONE;
}
error_t
explist_append_numexp( struct explist *list, struct numexp *numexp )
{
struct expression *exp;
exp = malloc( sizeof *exp );
if( exp == NULL ) return BASIC_ERROR_MEMORY;
exp->type = EXPRESSION_NUMEXP;
exp->types.numexp = numexp;
list->items = g_slist_append( list->items, (gpointer)exp );
return BASIC_ERROR_NONE;
}
error_t
explist_append_strexp( struct explist *list, struct strexp *strexp )
{
struct expression *exp;
exp = malloc( sizeof *exp );
if( exp == NULL ) return BASIC_ERROR_MEMORY;
exp->type = EXPRESSION_STREXP;
exp->types.strexp = strexp;
list->items = g_slist_append( list->items, (gpointer)exp );
return BASIC_ERROR_NONE;
}
error_t
explist_append_forvar( struct explist *list, const char *name )
{
struct expression *exp; error_t error;
exp = malloc( sizeof *exp );
if( exp == NULL ) return BASIC_ERROR_MEMORY;
exp->type = EXPRESSION_NUMEXP;
error = numexp_new_variable( &(exp->types.numexp), name );
if( error ) { free( exp ); return error; }
list->items = g_slist_append( list->items, (gpointer)exp );
return BASIC_ERROR_NONE;
}
error_t
explist_append_strvar( struct explist *list, const char *name )
{
struct expression *exp; error_t error;
exp = malloc( sizeof *exp );
if( exp == NULL ) return BASIC_ERROR_MEMORY;
exp->type = EXPRESSION_STREXP;
error = strexp_new_variable( &(exp->types.strexp), name );
if( error ) { free( exp ); return error; }
list->items = g_slist_append( list->items, (gpointer)exp );
return BASIC_ERROR_NONE;
}
/* Convert a list of numeric expressions into an array of integer values */
error_t
explist_to_integer_array( struct program *program, struct explist *list,
size_t **values, size_t *length )
{
GSList *items;
size_t *ptr;
float value;
error_t error;
items = list->items;
*length = g_slist_length( items );
*values = malloc( *length * sizeof **values );
if( !*values ) return BASIC_ERROR_MEMORY;
ptr = *values;
while( items ) {
struct expression *exp;
exp = items->data;
if( exp->type != EXPRESSION_NUMEXP ) {
free( *values );
fprintf( stderr, "Internal error: non-numeric expression found at %s:%d",
__FILE__, __LINE__ );
return BASIC_ERROR_LOGIC;
}
error = numexp_eval( program, exp->types.numexp, &value );
if( error ) { free( *values ); return error; }
*ptr = value + 0.5;
items = items->next;
ptr++;
}
return BASIC_ERROR_NONE;
}
error_t
explist_free( struct explist *explist )
{
g_slist_foreach( explist->items, free_item, NULL );
g_slist_free( explist->items );
free( explist );
return BASIC_ERROR_NONE;
}
static void
free_item( gpointer data, gpointer user_data )
{
struct expression *expression = data;
switch( expression->type ) {
case EXPRESSION_NUMEXP: numexp_free( expression->types.numexp ); break;
case EXPRESSION_STREXP: strexp_free( expression->types.strexp ); break;
default:
fprintf( stderr, "Attempt to free unknown expression type %d\n",
expression->type );
break;
}
free( expression );
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.