Expression evaluator in just 8 lines of code

Share your advanced PureBasic knowledge/code with the community.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Expression evaluator in just 8 lines of code

Post by Trond »

Code: Select all

Procedure.l Eval(String.s)
  Protected Program.l, Result.l
  Program = RunProgram("cmd", "/c set /a " + String, "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide)
  WaitProgram(Program)
  Result = Val(ReadProgramString(Program))
  CloseProgram(Program)
  ProcedureReturn Result
EndProcedure

Debug Eval("5+2*10")
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

Just 7 lines :P

Code: Select all

Procedure.l Eval(String.s) 
  Protected Program = RunProgram("cmd", "/c set /a " + String, "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide) 
  WaitProgram(Program) 
  Protected Result = Val(ReadProgramString(Program)) 
  CloseProgram(Program) 
  ProcedureReturn Result 
EndProcedure 

Debug Eval("5+2*10")
Nice idea, the example works.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Max
User
User
Posts: 67
Joined: Thu Nov 30, 2006 4:57 pm
Location: I long for the absolute loneliness of the death

Post by Max »

:idea: hard! :shock: :o :D
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Both versions don't work with floats (try 22/7) making them a bit useless. :)
Oh, and they don't work with Windows 9x/Me either, which may be an issue.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

I doubt this was really intended to be taken seriously...
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post by AND51 »

I just copied the original code and I removed the first Protected-line.

Tested under XP, works. Don't forget to try command.com instead of cmd.exe if you do not have XP... :wink:

22/7 ? Not tested yet. I assume, that command/cmd cannot handle floats, just integers.
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Max wrote::idea: hard! :shock: :o :D
... to think of?
PB wrote:Both versions don't work with floats (try 22/7) making them a bit useless. :)
Oh, and they don't work with Windows 9x/Me either, which may be an issue.
Not useless for integer expressions. :wink: On the other hand, it supports hexadecimal and octal numbers.
But yes, I didn't think about the 9x problem. That's a real problem.
dracflamloc wrote:I doubt this was really intended to be taken seriously...
Yes, it's not exactly serious, but it actually can be used.
dracflamloc
Addict
Addict
Posts: 1648
Joined: Mon Sep 20, 2004 3:52 pm
Contact:

Post by dracflamloc »

Oh yes I know. Its kind of a neat hack. I didn't realize that you could do that with the set command.
va!n
Addict
Addict
Posts: 1104
Joined: Wed Apr 20, 2005 12:48 pm

Post by va!n »

codelines size compo? :lol:
just 4 lines... ok, it could fit in one line :D

Code: Select all

Procedure.l Eval(String.s)
  Protected Program = RunProgram("cmd", "/c set /a " + String, "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide), Result = Val(ReadProgramString(Program))
  WaitProgram(Program) And CloseProgram(Program) : ProcedureReturn Result
EndProcedure

Debug Eval("5+2*10")
va!n aka Thorsten

Intel i7-980X Extreme Edition, 12 GB DDR3, Radeon 5870 2GB, Windows7 x64,
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Post by Konne »

Doesn't work with unicode either.
Apart from that Mrs Lincoln, how was the show?
Heathen
Enthusiast
Enthusiast
Posts: 498
Joined: Tue Sep 27, 2005 6:54 pm
Location: At my pc coding..

Post by Heathen »

va!n wrote:codelines size compo? :lol:
just 4 lines... ok, it could fit in one line :D

Code: Select all

Procedure.l Eval(String.s)
  Protected Program = RunProgram("cmd", "/c set /a " + String, "", #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide), Result = Val(ReadProgramString(Program))
  WaitProgram(Program) And CloseProgram(Program) : ProcedureReturn Result
EndProcedure

Debug Eval("5+2*10")
:lol:
I love Purebasic.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Trond, I posted:
Expression Evaluator in just 45 lines of code
Then you had to post:
Expression Evaluator in just 8 lines of code
Only mine is as useful to yours as 45 is to .00008!

What a sorry hack, I mean, Integer math only? No functions or variables? I doubt you can be a Norwegian after all. :)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Well, you can use variables.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Ohh, variables, well, rocket science then!

Image

:D
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> Ohh, variables, well, rocket science then!

That is the funniest pic I've seen in ages! A definite keeper! :lol:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply