FB II Compiler
Parse text for mathematical functions
Basically you're going to have to make a function and "parse" it out. Just a basic expression parser is below. It'll take two simple numbers and evaluate them, for example, 2*2 or 3-6 or 4/4444, even hex numbers like &Hff/&Hf, and a combination like 17*&Hf.
What you'll need to do is break down the math into simpler terms, for example:
Turn it into 5*4
and then into
Then you add variables, x,y,z, etc.
The key to doing it is breaking it into simpler parts, and recursing the input line until your line is blank. And then somehow you have to be able to handle errors too.
Keep in mind below is just a simple one that'll take two numbers and one math type(-,+,*,/)
Here it is:
INPUT "Enter your expression: ";aa$
multis% = INSTR(1,aa$,"*")
divis% = INSTR(1,aa$,"/")
addis% = INSTR(1,aa$,"+")
subis% = INSTR(1,aa$,"-")
relation% = -1
IF multis% THEN relation% = multis%:rtype% = 1
IF divis% THEN relation% = divis%:rtype% = 2
IF addis% THEN relation% = addis%:rtype% = 3
IF subis% THEN relation% = subis%:rtype% = 4
LONG IF relation% <> -1
leftexp$ = LEFT$(aa$,relation% - 1)
rightexp$ = MID$(aa$,relation% + 1)
lexp& = VAL(leftexp$)
rexp& = VAL(rightexp$)
SELECT CASE rtype%
ans# = lexp&*rexp&
ans# = lexp&/rexp&
ans# = lexp&+rexp&
ans# = lexp&-rexp&
PRINT "Answer: ";ans#
PRINT "Work with me here people..."
UNTIL FN BUTTON