Skip to content

Commit

Permalink
[float] resultDouble.TryParse(str).
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Dec 29, 2022
1 parent e530862 commit c92e469
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions AST.ci
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public enum CiId
MathNaN,
MathNegativeInfinity,
MathPositiveInfinity,
DoubleTryParse,
StringContains,
StringEndsWith,
StringIndexOf,
Expand Down Expand Up @@ -1372,6 +1373,7 @@ public class CiSystem : CiScope
Add(ushortType);
CiRangeType# minus1Type = CiRangeType.New(-1, 0x7fffffff);
Add(FloatType);
DoubleType.Add(CiMethod.NewMutator(CiVisibility.Public, BoolType, CiId.DoubleTryParse, "TryParse", CiVar.New(StringPtrType, "value")));
Add(DoubleType);
Add(BoolType);
CiClass# stringClass = CiClass.New(CiCallType.Normal, CiId.StringClass, "string");
Expand Down
7 changes: 7 additions & 0 deletions GenCs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,13 @@ public override void VisitSymbolReference(CiSymbolReference expr, CiPriority par
protected override void WriteCall(CiExpr obj, CiMethod method, List<CiExpr> args, CiPriority parent)
{
switch (method.Id) {
case CiId.DoubleTryParse:
Write("double.TryParse(");
args[0].Accept(this, CiPriority.Argument);
Write(", out ");
obj.Accept(this, CiPriority.Argument);
WriteChar(')');
break;
case CiId.StringIndexOf:
case CiId.StringLastIndexOf:
obj.Accept(this, CiPriority.Primary);
Expand Down
4 changes: 1 addition & 3 deletions Parser.ci
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ public class CiParser : CiLexer
CiLiteralDouble# ParseDouble!()
{
double d;
bool ok;
native { ok = double.TryParse(GetLexeme().Replace("_", ""), out d); }
if (!ok)
if (!d.TryParse(GetLexeme().Replace("_", "")))
ReportError("Invalid floating-point number");
CiLiteralDouble# result = new CiLiteralDouble { Line = this.Line, Type = this.Program.System.DoubleType, Value = d };
NextToken();
Expand Down
5 changes: 3 additions & 2 deletions Transpiled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,7 @@ public enum CiId
MathNaN,
MathNegativeInfinity,
MathPositiveInfinity,
DoubleTryParse,
StringContains,
StringEndsWith,
StringIndexOf,
Expand Down Expand Up @@ -2820,6 +2821,7 @@ internal CiSystem()
Add(ushortType);
CiRangeType minus1Type = CiRangeType.New(-1, 2147483647);
Add(this.FloatType);
this.DoubleType.Add(CiMethod.NewMutator(CiVisibility.Public, this.BoolType, CiId.DoubleTryParse, "TryParse", CiVar.New(this.StringPtrType, "value")));
Add(this.DoubleType);
Add(this.BoolType);
CiClass stringClass = CiClass.New(CiCallType.Normal, CiId.StringClass, "string");
Expand Down Expand Up @@ -3168,8 +3170,7 @@ void CheckXcrementParent()
CiLiteralDouble ParseDouble()
{
double d;
bool ok;
ok = double.TryParse(GetLexeme().Replace("_", ""), out d); if (!ok)
if (!double.TryParse(GetLexeme().Replace("_", ""), out d))
ReportError("Invalid floating-point number");
CiLiteralDouble result = new CiLiteralDouble { Line = this.Line, Type = this.Program.System.DoubleType, Value = d };
NextToken();
Expand Down
8 changes: 8 additions & 0 deletions test/DoubleTryParse.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public static class Test
{
public static bool Run()
{
double d;
return d.TryParse("5") && d == 5; //FAIL: c cpp java js py swift ts cl
}
}

0 comments on commit c92e469

Please sign in to comment.