Skip to content

Commit

Permalink
Fixed number parsing to be region agnostic (always use . to separat…
Browse files Browse the repository at this point in the history
…e decimals)
  • Loading branch information
martindevans committed Jun 13, 2021
1 parent 837322c commit 3faec2e
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
3 changes: 2 additions & 1 deletion Yolol.Cylon/Deserialisation/Versions/V_0_3_0.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json.Linq;
using Semver;
Expand Down Expand Up @@ -132,7 +133,7 @@ private BaseExpression ParseExpression(JToken jtok)
return ParseUnaryExpression(jtok);

case "expression::number":
return new ConstantNumber((Number)decimal.Parse(jtok.Value<string>("num")));
return new ConstantNumber((Number)decimal.Parse(jtok.Value<string>("num"), CultureInfo.InvariantCulture));

case "expression::string":
return new ConstantString(jtok.Value<string>("str"));
Expand Down
3 changes: 2 additions & 1 deletion Yolol.Cylon/Deserialisation/Versions/V_1_X_X.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json.Linq;
using Yolol.Execution;
Expand Down Expand Up @@ -192,7 +193,7 @@ BaseExpression ParseModify(string op)
return ParseModify(type[2]);

case "number":
return new ConstantNumber((Number)decimal.Parse(jtok.Value<string>("num")));
return new ConstantNumber((Number)decimal.Parse(jtok.Value<string>("num"), CultureInfo.InvariantCulture));

case "string":
return new ConstantString(jtok.Value<string>("str"));
Expand Down
5 changes: 3 additions & 2 deletions Yolol/Execution/Number.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using Yolol.Execution.Attributes;

Expand Down Expand Up @@ -124,13 +125,13 @@ public override int GetHashCode()
public static Number Parse(string s)
{
// First check if the number is out of the valid range
var d = double.Parse(s);
var d = double.Parse(s, CultureInfo.InvariantCulture);
if (d >= MaxValue._value)
return MaxValue;
else if (d <= MinValue._value)
return MinValue;

return (Number)decimal.Parse(s);
return (Number)decimal.Parse(s, CultureInfo.InvariantCulture);
}

public static explicit operator Number(bool b)
Expand Down
9 changes: 6 additions & 3 deletions Yolol/Grammar/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static Result<Program, ParseError> ParseProgram(string program)
catch (FormatException e)
{
var c = e.Data["cursor"] as Cursor;
return new Result<Program, ParseError>(new ParseError(c!, e.Message));
return new Result<Program, ParseError>(new ParseError(c, e.Message));
}
}

Expand Down Expand Up @@ -65,10 +65,10 @@ public Result(TErr err)

public class ParseError
{
public Cursor Cursor { get; }
public Cursor? Cursor { get; }
public string Message { get; }

public ParseError(Cursor cursor, string message)
public ParseError(Cursor? cursor, string message)
{
message = message
.Replace("\r", "\\r")
Expand All @@ -80,6 +80,9 @@ public ParseError(Cursor cursor, string message)

public override string ToString()
{
if (Cursor == null)
return Message;

var spaces = new string(' ', Math.Max(0, Cursor.Column - 2));

return $"{Cursor.Subject}\n"
Expand Down
2 changes: 1 addition & 1 deletion Yolol/Yolol.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageProjectUrl>https://github.com/martindevans/Yolol</PackageProjectUrl>
<RepositoryUrl>https://github.com/martindevans/Yolol</RepositoryUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>13.0.0</Version>
<Version>13.0.1</Version>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
Expand Down
59 changes: 59 additions & 0 deletions YololEmulator.Tests/Ladder/PrimeCountingFunction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Yolol.Execution;

namespace YololEmulator.Tests.Ladder
{
[TestClass]
public class PrimeCountingFunction
: BaseGenerator
{
private readonly Dictionary<int, bool> _cache = new();

[TestMethod]
public void Generate()
{
Run(34923, 10000, false, Generator.ScoreMode.BasicScoring);
}

protected override bool GenerateCase(Random random, int index, Dictionary<string, Value> inputs, Dictionary<string, Value> outputs)
{
var i = index + 1;
inputs.Add("i", i);
outputs.Add("o", PrimeCount(i));
return true;
}

private int PrimeCount(int n)
{
var count = n >= 2 ? 1 : 0;
for (var i = 3; i < n; i += 2)
if (IsOddIntPrime(i))
count++;
return count;
}

private bool IsOddIntPrime(int number)
{
if (number <= 1)
return false;
if (_cache.TryGetValue(number, out var cached))
return cached;

var boundary = (int)Math.Floor(Math.Sqrt(number));

for (var i = 3; i <= boundary; i += 2)
{
if (number % i == 0)
{
_cache[number] = false;
return false;
}
}

_cache[number] = true;
return true;
}
}
}
2 changes: 1 addition & 1 deletion YololEmulator/YololEmulator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<Platforms>AnyCPU;x64</Platforms>
<Version>10.0.0</Version>
<Version>10.0.1</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down

0 comments on commit 3faec2e

Please sign in to comment.