sheaf@helios (Sheaf) (06/20/91)
Here is a programming problem that's been driving me nuts for some time now, and I'm hoping someone can shed some light on this for me. I use several languge compilers including Pascal and several versions of BASIC, but it's sort of unimportant since my problem is the same with all of them. I am developing some mathematical software utilities, and very simply, I want the user to be able to enter a function on the screen and have it graphed by the program. Sounds simple enough, and I have some great 2D and 3D graphing routines, but the problem is this: in order to graph something, you have to define it as a function, and while the coefficients in the DEFINE statement can be variables, the expression itself has to be hardcoded. Also, the DEFINE statements can't define a function from a string variable with the expression stored in it. So, to put it simply, is there any way (in any language) to define a function bby user input ? I know it can be done, I have software that does exactly this, but for the life of me I can't figure out how. If you have any tips, please E-mail me. Steve Sheaffer sheaf@groucho.phyast.pitt.edu
rhyde@koufax.ucr.edu (randy hyde) (06/22/91)
Several languages, generally interpreters, provide an "execute" statement. You supply a string which contains a valid statement and it parses and executes that statement. Some older versions of BASIC do this. SNOBOL4 does this. LISP does this. Etc. Forget Pascal and C. I don't know of any interpreters for these languages that allow this. Of course, you could always write your own expression evaluator (you could use FLEX & BISON to generate the code for you) and link this into your program. *** Randy Hyde
pshuang@athena.mit.edu (Ping-Shun Huang) (06/22/91)
In article <142295@unix.cis.pitt.edu> sheaf@helios (Sheaf) writes: > So, to put it simply, is there any way (in any language) to define a > function by user input? Hmm... you're using Pascal and BASIC, right? I don't suppose switching to a language like Scheme or Lisp is a viable option, right? If you're using interpreted BASIC, it should be possible for you to create a text file on disk with the user's input, like this: 30000 DEF FN(X)=X^2+3*X^3 ; can't remember correct syntax Depending on which BASIC you are using, it may be possible for you to MERGE that line into your program. You could then use the defined numeric function as you please. In Pascal this would be much more difficult, unless you're running interpreted Pascal. What you really want, and which is available in various, more exotic languages (i.e. C and assembler and BASIC and Pascal don't have it directly {grin}) is an eval function, which would evaluate any expression which is legal in the language itself. (I could tell some stories about one of the projects at the end of the term for the first computer science class at MIT... writing a Scheme interpreter in Scheme itself, i.e. creating a meta-circular evaluator.) In your case, you would only need an eval function capable of parsing arithmetic and algebraic expressions, which is still not trivial unless you're willing to ask your users to adopt postfix or prefix notation rather than infix. Essentially you would have to write a recursive routine which would take user input as a string, break it up into the constituent tokens, and then interpret the tokens on the fly everytime your graphing application needed the function. -- Above text where applicable is (c) Copyleft 1991, all rights deserved by: UNIX:/etc/ping instantiated (Ping Huang) [INTERNET: pshuang@athena.mit.edu]
rennyk@apex.com (Renny K) (06/26/91)
In article <PSHUANG.91Jun21171356@beeblebrox.mit.edu> pshuang@athena.mit.edu (Ping-Shun Huang) writes: >In article <142295@unix.cis.pitt.edu> sheaf@helios (Sheaf) writes: > > > So, to put it simply, is there any way (in any language) to define a > > function by user input? > >Hmm... you're using Pascal and BASIC, right? I don't suppose switching If you're using Pascal or C, it is best to write an evaluator. (I had to do this once and it is not that hard). There might be sources for such in PD, but not that I know of. If you are indeed talking about C or Pascal, e-mail me. I might be able to dig up the source (I KNOW I have the C one, the pascal... I'll check for) Renny -- ------------------------------------------------------------------------------- Renny Koshy rennyk@apex.com Apex Computer, Redmond, WA.