Function registry fix.
This commit is contained in:
parent
e51068a27e
commit
afb7a65a66
9 changed files with 74 additions and 96 deletions
|
@ -10,6 +10,7 @@ namespace csmic
|
|||
{
|
||||
#region Properties
|
||||
|
||||
string Name { get; }
|
||||
IEnumerable<FunctionArgument> ExpectedArguments { get; }
|
||||
FunctionValue ReturnValue { get; }
|
||||
|
||||
|
|
|
@ -173,9 +173,9 @@ namespace csmic
|
|||
|
||||
#region Functions
|
||||
|
||||
internal void RegisterFunction(string name, ICodedFunction function)
|
||||
public void RegisterFunction(ICodedFunction function)
|
||||
{
|
||||
functions[name] = function;
|
||||
functions[function.Name] = function;
|
||||
}
|
||||
|
||||
internal FunctionValue ExecuteFunction(string name, params FunctionArgument[] args)
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
namespace csmic.stdlib
|
||||
{
|
||||
public class Constants
|
||||
public static class Constants
|
||||
{
|
||||
public void Initialize(InputInterpreter inputInterpreter)
|
||||
public static void Initialize(InputInterpreter inputInterpreter)
|
||||
{
|
||||
if(inputInterpreter == null)
|
||||
{
|
||||
|
|
26
src/stdlib/Functions.cs
Normal file
26
src/stdlib/Functions.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
using csmic;
|
||||
using stdlib.functions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace stdlib
|
||||
{
|
||||
public static class Functions
|
||||
{
|
||||
public static void Initialize(InputInterpreter inputInterpreter)
|
||||
{
|
||||
if (inputInterpreter == null)
|
||||
{
|
||||
throw new ArgumentNullException("inputInterpreter", "Cannot initialize a null InputInterpreter.");
|
||||
}
|
||||
|
||||
inputInterpreter.RegisterFunction(new AbsoluteValue());
|
||||
inputInterpreter.RegisterFunction(new Sign());
|
||||
inputInterpreter.RegisterFunction(new Min());
|
||||
inputInterpreter.RegisterFunction(new Max());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -5,6 +5,14 @@ namespace stdlib.functions
|
|||
{
|
||||
public class AbsoluteValue : FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "abs";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
|
|
|
@ -9,6 +9,14 @@ namespace stdlib.functions
|
|||
{
|
||||
public class Max : FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "max";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
|
|
|
@ -9,6 +9,14 @@ namespace stdlib.functions
|
|||
{
|
||||
public class Min : FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "min";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
|
|
|
@ -8,6 +8,14 @@ namespace stdlib.functions
|
|||
public const decimal POSITIVE = 1;
|
||||
public const decimal NEGATIVE = -1;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "sign";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
|
|
|
@ -1,105 +1,24 @@
|
|||
using csmic;
|
||||
using csmic.stdlib;
|
||||
using NUnit.Framework;
|
||||
using stdlib;
|
||||
using stdlib.functions;
|
||||
using System.Globalization;
|
||||
using System.Reflection.Metadata;
|
||||
|
||||
namespace tests;
|
||||
|
||||
public class StdlibFunctionsTests
|
||||
{
|
||||
private static FunctionArgument NumArg(decimal d) => new FunctionArgument("value", new FunctionValue(FunctionValueType.Numeric, d));
|
||||
private InputInterpreter _interp = null!;
|
||||
|
||||
[Test]
|
||||
public void AbsoluteValue_Positive_ReturnsSame()
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
var fn = new AbsoluteValue();
|
||||
var result = fn.Execute(NumArg(5m));
|
||||
Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
|
||||
Assert.That(result.Value, Is.EqualTo(5m));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AbsoluteValue_Negative_ReturnsPositive()
|
||||
{
|
||||
var fn = new AbsoluteValue();
|
||||
var result = fn.Execute(NumArg(-12.5m));
|
||||
Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
|
||||
Assert.That(result.Value, Is.EqualTo(12.5m));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AbsoluteValue_Zero_ReturnsZero()
|
||||
{
|
||||
var fn = new AbsoluteValue();
|
||||
var result = fn.Execute(NumArg(0m));
|
||||
Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
|
||||
Assert.That(result.Value, Is.EqualTo(0m));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AbsoluteValue_InvalidArgType_ReturnsNone()
|
||||
{
|
||||
var fn = new AbsoluteValue();
|
||||
var badArg = new FunctionArgument("value", FunctionValue.STRING);
|
||||
var result = fn.Execute(badArg);
|
||||
Assert.That(result.Type, Is.EqualTo(FunctionValueType.None));
|
||||
Assert.That(result.Value, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AbsoluteValue_WrongArgCount_ReturnsNone()
|
||||
{
|
||||
var fn = new AbsoluteValue();
|
||||
var result0 = fn.Execute();
|
||||
var result2 = fn.Execute(NumArg(1m), NumArg(2m));
|
||||
Assert.That(result0.Type, Is.EqualTo(FunctionValueType.None));
|
||||
Assert.That(result2.Type, Is.EqualTo(FunctionValueType.None));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Sign_Negative_ReturnsNegativeOne()
|
||||
{
|
||||
var fn = new Sign();
|
||||
var result = fn.Execute(NumArg(-1m));
|
||||
Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
|
||||
Assert.That(result.Value, Is.EqualTo(Sign.NEGATIVE));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Sign_Zero_ReturnsPositiveOne()
|
||||
{
|
||||
var fn = new Sign();
|
||||
var result = fn.Execute(NumArg(0m));
|
||||
Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
|
||||
Assert.That(result.Value, Is.EqualTo(Sign.POSITIVE));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Sign_Positive_ReturnsPositiveOne()
|
||||
{
|
||||
var fn = new Sign();
|
||||
var result = fn.Execute(NumArg(99.99m));
|
||||
Assert.That(result.Type, Is.EqualTo(FunctionValueType.Numeric));
|
||||
Assert.That(result.Value, Is.EqualTo(Sign.POSITIVE));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Sign_InvalidArgType_ReturnsNone()
|
||||
{
|
||||
var fn = new Sign();
|
||||
var badArg = new FunctionArgument("value", FunctionValue.STRING);
|
||||
var result = fn.Execute(badArg);
|
||||
Assert.That(result.Type, Is.EqualTo(FunctionValueType.None));
|
||||
Assert.That(result.Value, Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Sign_WrongArgCount_ReturnsNone()
|
||||
{
|
||||
var fn = new Sign();
|
||||
var result0 = fn.Execute();
|
||||
var result2 = fn.Execute(NumArg(1m), NumArg(2m));
|
||||
Assert.That(result0.Type, Is.EqualTo(FunctionValueType.None));
|
||||
Assert.That(result2.Type, Is.EqualTo(FunctionValueType.None));
|
||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||
_interp = new InputInterpreter();
|
||||
Constants.Initialize(_interp);
|
||||
Functions.Initialize(_interp); ;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue