Adopt .NET naming and structure: CsMic.* namespaces, PascalCase projects and solution; update Coco namespace; update package id and NuGet publish target; adjust project refs and tests.
This commit is contained in:
parent
e84cce9fef
commit
0a67e6e2cc
24 changed files with 54 additions and 49 deletions
23
src/StandardLibrary/Constants.cs
Normal file
23
src/StandardLibrary/Constants.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using CsMic;
|
||||
|
||||
namespace CsMic.StandardLibrary
|
||||
{
|
||||
public static class Constants
|
||||
{
|
||||
public static void Initialize(InputInterpreter inputInterpreter)
|
||||
{
|
||||
if(inputInterpreter == null)
|
||||
{
|
||||
throw new ArgumentNullException("inputInterpreter", "Cannot initialize a null InputInterpreter.");
|
||||
}
|
||||
|
||||
inputInterpreter.Interpret("pi :: 3.1415926535897931");
|
||||
inputInterpreter.Interpret("e :: 2.7182818284590451");
|
||||
inputInterpreter.Interpret("tau :: 6.2831853071795862");
|
||||
inputInterpreter.Interpret("phi :: 1.6180339887498948");
|
||||
inputInterpreter.Interpret("goldenratio :: 1.6180339887498948");
|
||||
inputInterpreter.Interpret("eurler :: 0.5772156649015329");
|
||||
inputInterpreter.Interpret("omega :: 0.5671432904097839");
|
||||
}
|
||||
}
|
||||
}
|
19
src/StandardLibrary/CsMic.StandardLibrary.csproj
Normal file
19
src/StandardLibrary/CsMic.StandardLibrary.csproj
Normal file
|
@ -0,0 +1,19 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<RootNamespace>CsMic.StandardLibrary</RootNamespace>
|
||||
<AssemblyName>CsMic.StandardLibrary</AssemblyName>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Core\CsMic.Core.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="functions\hyperbolic\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
26
src/StandardLibrary/Functions.cs
Normal file
26
src/StandardLibrary/Functions.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
using CsMic;
|
||||
using CsMic.StandardLibrary.Functions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CsMic.StandardLibrary
|
||||
{
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
35
src/StandardLibrary/functions/AbsoluteValue.cs
Normal file
35
src/StandardLibrary/functions/AbsoluteValue.cs
Normal file
|
@ -0,0 +1,35 @@
|
|||
using CsMic;
|
||||
using FunctionValueType = CsMic.FunctionValueType;
|
||||
|
||||
namespace CsMic.StandardLibrary.Functions
|
||||
{
|
||||
public class AbsoluteValue : FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "abs";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
return base.Execute(args, (_args) =>
|
||||
{
|
||||
var input = _args[0].Value;
|
||||
decimal number = Convert.ToDecimal(input.Value);
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, Math.Abs(number));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
70
src/StandardLibrary/functions/FunctionBase.cs
Normal file
70
src/StandardLibrary/functions/FunctionBase.cs
Normal file
|
@ -0,0 +1,70 @@
|
|||
using CsMic;
|
||||
using FunctionValueType = CsMic.FunctionValueType;
|
||||
|
||||
namespace CsMic.StandardLibrary.Functions
|
||||
{
|
||||
public abstract class FunctionBase
|
||||
{
|
||||
public virtual IEnumerable<FunctionArgument> ExpectedArguments { get; }
|
||||
|
||||
public virtual FunctionValue ReturnValue
|
||||
{
|
||||
get
|
||||
|
||||
{
|
||||
return FunctionValue.NUMBER;
|
||||
}
|
||||
}
|
||||
|
||||
public bool ArgumentCheck(params FunctionArgument[] args)
|
||||
{
|
||||
// Top level sanity checks.
|
||||
if (args == null || args.Length != this.ExpectedArguments.Count())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check each argument against what is expected.
|
||||
var expectedArgumentsArray = this.ExpectedArguments.ToArray();
|
||||
for (int i = 0; i < args.Length; i++)
|
||||
{
|
||||
var expectedArgument = expectedArgumentsArray[i];
|
||||
var argument = args[i];
|
||||
if (argument.Value == null || argument.Value.Value == null || argument.Value.Type != expectedArgument.Value.Type)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (argument.Value.Type == FunctionValueType.Numeric && argument.Value.Value is not decimal)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (argument.Value.Type == FunctionValueType.String && argument.Value.Value is not string)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// All checks passed.
|
||||
return true;
|
||||
}
|
||||
|
||||
public FunctionValue Execute(FunctionArgument[] args, Func<FunctionArgument[], FunctionValue> action)
|
||||
{
|
||||
if (!ArgumentCheck(args))
|
||||
{
|
||||
return FunctionValue.NONE;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return action(args);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return FunctionValue.NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
43
src/StandardLibrary/functions/Max.cs
Normal file
43
src/StandardLibrary/functions/Max.cs
Normal file
|
@ -0,0 +1,43 @@
|
|||
using CsMic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CsMic.StandardLibrary.Functions
|
||||
{
|
||||
public class Max : FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "max";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new FunctionArgument("first", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("second", FunctionValue.NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
return base.Execute(args, (_args) =>
|
||||
{
|
||||
var inputFirst = _args[0].Value;
|
||||
var inputSecond = _args[1].Value;
|
||||
decimal first = Convert.ToDecimal(inputFirst.Value);
|
||||
decimal second = Convert.ToDecimal(inputSecond.Value);
|
||||
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, Math.Max(first, second));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
43
src/StandardLibrary/functions/Min.cs
Normal file
43
src/StandardLibrary/functions/Min.cs
Normal file
|
@ -0,0 +1,43 @@
|
|||
using CsMic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CsMic.StandardLibrary.Functions
|
||||
{
|
||||
public class Min : FunctionBase, ICodedFunction
|
||||
{
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "min";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new FunctionArgument("first", FunctionValue.NUMBER);
|
||||
yield return new FunctionArgument("second", FunctionValue.NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
return base.Execute(args, (_args) =>
|
||||
{
|
||||
var inputFirst = _args[0].Value;
|
||||
var inputSecond = _args[1].Value;
|
||||
decimal first = Convert.ToDecimal(inputFirst.Value);
|
||||
decimal second = Convert.ToDecimal(inputSecond.Value);
|
||||
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, Math.Min(first, second));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
38
src/StandardLibrary/functions/Sign.cs
Normal file
38
src/StandardLibrary/functions/Sign.cs
Normal file
|
@ -0,0 +1,38 @@
|
|||
using CsMic;
|
||||
using FunctionValueType = CsMic.FunctionValueType;
|
||||
|
||||
namespace CsMic.StandardLibrary.Functions
|
||||
{
|
||||
public class Sign : FunctionBase, ICodedFunction
|
||||
{
|
||||
public const decimal POSITIVE = 1;
|
||||
public const decimal NEGATIVE = -1;
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
return "sign";
|
||||
}
|
||||
}
|
||||
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
return base.Execute(args, (_args) =>
|
||||
{
|
||||
var input = _args[0].Value;
|
||||
decimal number = Convert.ToDecimal(input.Value);
|
||||
|
||||
return new FunctionValue(FunctionValueType.Numeric, number >= 0 ? POSITIVE : NEGATIVE);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue