Page 1 of 1

Expression evaluator in just 8 lines of code

Posted: Thu Dec 07, 2006 4:23 pm
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")

Posted: Thu Dec 07, 2006 4:33 pm
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.

Posted: Thu Dec 07, 2006 4:34 pm
by Max
:idea: hard! :shock: :o :D

Posted: Thu Dec 07, 2006 9:20 pm
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.

Posted: Thu Dec 07, 2006 9:53 pm
by dracflamloc
I doubt this was really intended to be taken seriously...

Posted: Thu Dec 07, 2006 10:11 pm
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.

Posted: Thu Dec 07, 2006 11:09 pm
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.

Posted: Thu Dec 07, 2006 11:40 pm
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.

Posted: Fri Dec 08, 2006 11:05 am
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")

Posted: Fri Dec 08, 2006 11:38 am
by Konne
Doesn't work with unicode either.

Posted: Fri Dec 08, 2006 9:23 pm
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:

Posted: Fri Dec 08, 2006 11:46 pm
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. :)

Posted: Fri Dec 08, 2006 11:57 pm
by Trond
Well, you can use variables.

Posted: Sat Dec 09, 2006 12:14 am
by utopiomania
Ohh, variables, well, rocket science then!

Image

:D

Posted: Sat Dec 09, 2006 12:45 am
by PB
> Ohh, variables, well, rocket science then!

That is the funniest pic I've seen in ages! A definite keeper! :lol: