Skip to content

Commit

Permalink
Add parseFloat, parseInt, and parseHex methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohansi committed Jun 7, 2024
1 parent f9fec06 commit 560cd85
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
77 changes: 77 additions & 0 deletions Mond.Tests/Libraries/ParseModuleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using NUnit.Framework;

namespace Mond.Tests.Libraries;

[TestFixture]
internal class ParseModuleTests
{
[Test]
public void ParseFloat_ValidNumber()
{
var result = Script.Run(@"
return parseFloat('123.456');
");

Assert.AreEqual((MondValue)123.456, result);
}

[Test]
public void ParseFloat_InvalidNumber()
{
var result = Script.Run(@"
return parseFloat('hello');
");

Assert.AreEqual(MondValue.Undefined, result);
}

[Test]
public void ParseInt_ValidNumber()
{
var result = Script.Run(@"
return parseInt('123');
");

Assert.AreEqual((MondValue)123, result);
}

[Test]
public void ParseInt_InvalidNumber()
{
var result = Script.Run(@"
return parseInt('hello');
");

Assert.AreEqual(MondValue.Undefined, result);
}

[Test]
public void ParseHex_ValidNumber()
{
var result = Script.Run(@"
return parseHex('DEaDb33F');
");

Assert.AreEqual((MondValue)0xDEaDb33F, result);
}

[Test]
public void ParseHex_DigitsOnly()
{
var result = Script.Run(@"
return parseHex('1000');
");

Assert.AreEqual((MondValue)0x1000, result);
}

[Test]
public void ParseHex_InvalidNumber()
{
var result = Script.Run(@"
return parseFloat('hello');
");

Assert.AreEqual(MondValue.Undefined, result);
}
}
32 changes: 32 additions & 0 deletions Mond/Libraries/Core/Parse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Globalization;
using Mond.Binding;

namespace Mond.Libraries.Core;

[MondModule("Parse", bareMethods: true)]
internal static partial class ParseModule
{
[MondFunction]
public static MondValue ParseFloat(string str)
{
return double.TryParse(str, NumberStyles.Float, CultureInfo.InvariantCulture, out var result)
? result
: MondValue.Undefined;
}

[MondFunction]
public static MondValue ParseInt(string str)
{
return long.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result)
? result
: MondValue.Undefined;
}

[MondFunction]
public static MondValue ParseHex(string str)
{
return long.TryParse(str, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var result)
? result
: MondValue.Undefined;
}
}
14 changes: 13 additions & 1 deletion Mond/Libraries/CoreLibraries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Mond.Libraries
{
/// <summary>
/// Contains the basic libraries that should be supported everywhere. This
/// includes the <c>error</c>, <c>try</c> and <c>require</c> functions, the
/// includes the <c>error</c>, <c>try</c>, <c>require</c>, and parse functions, the
/// <c>Char</c> and <c>Math</c> modules, and the <c>Random</c> class.
/// </summary>
public class CoreLibraries : IMondLibraryCollection
Expand All @@ -20,6 +20,7 @@ public IEnumerable<IMondLibrary> Create(MondState state)
yield return new MathLibrary();
yield return new RandomLibrary();
yield return new OperatorLibrary();
yield return new ParseLibrary();
}
}

Expand Down Expand Up @@ -139,6 +140,17 @@ public IEnumerable<KeyValuePair<string, MondValue>> GetDefinitions(MondState sta
}
}

/// <summary>
/// Library containing the built-in parse functions.
/// </summary>
public class ParseLibrary : IMondLibrary
{
public IEnumerable<KeyValuePair<string, MondValue>> GetDefinitions(MondState state)
{
return new ParseModule.Library().GetDefinitions(state);
}
}

/// <summary>
/// Library containing the <c>Random</c> class.
/// </summary>
Expand Down

0 comments on commit 560cd85

Please sign in to comment.