/[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 203 - (show annotations) (download) (as text)
Mon Jan 23 04:13:17 2006 UTC (18 years, 5 months ago) by johnpye
File MIME type: text/x-csrc
File size: 9450 byte(s)
Documentation changes; making 'FIX' statement less verbose;
Commenting out ununsed 'CreateExternalFunction' function
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 #ifdef THIS_IS_AN_UNUSED_FUNCTION
46 struct ExternalFunc *CreateExternalFunc(CONST char *name)
47 {
48 struct ExternalFunc *result;
49 result = (struct ExternalFunc *)ascmalloc(sizeof(struct ExternalFunc));
50 if (result == NULL) {
51 return NULL;
52 }
53 result->name = name;
54 result->n_inputs = 0;
55 result->n_outputs = 0;
56 result->help = NULL;
57 result->value = NULL;
58 result->deriv = NULL;
59 result->deriv2 = NULL;
60 return result;
61 }
62 #endif /*THIS_IS_AN_UNUSED_FUNCTION */
63
64
65 int CreateUserFunction(CONST char *name,
66 ExtEvalFunc *init,
67 ExtEvalFunc **value,
68 ExtEvalFunc **deriv,
69 ExtEvalFunc **deriv2,
70 CONST unsigned long n_inputs,
71 CONST unsigned long n_outputs,
72 CONST char *help)
73 {
74 struct ExternalFunc *efunc;
75 if (name == NULL) {
76 return 1;
77 }
78 efunc = LookupExtFunc(name);
79 if (efunc != NULL) { /* name was pre-loaded -- just update the info */
80 efunc->n_inputs = n_inputs;
81 efunc->n_outputs = n_outputs;
82 efunc->init = init;
83 efunc->value = value; /* should be *value */
84 efunc->deriv = deriv; /* should be *deriv */
85 efunc->deriv2 = deriv2; /* should be *deriv2 */
86 if (help) {
87 if (efunc->help) ascfree((char *)efunc->help);
88 efunc->help = (char *)ascmalloc((strlen(help)+1)*sizeof(char));
89 asc_assert(efunc->help != NULL);
90 strcpy(efunc->help,help);
91 }
92 else
93 efunc->help = NULL;
94 } else {
95 efunc = (struct ExternalFunc *)ascmalloc(sizeof(struct ExternalFunc));
96 asc_assert(efunc!=NULL);
97 efunc->name = SCP(AddSymbol(name)); /* add or find name in symbol table */
98 /* the main symtab owns the string */
99 efunc->n_inputs = n_inputs;
100 efunc->n_outputs = n_outputs;
101 efunc->init = init;
102 efunc->value = value; /* should be *value */
103 efunc->deriv = deriv; /* should be *deriv */
104 efunc->deriv2 = deriv2; /* should be *deriv2 */
105 if (help) {
106 efunc->help = (char *)ascmalloc((strlen(help)+1)*sizeof(char));
107 asc_assert(efunc->help != NULL);
108 strcpy(efunc->help,help);
109 }
110 else
111 efunc->help = NULL;
112 (void)AddExternalFunc(efunc,1);
113 }
114 return 0;
115 }
116
117 void DestroyExternalFunc(struct ExternalFunc *efunc)
118 {
119 struct ExternalFunc *tmp;
120 if (efunc) {
121 tmp = efunc;
122 tmp->name = NULL; /* the main symbol table owns the string */
123 if (tmp->help) ascfree(tmp->help); /* we own the string */
124 tmp->help = NULL;
125 tmp->init = NULL;
126 tmp->value = NULL;
127 tmp->deriv = NULL;
128 tmp->deriv2 = NULL;
129 ascfree((char *)tmp);
130 efunc = NULL;
131 }
132 }
133
134 /*
135 * This is simply a function returning a pointer to a function.
136 * In this case the evaluation function. see Harbison&Steele p 258. :^)
137 */
138
139 int (*GetInitFunc(struct ExternalFunc *efunc))(/* */)
140 {
141 asc_assert(efunc!=NULL);
142 return efunc->init;
143 }
144
145 ExtBBoxFunc *GetValueFunc(struct ExternalFunc *efunc)
146 {
147 asc_assert(efunc!=NULL);
148 return (ExtBBoxFunc *)efunc->value;
149 }
150
151
152 ExtBBoxFunc *GetDerivFunc(struct ExternalFunc *efunc)
153 {
154 asc_assert(efunc!=NULL);
155 return (ExtBBoxFunc *)efunc->deriv;
156 }
157
158 ExtBBoxFunc *GetDeriv2Func(struct ExternalFunc *efunc)
159 {
160 asc_assert(efunc!=NULL);
161 return (ExtBBoxFunc *)efunc->deriv2;
162 }
163
164 /*
165 * GlassBox relations in particular register not just
166 * a single function but rather a pointer to a jump table
167 * of functions. There will be a jump table ptr for each
168 * of value, deriv, deriv2.
169 */
170
171 /*
172 * The following means:
173 * GetValue is a function that returning pointer to array[] of
174 * pointer to functions, which take args and return an int.
175 *
176 * int (*(*GetValueJumpTable(struct ExternalFunc *efunc))[])(args)
177 */
178
179 ExtEvalFunc **GetValueJumpTable(struct ExternalFunc *efunc)
180 {
181 asc_assert(efunc!=NULL);
182 return efunc->value; /* error, efunc->value is not an array of pointers */
183 }
184
185 ExtEvalFunc **GetDerivJumpTable(struct ExternalFunc *efunc)
186 {
187 asc_assert(efunc!=NULL);
188 return efunc->deriv; /* error, efunc->value is not an array of pointers */
189 }
190
191 #ifdef THIS_IS_AN_UNUSED_FUNCTION
192 static
193 ExtEvalFunc **GetValueDeriv2Table(struct ExternalFunc *efunc)
194 {
195 asc_assert(efunc!=NULL);
196 return efunc->deriv2; /* error, efunc->value is not an array of pointers */
197 }
198 #endif /* THIS_IS_AN_UNUSED_FUNCTION */
199
200
201 CONST char *ExternalFuncName(CONST struct ExternalFunc *efunc)
202 {
203 asc_assert(efunc!=NULL);
204 return efunc->name;
205 }
206
207 unsigned long NumberInputArgs(CONST struct ExternalFunc *efunc)
208 {
209 asc_assert(efunc!=NULL);
210 return efunc->n_inputs;
211 }
212
213 unsigned long NumberOutputArgs(CONST struct ExternalFunc *efunc)
214 {
215 asc_assert(efunc!=NULL);
216 return efunc->n_outputs;
217 }
218
219 /*
220 * These are the table management routines.
221 */
222
223 void InitExternalFuncLibrary(void)
224 {
225 struct Table *result;
226 result = CreateTable(EXTFUNCHASHSIZE); /* this isn't destroyed at end. fix.*/
227 ExternalFuncLibrary = result;
228 }
229
230 int AddExternalFunc(struct ExternalFunc *efunc, int force)
231 {
232
233 struct ExternalFunc *found, *tmp;
234 char *name;
235
236 asc_assert(efunc!=NULL);
237 name = (char *)efunc->name;
238 found = (struct ExternalFunc *)LookupTableData(ExternalFuncLibrary,name);
239 if (found) { /* function name already exists */
240 if (force==0)
241 return 0;
242 else{ /* need to update information */
243 tmp = (struct ExternalFunc *)RemoveTableData(ExternalFuncLibrary,name);
244 DestroyExternalFunc(tmp);
245 AddTableData(ExternalFuncLibrary,(void *)efunc,name);
246 return 1;
247 }
248 }
249 else{ /* need to add function to library */
250 AddTableData(ExternalFuncLibrary,(void *)efunc,name);
251 return 1;
252 }
253 }
254
255 struct ExternalFunc *LookupExtFunc(CONST char *funcname)
256 {
257 struct ExternalFunc *found;
258 if (!funcname) {
259 return NULL;
260 }
261 found = (struct ExternalFunc *)
262 LookupTableData(ExternalFuncLibrary,funcname);
263 if (found) {
264 return found;
265 } else {
266 return NULL; /* name not found */
267 }
268 }
269
270 struct ExternalFunc *RemoveExternalFunc(char *funcname)
271 {
272 struct ExternalFunc *found;
273 if (!funcname)
274 return NULL;
275 found = (struct ExternalFunc *)
276 RemoveTableData(ExternalFuncLibrary,funcname);
277 return found;
278 }
279
280
281 static
282 void ExternalFuncDestroyFunc(void *efunc)
283 {
284 struct ExternalFunc *local;
285 local = (struct ExternalFunc *)efunc;
286 if (local)
287 DestroyExternalFunc(local);
288 }
289
290 void DestroyExtFuncLibrary(void)
291 {
292 TableApplyAll(ExternalFuncLibrary,
293 (TableIteratorOne)ExternalFuncDestroyFunc);
294 DestroyTable(ExternalFuncLibrary,0);
295 ExternalFuncLibrary = NULL;
296 }
297
298 static
299 void PrintExtFuncLibraryFunc(void *efunc, void *fp)
300 {
301 struct ExternalFunc *local_efunc = (struct ExternalFunc *)efunc;
302
303 if (local_efunc!=NULL) {
304 FPRINTF(fp,"%s\n",ExternalFuncName(local_efunc));
305 if (local_efunc->help) {
306 FPRINTF(fp,"%s\n",local_efunc->help);
307 } else {
308 FPRINTF(fp,"No help information available for this function\n");
309 }
310 }
311 }
312
313 void PrintExtFuncLibrary(FILE *fp)
314 {
315 if (!fp) {
316 FPRINTF(ASCERR,"Invalid file handle in PrintExtFuncLibrary\n");
317 return;
318 }
319 TableApplyAllTwo(ExternalFuncLibrary, PrintExtFuncLibraryFunc,
320 (void *)fp);
321 }
322
323 static
324 void WriteExtFuncString(struct ExternalFunc *efunc, Asc_DString *dsPtr)
325 {
326 if (efunc!=NULL) {
327 Asc_DStringAppend(dsPtr,"{{",2);
328 Asc_DStringAppend(dsPtr,ExternalFuncName(efunc),-1);
329 Asc_DStringAppend(dsPtr,"} {",3);
330 if (efunc->help!=NULL) {
331 Asc_DStringAppend(dsPtr,efunc->help,-1);
332 } else {
333 Asc_DStringAppend(dsPtr,"No help available.",18);
334 }
335 Asc_DStringAppend(dsPtr,"}} ",3);
336 }
337 }
338
339 char *WriteExtFuncLibraryString(void)
340 {
341 char *result;
342 Asc_DString ds, *dsPtr;
343 dsPtr = &ds;
344 Asc_DStringInit(dsPtr);
345 TableApplyAllTwo(ExternalFuncLibrary,(TableIteratorTwo)WriteExtFuncString,
346 (void *) dsPtr);
347 result = Asc_DStringResult(dsPtr);
348 return result;
349 }
350
351 void
352 TraverseExtFuncLibrary(void (*func)(void *,void *), void *secondparam){
353 TableApplyAllTwo(ExternalFuncLibrary, func, secondparam);
354 }

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