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 IfThen(cond: Boolean; const TrueBranch, FalseBranch: IAstNode): IAstNode;
|
||||
begin
|
||||
if cond then
|
||||
exit(TrueBranch)
|
||||
else
|
||||
exit(FalseBranch);
|
||||
end;
|
||||
|
||||
var
|
||||
elements: TList<TExpr>;
|
||||
head: TExpr;
|
||||
tailTokens: TArray<TToken>;
|
||||
tailNodes: TArray<IAstNode>;
|
||||
initializer: IAstNode;
|
||||
begin
|
||||
Consume(tkLeftParen);
|
||||
if FCurrentToken.Kind = tkRightParen then
|
||||
@@ -373,13 +366,11 @@ begin
|
||||
if not (Length(tailNodes) in [2, 3]) then
|
||||
raise Exception.Create('Syntax Error in if statement.');
|
||||
|
||||
Result :=
|
||||
TAst.IfExpr(
|
||||
tailNodes[0],
|
||||
tailNodes[1],
|
||||
if Length(tailNodes) > 2 then tailNodes[2]
|
||||
else nil
|
||||
)
|
||||
var elseBranch: IAstNode := nil;
|
||||
if Length(tailNodes) = 3 then
|
||||
elseBranch := tailNodes[2];
|
||||
|
||||
Result := TAst.IfExpr(tailNodes[0], tailNodes[1], elseBranch);
|
||||
end
|
||||
else if SameText(head.Token.Text, '?') then
|
||||
begin
|
||||
@@ -390,9 +381,20 @@ begin
|
||||
end
|
||||
else if SameText(head.Token.Text, 'def') then
|
||||
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
|
||||
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
|
||||
else if SameText(head.Token.Text, 'defmacro') then
|
||||
begin
|
||||
@@ -488,7 +490,9 @@ begin
|
||||
tkNumber:
|
||||
begin
|
||||
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
|
||||
Result.Node := TAst.Constant(dbl)
|
||||
else
|
||||
@@ -904,4 +908,7 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
initialization
|
||||
FormatSettings.DecimalSeparator := '.';
|
||||
|
||||
end.
|
||||
|
||||
Reference in New Issue
Block a user