Restored from previous forum. Originally posted by teachco.
In Casio-Basic (Casio pocket computer) I found the function VALF
Example:
X$ = "123+456"
result = VALF(X$)
print result
Output:
579
ATTENTION! In PB the function VALF has another functionality!!!
For a possible realisation of this we should use 'ValExpr'.
With ValExpr we could interpret a string as a statement. A nice meta-function! Is'nt it? Is this possible in purebasic?
So we would be able to interpret (or jit (just in time compiling)) pb-code at runtime. This is something like a second language-level for special applications - for example you can write a learning environment within a PB-application or you can offer a pb programming course online via the internet. But - I think - , then we have a security problem.
So - at this moment - it is a discussion point.
New idea: ValExpr
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by tinman.
I think a ValExpr function would be a nice idea, but not to the extent where you could run any line of PB code.
Of course, this is just my opinion.
--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.20)
There would be a big difference between simple maths strings and being able to run any line of PB code. For example, your program would need every single library linked with it. You'd maybe also need the compiler (or parts of it). And how to manage variables which only get created in the lines of PB code that you execute within your strings? Oh, and dealing with loops (possibly unterminated) wouldn't be fun.So we would be able to interpret (or jit (just in time compiling)) pb-code at runtime.
I think a ValExpr function would be a nice idea, but not to the extent where you could run any line of PB code.
Of course, this is just my opinion.
--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.20)
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by fweil.
I coded soft apps formerly, was not with PB, using such features for user requirements as they needed to enter arithmetical expressions to put it to a math solver.
The way to do this is to create a arithmetical expression interpretor able to parse expressions and to calculate results.
It should not take more than some 100s of code lines in PB having all parenthezised and functions notations.
It is somewhere a bit hard to do, but if it is more than two interested people I agree to come back on that subject giving a procedure able to return the result value of any normalized expression.
The only money I would like back is to have double floats in PB and any volunteer contribution in USD or EUR.
Frankly it is a job but I have all my sources just to try to adapt. So it should not take more than 2 or 3 days to make a first level parser.
Tell me about ...
Francois Weil
14, rue Douer
F64100 Bayonne
I coded soft apps formerly, was not with PB, using such features for user requirements as they needed to enter arithmetical expressions to put it to a math solver.
The way to do this is to create a arithmetical expression interpretor able to parse expressions and to calculate results.
It should not take more than some 100s of code lines in PB having all parenthezised and functions notations.
It is somewhere a bit hard to do, but if it is more than two interested people I agree to come back on that subject giving a procedure able to return the result value of any normalized expression.
The only money I would like back is to have double floats in PB and any volunteer contribution in USD or EUR.
Frankly it is a job but I have all my sources just to try to adapt. So it should not take more than 2 or 3 days to make a first level parser.
Tell me about ...
Francois Weil
14, rue Douer
F64100 Bayonne
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by gnozal.
Here is the source code of an arithmetic expression evaluator found on a Rapid-Q forum. It should be easy to translate it to Purebasic.
--------------------------------------------------------------------
'Arithmetic-Expression Evaluator (includes integer divide)
'Jan. 23, 2001
'This parser is one of the modules of Calculait 3.0, a full-featured
'scientific calculator with a world clock. It is available
'for downloading from ZDNet, Nonags and other freeware sites.
'This updated version includes a routine to handle results in
'scientific format.
DECLARE SUB EvaluateArith
DECLARE SUB CheckE(newstring$ AS STRING)
DIM formula$ AS STRING
DO
CLS
PRINT "This is the engine for an arithmetic-expression evaluator."
PRINT "It doesn't use the stack as the sample evaluate.bas does"
PRINT "and, hence, is easier to read. It can correctly evaluate"
PRINT " an expression such as -(-5)+(-4)-(-3)+(2)-(1), which"
PRINT " evaluate.bas can't do."
PRINT
PRINT " You can do as you please with this code."
PRINT " By Achilles Mina"
PRINT
INPUT formula$
'formula$ = "2^(1 - (2^2)/2 * (1 + ((2 ^ 2) + 1)) + 1)"
'formula$ = "(1 + 2+3+4+5+6+7+8+9+10-55)"
'formula$ = "3+(4*(-3+2*34)+2)^2"
'formula$ = "-5-4-3-2-1"
'formula$ = "(-2)^2 +1"
'formula$ = "-(-5)+(-4)-(-3)+(2)-(1) + (.5 + 1)^2"
EvaluateArith
LOOP UNTIL a = 30
SUB EvaluateArith
DEFDBL answer
formula$ = LCASE$(REPLACESUBSTR$(formula$," ",""))
FOR i = 1 TO 1000
DO
reformatted = 0
formulaL = LEN(formula$)
par1 = RINSTR(formulaL,formula$,"(")
par2 = INSTR(par1,formula$,")")
IF par1 = 0 OR par2 = 0 THEN
betweenpars$ = formula$
ELSE
betweenpars$ = MID$(formula$,par1+1,(par2-1)-par1)
END IF
IF INSTR(betweenpars$,"^") 0 THEN
pivotoperator$ = "^"
operator = INSTR(betweenpars$,"^")
ELSEIF INSTR(betweenpars$,"*") 0 THEN
pivotoperator$ = "*"
operator = INSTR(betweenpars$,"*")
ELSEIF INSTR(betweenpars$,"/") 0 THEN
pivotoperator$ = "/"
operator = INSTR(betweenpars$,"/")
ELSEIF INSTR(betweenpars$,"\") 0 THEN
pivotoperator$ = "\"
operator = INSTR(betweenpars$,"\")
ELSEIF INSTR(betweenpars$,"+") 0 OR INSTR(betweenpars$,"-") 0 THEN
IF MID$(betweenpars$,1,1) = "-" THEN
start = 2
ELSE
start = 1
END IF
IF (TALLY(betweenpars$,"-") = 1 AND TALLY(betweenpars$,"+") = 0) AND MID$(betweenpars$,1,1) = "-" THEN
formula$ = DELETE$(formula$,par1,1)
formula$ = DELETE$(formula$,par2-1,1)
IF formula$ = betweenpars$ THEN
print
answer = VAL(formula$)
IF answer = LEN(betweenpars$) THEN
postoperator = x
EXIT DO
END IF
operator$ = MID$(betweenpars$,x,1)
SELECT CASE operator$
CASE "+","-","*","/","\"
postoperator = x - 1
EXIT DO
END SELECT
LOOP
x = operator
DO
DEC x
IF x = LEN(newstring$) THEN
powerEnd = x
EXIT DO
END IF
powerEnd$ = MID$(newstring$,x,1)
SELECT CASE powerEnd$
CASE "+","*","/","\"
powerEnd = x - 1
EXIT DO
CASE "-"
IF x whereE + 1 THEN
powerEnd = x - 1
EXIT DO
END IF
END SELECT
LOOP
oldbase$ = LEFT$(newstring$,whereE-1)
base$ = REPLACESUBSTR$(oldbase$,".","")
power$ = MID$(newstring$,whereE+1,powerEnd-whereE)
checkneg$ = LEFT$(power$,1)
IF checkneg$ = "-" THEN
move = ABS(VAL(power$))
newbase$ = "." + STRING$(move-(whereDot-1),"0") + base$
END IF
newstring$ = newbase$
END IF
END SUB
Here is the source code of an arithmetic expression evaluator found on a Rapid-Q forum. It should be easy to translate it to Purebasic.
--------------------------------------------------------------------
'Arithmetic-Expression Evaluator (includes integer divide)
'Jan. 23, 2001
'This parser is one of the modules of Calculait 3.0, a full-featured
'scientific calculator with a world clock. It is available
'for downloading from ZDNet, Nonags and other freeware sites.
'This updated version includes a routine to handle results in
'scientific format.
DECLARE SUB EvaluateArith
DECLARE SUB CheckE(newstring$ AS STRING)
DIM formula$ AS STRING
DO
CLS
PRINT "This is the engine for an arithmetic-expression evaluator."
PRINT "It doesn't use the stack as the sample evaluate.bas does"
PRINT "and, hence, is easier to read. It can correctly evaluate"
PRINT " an expression such as -(-5)+(-4)-(-3)+(2)-(1), which"
PRINT " evaluate.bas can't do."
PRINT " You can do as you please with this code."
PRINT " By Achilles Mina"
INPUT formula$
'formula$ = "2^(1 - (2^2)/2 * (1 + ((2 ^ 2) + 1)) + 1)"
'formula$ = "(1 + 2+3+4+5+6+7+8+9+10-55)"
'formula$ = "3+(4*(-3+2*34)+2)^2"
'formula$ = "-5-4-3-2-1"
'formula$ = "(-2)^2 +1"
'formula$ = "-(-5)+(-4)-(-3)+(2)-(1) + (.5 + 1)^2"
EvaluateArith
LOOP UNTIL a = 30
SUB EvaluateArith
DEFDBL answer
formula$ = LCASE$(REPLACESUBSTR$(formula$," ",""))
FOR i = 1 TO 1000
DO
reformatted = 0
formulaL = LEN(formula$)
par1 = RINSTR(formulaL,formula$,"(")
par2 = INSTR(par1,formula$,")")
IF par1 = 0 OR par2 = 0 THEN
betweenpars$ = formula$
ELSE
betweenpars$ = MID$(formula$,par1+1,(par2-1)-par1)
END IF
IF INSTR(betweenpars$,"^") 0 THEN
pivotoperator$ = "^"
operator = INSTR(betweenpars$,"^")
ELSEIF INSTR(betweenpars$,"*") 0 THEN
pivotoperator$ = "*"
operator = INSTR(betweenpars$,"*")
ELSEIF INSTR(betweenpars$,"/") 0 THEN
pivotoperator$ = "/"
operator = INSTR(betweenpars$,"/")
ELSEIF INSTR(betweenpars$,"\") 0 THEN
pivotoperator$ = "\"
operator = INSTR(betweenpars$,"\")
ELSEIF INSTR(betweenpars$,"+") 0 OR INSTR(betweenpars$,"-") 0 THEN
IF MID$(betweenpars$,1,1) = "-" THEN
start = 2
ELSE
start = 1
END IF
IF (TALLY(betweenpars$,"-") = 1 AND TALLY(betweenpars$,"+") = 0) AND MID$(betweenpars$,1,1) = "-" THEN
formula$ = DELETE$(formula$,par1,1)
formula$ = DELETE$(formula$,par2-1,1)
IF formula$ = betweenpars$ THEN
answer = VAL(formula$)
IF answer = LEN(betweenpars$) THEN
postoperator = x
EXIT DO
END IF
operator$ = MID$(betweenpars$,x,1)
SELECT CASE operator$
CASE "+","-","*","/","\"
postoperator = x - 1
EXIT DO
END SELECT
LOOP
x = operator
DO
DEC x
IF x = LEN(newstring$) THEN
powerEnd = x
EXIT DO
END IF
powerEnd$ = MID$(newstring$,x,1)
SELECT CASE powerEnd$
CASE "+","*","/","\"
powerEnd = x - 1
EXIT DO
CASE "-"
IF x whereE + 1 THEN
powerEnd = x - 1
EXIT DO
END IF
END SELECT
LOOP
oldbase$ = LEFT$(newstring$,whereE-1)
base$ = REPLACESUBSTR$(oldbase$,".","")
power$ = MID$(newstring$,whereE+1,powerEnd-whereE)
checkneg$ = LEFT$(power$,1)
IF checkneg$ = "-" THEN
move = ABS(VAL(power$))
newbase$ = "." + STRING$(move-(whereDot-1),"0") + base$
END IF
newstring$ = newbase$
END IF
END SUB
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by PB.
> It should be easy to translate it to Purebasic.
Unfortunately not; it contains commands that PureBasic doesn't support:
RInstr (PureBasic only has FindString, and not RFindString)
Exit Do
Exit Sub
Format$
And can PureBasic's float support handle this line:
IF answer
PB - Registered PureBasic Coder
> It should be easy to translate it to Purebasic.
Unfortunately not; it contains commands that PureBasic doesn't support:
RInstr (PureBasic only has FindString, and not RFindString)
Exit Do
Exit Sub
Format$
And can PureBasic's float support handle this line:
IF answer
PB - Registered PureBasic Coder
-
- PureBasic Guru
- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by gnozal.
> Unfortunately not; it contains commands that PureBasic doesn't support:
> RInstr
> Exit Do
> Exit Sub
> Format$
I think Purebasic should support those commands ; every Basic dialect knows Exit Do, Exit Sub, etc... (even QuickBasic !) And Rinstr is very usefull too ! I'm waiting for those commands at each Purebasic release... ;>
> Unfortunately not; it contains commands that PureBasic doesn't support:
> RInstr
> Exit Do
> Exit Sub
> Format$
I think Purebasic should support those commands ; every Basic dialect knows Exit Do, Exit Sub, etc... (even QuickBasic !) And Rinstr is very usefull too ! I'm waiting for those commands at each Purebasic release... ;>