excecute code at runtime

Just starting out? Need help? Post your questions and find answers here.
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

excecute code at runtime

Post by t57042 »

Is this somehow possible in Pb?

a$="5+3"

debug @a$ ---> result printed is 8

Richard
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: excecute code at runtime

Post by RASHAD »

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
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Re: excecute code at runtime

Post by Arctic Fox »

An expression evaluator might do the job.
http://www.purebasic.fr/english/viewtop ... 12&t=18359
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: excecute code at runtime

Post by Kiffi »

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)
Greetings ... Kiffi
Hygge
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: excecute code at runtime

Post by MachineCode »

Nice, Kiffi... but kinda useless without float results ("22/7").

Also, very bloated... the 6 lines of code create a 403 KB executable. :shock:

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!
t57042
Enthusiast
Enthusiast
Posts: 203
Joined: Fri Feb 22, 2008 12:28 pm
Location: Belgium

Re: excecute code at runtime

Post by t57042 »

Why does the sqlite way not work with floats?
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: excecute code at runtime

Post by skywalk »

Very nice Kiffi :)
@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
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: excecute code at runtime

Post by MachineCode »

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!
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: excecute code at runtime

Post by Danilo »

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:

Code: Select all

XIncludeFile "Evaluate.pbi"

Debug Evaluate("10 + 4 * 2")
Example with variables:

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)
Post Reply