Incorporating Users' Functions

David M. LaRocque larocque at jupiter.crd.ge.com
Tue Sep 18 05:10:19 AEST 1990


I'm looking for suggestions on the best technique to incorporate
user-written functions into a software package I'm developing.  
Specifically, I have helped write a large system in C that is
used by researchers to reason about events.  It is a system
that is used to develop other systems.  As such, there needs to
be a way to allow users to develop their own functions which can
in turn be accessed by the system.  The system was originally
written in Lisp.  In the Lisp version users can simply write their own
functions and add them to the "Lisp world".  The Lisp version can 
then "eval" the function name with the arguements.  I wish it
were that simple in C.

As background, the user creates english-like rules such as:

if (is-value-predicate X classification mammal)
then (set-value X respiration-type air-breathing).

The user creates a file of rules such as the one above.  The file
of rules is parsed by the yacc/lex and appropriate data structures
created.  For the above rule all objects that have a classification
of mammals would have their respiration-type set to air-breathing.
Simple functions like "is-value-predicate" and "set-value" are 
provided for the user.  However, the user might want to do something
more complicated like:

if (hairy-user-calculation X air-speed)
then (set-value X passed-hairy-test yes).

The problem is how do I allow the user to write the C function
"hairy-user-calculation" and have the system call it with the 
appropriate arguements.

Since the file of rules is parsed using yacc/lex, what I think I
need is a way for yacc/lex to grab a function pointer to
the function.  Another problem is that one doesn't know how
many arguements a given function might use, but perhaps that
could be dealt with with the varargs facility.

I suppose what might be ideal is to have the user create all his
functions in a separate file that is compiled into the main
program.  Then I think the problem becomes finding out what a
function's pointer is when one only knows the name of the function.

Thanks, Dave

/**************************************************
 * larocque at crd.ge.com        (518) 387-5805
 * ...!crdgw1!cetus.crd.ge.com!larocque
 **************************************************/



More information about the Comp.lang.c mailing list