[comp.binaries.apple2] Alternate Applesoft Evaluator

kadickey@phoenix.Princeton.EDU (Kent Andrew Dickey) (08/28/89)

I just saw the post of that function evaluator just posted.  In summary,
it let you input a valid AppleSoft function and it would set a function
equal to that expression.  There are two main disadvantages to that
approach:

1) You have a self-modifying program, which means you must
leave plenty of space on the line that defines the function.  In the
original sample program, the line was merely this:

20 DEF FN F(X) = :::::::::::::::

If you enter an expression that takes more than 16 spaces in memory
(note that Applesoft keywords, like COS and SIN, all only take 1 byte in
memory), then your program will be destroyed.  That line should contain
at least 200 or so colons, to be more bullet-proof.

2)  The function MUST be entered at the keyboard.  The previous program
glosses over that fact quite nicely, but it actually reads the entered
function from the keyboard buffer.  So, a program like this wouldn't
work:

100 INPUT "Enter Function";F$
110 INPUT "Plot now??";Y$
120 IF Y$="Y" THEN & F: GOSUB Plot_Routine

The & F part (which redefines the function F to be what's in the keyboard
buffer), would set the function to = "Y".  Not quite what you had in mind,
right?  This limitation makes implementing multiple input functions
difficult, and having a variable amount nearly impossible.

So, I present my alternative, more powerful expression evaluator.  Rather
than merely evaluating equations, this program actually can perform any
Applesoft command!  That's right, F$="LIST":&F$  would list the program!

--------Snip-----------Snip-------------Snip-----------------
CALL -151
0300:A9 4C 8D F5 03 A9 10 8D F6 03 A9 03 8D F7 03 60
0310:20 B7 00 C9 22 D0 1B A0 00 20 B1 00 99 00 02 F0
0320:3A C8 C9 22 D0 F3 88 A9 00 99 00 02 20 B1 00 4C
0330:5B 03 20 E3 DF 85 85 84 86 24 11 70 03 4C 76 DD
0340:A0 02 B1 85 99 55 00 88 10 F8 A0 00 B1 56 99 00
0350:02 C8 C4 55 90 F6 A9 00 99 00 02 A5 B8 8D 8F 03
0360:A5 B9 8D 90 03 A5 76 8D 91 03 A9 FF 85 76 A9 00
0370:85 B8 A9 02 85 B9 20 59 D5 20 B1 00 20 28 D8 AD
0380:91 03 85 76 AD 90 03 85 B9 AD 8F 03 85 B8 60
BSAVE EXPR.EVAL,A$300,L$8F
--------Snip-----------Snip-------------Snip----------------

How to use it:

Just BRUN EVALUATOR and it patches itself into the ampersand vector.
If you need it to co-reside with other ampersand patches, send me some
mail and I'll see what can be done.

Just use the form & A$  or &"K=999.7" and it will perform the instructions
as if you had typed them from the keyboard.

It can evaluate a single string command, of the form & F$, or a
text string of the form &"VTAB 12".  The support for evaluating text
strings is actually pretty useless, and takes a fair amount of code to
handle, but I thought I'd include it anyways.  You CANNOT combine
the & expression as you would in a print statement: &F$+G$ doesn't
work, and neither does &"A=";F$.  Do the string concatenation step
before the & statement.

The executed statement is done in Applesoft's immediate mode--that is,
I make Applesoft think that you just entered the command at the keyboard.
I haven't tested how that affects ONERR GOTO routines, but they should
work OK.  Also, the error returned will be the genuine Applesoft error,
whatever it was (Formula too complex, etc).


Thus, to evalute formulas, you'd perform something like the following:

10  PRINT CHR$(4);"BRUN EXPR.EVAL"
20  INPUT "Enter a formula, must be in Applesoft syntax:";F$
30  F$="AN="+F$
40  & F$
50  PRINT AN


Although it should let you perform any operation at all, &"LIST" and
other similar comands seem to stop program execution at that point.  This
should not be too big of a problem.

Also, very important:  It doesn't work in immediate mode.  That is, you
can't type:  &"h=8" at the ] prompt.  It only works within a program.
This is because the program uses the keyboard buffer temporarily, which
which is where your originally typed command is stored!

This program was rushed out in a few hours, without too much extensive
testing.  If you find anything odd happening, just let me know.

                        Kent Dickey
kadickey@phoenix.Princeton.Edu
kadickey@PUCC