Added two base functions

This commit is contained in:
Jordan Wages 2025-08-20 04:55:04 -05:00
commit fcbdaa1f9d
4 changed files with 82 additions and 35 deletions

View file

@ -7,25 +7,64 @@ namespace stdlib.functions
{
public virtual IEnumerable<FunctionArgument> ExpectedArguments { get; }
internal bool ArgumentCheck(params FunctionArgument[] args)
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.Type != expectedArgument.Value.Type)
if (argument.Value == null || argument.Value.Value == null || argument.Value.Type != expectedArgument.Value.Type)
{
return false;
}
if (argument.Value.Type == ValueType.Numeric && argument.Value.Value is not decimal)
{
return false;
}
if (argument.Value.Type == ValueType.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;
}
}
}
}