Fix float parsing issue
This commit is contained in:
File diff suppressed because one or more lines are too long
+24
-17
@@ -328,19 +328,12 @@ end;
|
|||||||
|
|
||||||
function TParser.ParseList: IAstNode;
|
function TParser.ParseList: IAstNode;
|
||||||
|
|
||||||
function IfThen(cond: Boolean; const TrueBranch, FalseBranch: IAstNode): IAstNode;
|
|
||||||
begin
|
|
||||||
if cond then
|
|
||||||
exit(TrueBranch)
|
|
||||||
else
|
|
||||||
exit(FalseBranch);
|
|
||||||
end;
|
|
||||||
|
|
||||||
var
|
var
|
||||||
elements: TList<TExpr>;
|
elements: TList<TExpr>;
|
||||||
head: TExpr;
|
head: TExpr;
|
||||||
tailTokens: TArray<TToken>;
|
tailTokens: TArray<TToken>;
|
||||||
tailNodes: TArray<IAstNode>;
|
tailNodes: TArray<IAstNode>;
|
||||||
|
initializer: IAstNode;
|
||||||
begin
|
begin
|
||||||
Consume(tkLeftParen);
|
Consume(tkLeftParen);
|
||||||
if FCurrentToken.Kind = tkRightParen then
|
if FCurrentToken.Kind = tkRightParen then
|
||||||
@@ -373,13 +366,11 @@ begin
|
|||||||
if not (Length(tailNodes) in [2, 3]) then
|
if not (Length(tailNodes) in [2, 3]) then
|
||||||
raise Exception.Create('Syntax Error in if statement.');
|
raise Exception.Create('Syntax Error in if statement.');
|
||||||
|
|
||||||
Result :=
|
var elseBranch: IAstNode := nil;
|
||||||
TAst.IfExpr(
|
if Length(tailNodes) = 3 then
|
||||||
tailNodes[0],
|
elseBranch := tailNodes[2];
|
||||||
tailNodes[1],
|
|
||||||
if Length(tailNodes) > 2 then tailNodes[2]
|
Result := TAst.IfExpr(tailNodes[0], tailNodes[1], elseBranch);
|
||||||
else nil
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
else if SameText(head.Token.Text, '?') then
|
else if SameText(head.Token.Text, '?') then
|
||||||
begin
|
begin
|
||||||
@@ -390,9 +381,20 @@ begin
|
|||||||
end
|
end
|
||||||
else if SameText(head.Token.Text, 'def') then
|
else if SameText(head.Token.Text, 'def') then
|
||||||
begin
|
begin
|
||||||
|
// Validate argument count for 'def' special form.
|
||||||
|
if not (Length(tailNodes) in [1, 2]) then
|
||||||
|
raise Exception.CreateFmt(
|
||||||
|
'Syntax Error: ''def'' requires an identifier and an optional initializer (1 or 2 arguments), but got %d.',
|
||||||
|
[Length(tailNodes)]);
|
||||||
|
|
||||||
if tailTokens[0].Kind <> tkIdentifier then
|
if tailTokens[0].Kind <> tkIdentifier then
|
||||||
raise Exception.Create('Syntax Error: Expected an identifier for def statement.');
|
raise Exception.Create('Syntax Error: Expected an identifier for def statement.');
|
||||||
Result := TAst.VarDecl(IIdentifierNode(tailNodes[0]), IfThen(Length(tailNodes) > 1, tailNodes[1], nil));
|
|
||||||
|
initializer := nil;
|
||||||
|
if Length(tailNodes) = 2 then
|
||||||
|
initializer := tailNodes[1];
|
||||||
|
|
||||||
|
Result := TAst.VarDecl(IIdentifierNode(tailNodes[0]), initializer);
|
||||||
end
|
end
|
||||||
else if SameText(head.Token.Text, 'defmacro') then
|
else if SameText(head.Token.Text, 'defmacro') then
|
||||||
begin
|
begin
|
||||||
@@ -488,7 +490,9 @@ begin
|
|||||||
tkNumber:
|
tkNumber:
|
||||||
begin
|
begin
|
||||||
if TryStrToInt64(FCurrentToken.Text, i64) then
|
if TryStrToInt64(FCurrentToken.Text, i64) then
|
||||||
Result.Node := TAst.Constant(i64)
|
Result.Node :=
|
||||||
|
TAst.Constant(i64)
|
||||||
|
// Use invariant format settings for float parsing.
|
||||||
else if TryStrToFloat(FCurrentToken.Text, dbl) then
|
else if TryStrToFloat(FCurrentToken.Text, dbl) then
|
||||||
Result.Node := TAst.Constant(dbl)
|
Result.Node := TAst.Constant(dbl)
|
||||||
else
|
else
|
||||||
@@ -904,4 +908,7 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
initialization
|
||||||
|
FormatSettings.DecimalSeparator := '.';
|
||||||
|
|
||||||
end.
|
end.
|
||||||
|
|||||||
Reference in New Issue
Block a user