Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 5 of 5 total
Thread Parsing expressions
Mon, Oct 24 2016 8:16 AMPermanent Link

Matthew Jones

Is there any facility in WebBuilder/javascript to parse an expression and give the result?
Such that I could have a string entered by the user like "3.5 * (2.1 + 5) - (100/3)"?

I could if necessary send it to the server, but would much rather do it in browser too.

--

Matthew Jones
Mon, Oct 24 2016 9:44 AMPermanent Link

Michael Dreher

"Matthew Jones" wrote:

 // Is there any facility in WebBuilder/javascript to parse an expression and give the result?
 // Such that I could have a string entered by the user like "3.5 * (2.1 + 5) - (100/3)"?
 //
 // I could if necessary send it to the server, but would much rather do it in browser too.

In JavaScript there is the eval-function. You can define

function MyEval(expr)
{
   return eval(expr);
}

for example in an external JavaScript and make an external binding in your WebBuilder unit. Calling...

var i : integer;
i := MyEval('3.5 * (2.1 + 5) - (100/3)');

should then result in -8.483 (not tested). But of couse, it's a JS expressin, not object pascal.

M. Dreher
Mon, Oct 24 2016 9:58 AMPermanent Link

Matthew Jones

Michael Dreher wrote:

> In JavaScript there is the eval-function. You can define
>
> function MyEval(expr)
> {
>    return eval(expr);
> }
>
> for example in an external JavaScript and make an external binding in your WebBuilder unit. Calling...
>
>  var i : integer;
>  i := MyEval('3.5 * (2.1 + 5) - (100/3)');
>
> should then result in -8.483 (not tested). But of couse, it's a JS expressin, not object pascal.
>
> M. Dreher

Excellent. I will give it a whirl. I'm only after a basic number parser. I have some Delphi code from the old application, but it uses vars and other things that may make it awkward to actually use.

(Funny thing about converting old code was finding some code that was like:
  stackTop : 0 .. StackRange;
I thought a moment about what this really meant, and then thought "get over yourself" when I realised it was just a fancy Integer.)

--

Matthew Jones
Mon, Oct 24 2016 10:25 AMPermanent Link

Rick

On 24/10/16 23:16, Matthew Jones wrote:
> Is there any facility in WebBuilder/javascript to parse an expression and give the result?
> Such that I could have a string entered by the user like "3.5 * (2.1 + 5) - (100/3)"?
>
> I could if necessary send it to the server, but would much rather do it in browser too.
>

I use the following in EWB code:

external function eval(str: string): variant;
..
..
..
integer ans;
ans:=eval('1+2*5-6');

The Javascript eval() function will execute any arbitrary Javascript
code so there are some security implications.

--
Rick
Mon, Oct 24 2016 10:43 AMPermanent Link

Matthew Jones

Rick wrote:

> external function eval(str: string): variant;

I sort of got stuck on this, for some minor reasons, and after a quick look in the help, realised that I hadn't really read the Engelke book chapter on this. So I did proper read of the requirements to make it work showed me the error of my ways. Now working.

The security implications are interesting, but I think I will simply limit the character set allowed. Digits, decimal point, operators and parenthesis should do most of what is required.

--

Matthew Jones
Image