Hi all,
I've been building the front end of a compiler for some time using fpc and plex, and at some point I had to modify ULexLib to support long strings, as it looked like it was made for old 255 character strings. I used AnsiString type for that and now my compiler supports long strings.
Now I want my compiler to support simple math expression ( in fact I just need addition and substractions). I found fpexprpars unit so I was trying to use TFPExpressionParser, I expected it to be easy but...
Sadly, the expressions I have to evaluate are stored into AnsiStrings, and when I assign the value to Parser.Expression it accepts it, but then when the expresion is evaluated I get exceptions like this:
EExprParser: Unknown identifier: 1+1
That doesn't happen if instead of assigning the value I got from my compiler parser ,I directly assing a string between quotes like '1+1';
So this doesn't work:
Parser := TFPExpressionParser.Create(nil);
Parser.BuiltIns := [bcMath];
WriteLn(CurrentText);
Parser.Expression := CurrentText;
parserResult := Parser.Evaluate; ;
but this works:
Parser := TFPExpressionParser.Create(nil);
Parser.BuiltIns := [bcMath];
WriteLn(CurrentText);
Parser.Expression := '1+1';
parserResult := Parser.Evaluate; ;
Note: CurrentText is a global variable of AnsiString type, holding the current text my compiler got, expressions are in between bracers so I get whole expression as single string.
In both cases I get "1+1" in the screen just before the evaluation, but the first one raises an exception.
I've tried to find how to convert strings, but didn't find a way. I tried to create a local variable with ShortString type, and assign CurrentText to that variable first, then assign to the parser, also tried to assing to a ShortStringVariable, then concatenate a single space string to force conversion, etc. No way.
Is there a simple way to make fpc convert an AnsiString to ShortString?
Note: Due to other reasons, I'm using {$MODE OBJFPC}
Thanks in advance!