/[ascend]/trunk/base/generic/compiler/extfunc.c
ViewVC logotype

Contents of /trunk/base/generic/compiler/extfunc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 101 - (show annotations) (download) (as text)
Sat Dec 10 04:22:07 2005 UTC (15 years, 4 months ago) by jds
File MIME type: text/x-csrc
File size: 9377 byte(s)
A little more progress killing compiler warnings.
1 /*
2 * External Functions Module
3 * by Kirk Andre Abbott
4 * Created: July 4, 1994.
5 * Version: $Revision: 1.8 $
6 * Version control file: $RCSfile: extfunc.c,v $
7 * Date last modified: $Date: 1998/02/05 22:23:26 $
8 * Last modified by: $Author: ballan $
9 *
10 * This file is part of the Ascend Language Interpreter.
11 *
12 * Copyright (C) 1990, 1993, 1994 Thomas Guthrie Epperly, Kirk Andre Abbott
13 *
14 * The Ascend Language Interpreter is free software; you can redistribute
15 * it and/or modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * The Ascend Language Interpreter is distributed in hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with the program; if not, write to the Free Software Foundation,
26 * Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named
27 * COPYING.
28 */
29
30 #include "utilities/ascConfig.h"
31 #include "utilities/ascMalloc.h"
32 #include "utilities/ascPanic.h"
33 #include "general/hashpjw.h"
34 #include "general/list.h"
35 #include "general/table.h"
36 #include "general/dstring.h"
37 #include "compiler/compiler.h"
38 #include "compiler/symtab.h"
39 #include "compiler/extfunc.h"
40
41 #define EXTFUNCHASHSIZE 31
42
43 static struct Table *ExternalFuncLibrary = NULL;
44
45 struct ExternalFunc *CreateExternalFunc(CONST char *name)
46 {
47 struct ExternalFunc *result;
48 result = (struct ExternalFunc *)ascmalloc(sizeof(struct ExternalFunc));
49 if (result == NULL) {
50 return NULL;
51 }
52 result->name = name;
53 result->n_inputs = 0;
54 result->n_outputs = 0;
55 result->help = NULL;
56 result->value = NULL;
57 result->deriv = NULL;
58 result->deriv2 = NULL;
59 return result;
60 }
61
62
63 int CreateUserFunction(CONST char *name,
64 ExtEvalFunc *init,
65 ExtEvalFunc **value,
66 ExtEvalFunc **deriv,
67 ExtEvalFunc **deriv2,
68 CONST unsigned long n_inputs,
69 CONST unsigned long n_outputs,
70 CONST char *help)
71 {
72 struct ExternalFunc *efunc;
73 if (name == NULL) {
74 return 1;
75 }
76 efunc = LookupExtFunc(name);
77 if (efunc != NULL) { /* name was pre-loaded -- just update the info */
78 efunc->n_inputs = n_inputs;
79 efunc->n_outputs = n_outputs;
80 efunc->init = init;
81 efunc->value = value; /* should be *value */
82 efunc->deriv = deriv; /* should be *deriv */
83 efunc->deriv2 = deriv2; /* should be *deriv2 */
84 if (help) {
85 if (efunc->help) ascfree((char *)efunc->help);
86 efunc->help = (char *)ascmalloc((strlen(help)+1)*sizeof(char));
87 asc_assert(efunc->help != NULL);
88 strcpy(efunc->help,help);
89 }
90 else
91 efunc->help = NULL;
92 } else {
93 efunc = (struct ExternalFunc *)ascmalloc(sizeof(struct ExternalFunc));
94 asc_assert(efunc!=NULL);
95 efunc->name = SCP(AddSymbol(name)); /* add or find name in symbol table */
96 /* the main symtab owns the string */
97 efunc->n_inputs = n_inputs;
98 efunc->n_outputs = n_outputs;
99 efunc->init = init;
100 efunc->value = value; /* should be *value */
101 efunc->deriv = deriv; /* should be *deriv */
102 efunc->deriv2 = deriv2; /* should be *deriv2 */
103 if (help) {
104 efunc->help = (char *)ascmalloc((strlen(help)+1)*sizeof(char));
105 asc_assert(efunc->help != NULL);
106 strcpy(efunc->help,help);
107 }
108 else
109 efunc->help = NULL;
110 (void)AddExternalFunc(efunc,1);
111 }
112 return 0;
113 }
114
115 void DestroyExternalFunc(struct ExternalFunc *efunc)
116 {
117 struct ExternalFunc *tmp;
118 if (efunc) {
119 tmp = efunc;
120 tmp->name = NULL; /* the main symbol table owns the string */
121 if (tmp->help) ascfree(tmp->help); /* we own the string */
122 tmp->help = NULL;
123 tmp->init = NULL;
124 tmp->value = NULL;
125 tmp->deriv = NULL;
126 tmp->deriv2 = NULL;
127 ascfree((char *)tmp);
128 efunc = NULL;
129 }
130 }
131
132 /*
133 * This is simply a function returning a pointer to a function.
134 * In this case the evaluation function. see Harbison&Steele p 258. :^)
135 */
136
137 int (*GetInitFunc(struct ExternalFunc *efunc))(/* */)
138 {
139 asc_assert(efunc!=NULL);
140 return efunc->init;
141 }
142
143 ExtBBoxFunc *GetValueFunc(struct ExternalFunc *efunc)
144 {
145 asc_assert(efunc!=NULL);
146 return (ExtBBoxFunc *)efunc->value;
147 }
148
149
150 ExtBBoxFunc *GetDerivFunc(struct ExternalFunc *efunc)
151 {
152 asc_assert(efunc!=NULL);
153 return (ExtBBoxFunc *)efunc->deriv;
154 }
155
156 ExtBBoxFunc *GetDeriv2Func(struct ExternalFunc *efunc)
157 {
158 asc_assert(efunc!=NULL);
159 return (ExtBBoxFunc *)efunc->deriv2;
160 }
161
162 /*
163 * GlassBox relations in particular register not just
164 * a single function but rather a pointer to a jump table
165 * of functions. There will be a jump table ptr for each
166 * of value, deriv, deriv2.
167 */
168
169 /*
170 * The following means:
171 * GetValue is a function that returning pointer to array[] of
172 * pointer to functions, which take args and return an int.
173 *
174 * int (*(*GetValueJumpTable(struct ExternalFunc *efunc))[])(args)
175 */
176
177 ExtEvalFunc **GetValueJumpTable(struct ExternalFunc *efunc)
178 {
179 asc_assert(efunc!=NULL);
180 return efunc->value; /* error, efunc->value is not an array of pointers */
181 }
182
183 ExtEvalFunc **GetDerivJumpTable(struct ExternalFunc *efunc)
184 {
185 asc_assert(efunc!=NULL);
186 return efunc->deriv; /* error, efunc->value is not an array of pointers */
187 }
188
189 #ifdef THIS_IS_AN_UNUSED_FUNCTION
190 static
191 ExtEvalFunc **GetValueDeriv2Table(struct ExternalFunc *efunc)
192 {
193 asc_assert(efunc!=NULL);
194 return efunc->deriv2; /* error, efunc->value is not an array of pointers */
195 }
196 #endif /* THIS_IS_AN_UNUSED_FUNCTION */
197
198
199 CONST char *ExternalFuncName(CONST struct ExternalFunc *efunc)
200 {
201 asc_assert(efunc!=NULL);
202 return efunc->name;
203 }
204
205 unsigned long NumberInputArgs(CONST struct ExternalFunc *efunc)
206 {
207 asc_assert(efunc!=NULL);
208 return efunc->n_inputs;
209 }
210
211 unsigned long NumberOutputArgs(CONST struct ExternalFunc *efunc)
212 {
213 asc_assert(efunc!=NULL);
214 return efunc->n_outputs;
215 }
216
217 /*
218 * These are the table management routines.
219 */
220
221 void InitExternalFuncLibrary(void)
222 {
223 struct Table *result;
224 result = CreateTable(EXTFUNCHASHSIZE); /* this isn't destroyed at end. fix.*/
225 ExternalFuncLibrary = result;
226 }
227
228 int AddExternalFunc(struct ExternalFunc *efunc, int force)
229 {
230
231 struct ExternalFunc *found, *tmp;
232 char *name;
233
234 asc_assert(efunc!=NULL);
235 name = (char *)efunc->name;
236 found = (struct ExternalFunc *)LookupTableData(ExternalFuncLibrary,name);
237 if (found) { /* function name already exists */
238 if (force==0)
239 return 0;
240 else{ /* need to update information */
241 tmp = (struct ExternalFunc *)RemoveTableData(ExternalFuncLibrary,name);
242 DestroyExternalFunc(tmp);
243 AddTableData(ExternalFuncLibrary,(void *)efunc,name);
244 return 1;
245 }
246 }
247 else{ /* need to add function to library */
248 AddTableData(ExternalFuncLibrary,(void *)efunc,name);
249 return 1;
250 }
251 }
252
253 struct ExternalFunc *LookupExtFunc(CONST char *funcname)
254 {
255 struct ExternalFunc *found;
256 if (!funcname) {
257 return NULL;
258 }
259 found = (struct ExternalFunc *)
260 LookupTableData(ExternalFuncLibrary,funcname);
261 if (found) {
262 return found;
263 } else {
264 return NULL; /* name not found */
265 }
266 }
267
268 struct ExternalFunc *RemoveExternalFunc(char *funcname)
269 {
270 struct ExternalFunc *found;
271 if (!funcname)
272 return NULL;
273 found = (struct ExternalFunc *)
274 RemoveTableData(ExternalFuncLibrary,funcname);
275 return found;
276 }
277
278
279 static
280 void ExternalFuncDestroyFunc(void *efunc)
281 {
282 struct ExternalFunc *local;
283 local = (struct ExternalFunc *)efunc;
284 if (local)
285 DestroyExternalFunc(local);
286 }
287
288 void DestroyExtFuncLibrary(void)
289 {
290 TableApplyAll(ExternalFuncLibrary,
291 (TableIteratorOne)ExternalFuncDestroyFunc);
292 DestroyTable(ExternalFuncLibrary,0);
293 ExternalFuncLibrary = NULL;
294 }
295
296 static
297 void PrintExtFuncLibraryFunc(void *efunc, void *fp)
298 {
299 struct ExternalFunc *local_efunc = (struct ExternalFunc *)efunc;
300
301 if (local_efunc!=NULL) {
302 FPRINTF(fp,"%s\n",ExternalFuncName(local_efunc));
303 if (local_efunc->help) {
304 FPRINTF(fp,"%s\n",local_efunc->help);
305 } else {
306 FPRINTF(fp,"No help information available for this function\n");
307 }
308 }
309 }
310
311 void PrintExtFuncLibrary(FILE *fp)
312 {
313 if (!fp) {
314 FPRINTF(ASCERR,"Invalid file handle in PrintExtFuncLibrary\n");
315 return;
316 }
317 TableApplyAllTwo(ExternalFuncLibrary, PrintExtFuncLibraryFunc,
318 (void *)fp);
319 }
320
321 static
322 void WriteExtFuncString(struct ExternalFunc *efunc, Asc_DString *dsPtr)
323 {
324 if (efunc!=NULL) {
325 Asc_DStringAppend(dsPtr,"{{",2);
326 Asc_DStringAppend(dsPtr,ExternalFuncName(efunc),-1);
327 Asc_DStringAppend(dsPtr,"} {",3);
328 if (efunc->help!=NULL) {
329 Asc_DStringAppend(dsPtr,efunc->help,-1);
330 } else {
331 Asc_DStringAppend(dsPtr,"No help available.",18);
332 }
333 Asc_DStringAppend(dsPtr,"}} ",3);
334 }
335 }
336
337 char *WriteExtFuncLibraryString(void)
338 {
339 char *result;
340 Asc_DString ds, *dsPtr;
341 dsPtr = &ds;
342 Asc_DStringInit(dsPtr);
343 TableApplyAllTwo(ExternalFuncLibrary,(TableIteratorTwo)WriteExtFuncString,
344 (void *) dsPtr);
345 result = Asc_DStringResult(dsPtr);
346 return result;
347 }
348
349 void
350 TraverseExtFuncLibrary(void (*func)(void *,void *), void *secondparam){
351 TableApplyAllTwo(ExternalFuncLibrary, func, secondparam);
352 }

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22