excecute code at runtime
excecute code at runtime
Is this somehow possible in Pb?
a$="5+3"
debug @a$ ---> result printed is 8
Richard
a$="5+3"
debug @a$ ---> result printed is 8
Richard
Re: excecute code at runtime
Hi
Code: Select all
Dim b$(2)
a$ = "15 + 22"
For i = 1 To 2
b$(i) = StringField(a$,i,"+")
Next
Debug ValF(b$(1)) + ValF(b$(2))
Egypt my love
- Arctic Fox
- Enthusiast
- Posts: 609
- Joined: Sun Dec 21, 2008 5:02 pm
- Location: Aarhus, Denmark
Re: excecute code at runtime
An expression evaluator might do the job.
http://www.purebasic.fr/english/viewtop ... 12&t=18359
http://www.purebasic.fr/english/viewtop ... 12&t=18359
Re: excecute code at runtime
Code: Select all
UseSQLiteDatabase()
OpenDatabase(0, ":memory:", "", "", #PB_Database_SQLite)
DatabaseQuery(0, "Select 5+3")
NextDatabaseRow(0)
Debug GetDatabaseString(0, 0) ; ---> result printed is 8
CloseDatabase(0)
Hygge
-
- Addict
- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: excecute code at runtime
Nice, Kiffi... but kinda useless without float results ("22/7").
Also, very bloated... the 6 lines of code create a 403 KB executable.
But I guess if your app has database code anyway, then it's a good hack for non-float calculations.
Also, very bloated... the 6 lines of code create a 403 KB executable.

But I guess if your app has database code anyway, then it's a good hack for non-float calculations.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
PureBasic: Born in 1998 and still going strong to this very day!
Re: excecute code at runtime
Why does the sqlite way not work with floats?
Re: excecute code at runtime
Very nice Kiffi
@MachineCode -

@MachineCode -
Code: Select all
UseSQLiteDatabase()
OpenDatabase(0, ":memory:", "", "", #PB_Database_SQLite)
DatabaseQuery(0, "Select round(22.0/7.0,2)")
NextDatabaseRow(0)
Debug GetDatabaseString(0, 0) ;--> 3.14
CloseDatabase(0)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
-
- Addict
- Posts: 1482
- Joined: Tue Feb 22, 2011 1:16 pm
Re: excecute code at runtime
Thanks, SkyWalk!
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
PureBasic: Born in 1998 and still going strong to this very day!
Re: excecute code at runtime
Another include for this: Evaluate() - Expression Parser
Supports the following operators:
+, -, *, /, % (modulo), ^ (power of), <<, >>, =, <>, <, >, <=, =<, >=, =>, ( ) , &, |, !, ~, NOT, OR, AND, XOR
- Integer
- Floats (1E5, 123.5e-20 etc.)
- add variables, for example EvaluateVariables("pi") = StrD(#PI,1000)
- Hex-Format ($FFFF, $abc)
- Bin-Format (%01010101)
- Errorhandler: SetEvaluateErrorHandler(@myErrorHandler())
Example:
Example with variables:
EDIT:
Download include with 2 examples: EvaluateExpression.zip (8,5k)
EDIT2:
Removed the need for #GENERATE_STACKMACHINE_ASM by using "CompilerIf Defined()"
Download again, EvaluateExpression.zip (8,5k)
EDIT3:
changed *start to *evaluateStart and variables in procedures from Define to Protected
Download again, EvaluateExpression.zip (8,5k)
Supports the following operators:
+, -, *, /, % (modulo), ^ (power of), <<, >>, =, <>, <, >, <=, =<, >=, =>, ( ) , &, |, !, ~, NOT, OR, AND, XOR
- Integer
- Floats (1E5, 123.5e-20 etc.)
- add variables, for example EvaluateVariables("pi") = StrD(#PI,1000)
- Hex-Format ($FFFF, $abc)
- Bin-Format (%01010101)
- Errorhandler: SetEvaluateErrorHandler(@myErrorHandler())
Example:
Code: Select all
XIncludeFile "Evaluate.pbi"
Debug Evaluate("10 + 4 * 2")
Code: Select all
XIncludeFile "Evaluate.pbi"
EvaluateVariables("n") = "64"
EvaluateVariables("f") = "0.25"
Debug Evaluate("n * f")
EDIT:
Download include with 2 examples: EvaluateExpression.zip (8,5k)
EDIT2:
Removed the need for #GENERATE_STACKMACHINE_ASM by using "CompilerIf Defined()"
Download again, EvaluateExpression.zip (8,5k)
EDIT3:
changed *start to *evaluateStart and variables in procedures from Define to Protected
Download again, EvaluateExpression.zip (8,5k)