Added two base functions
This commit is contained in:
parent
e458cdd910
commit
fcbdaa1f9d
4 changed files with 82 additions and 35 deletions
|
@ -12,8 +12,11 @@ namespace csmic
|
|||
public ValueType Type { get; set; }
|
||||
public object? Value { get; set; }
|
||||
|
||||
public static readonly FunctionValue TRUE = new FunctionValue(ValueType.Numeric, 1);
|
||||
public static readonly FunctionValue FALSE = new FunctionValue(ValueType.Numeric, 0);
|
||||
public static readonly FunctionValue TRUE = new FunctionValue(ValueType.Numeric, 1m);
|
||||
public static readonly FunctionValue FALSE = new FunctionValue(ValueType.Numeric, 0m);
|
||||
public static readonly FunctionValue NONE = new FunctionValue(ValueType.None, null);
|
||||
public static readonly FunctionValue NUMBER = new FunctionValue(ValueType.Numeric, 0m);
|
||||
public static readonly FunctionValue STRING = new FunctionValue(ValueType.String, string.Empty);
|
||||
|
||||
public FunctionValue()
|
||||
{
|
||||
|
|
|
@ -5,48 +5,23 @@ namespace stdlib.functions
|
|||
{
|
||||
public class AbsoluteValue : FunctionBase, ICodedFunction
|
||||
{
|
||||
public IEnumerable<FunctionArgument> ExpectedArguments
|
||||
public override IEnumerable<FunctionArgument> ExpectedArguments
|
||||
{
|
||||
get
|
||||
{
|
||||
yield return new FunctionArgument("value", new FunctionValue(ValueType.Numeric, 0m));
|
||||
yield return new FunctionArgument("value", FunctionValue.NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
public FunctionValue ReturnValue
|
||||
{
|
||||
new FunctionValue(ValueType.Numeric, 0m);
|
||||
}
|
||||
=>
|
||||
|
||||
public FunctionValue Execute(params FunctionArgument[] args)
|
||||
{
|
||||
base.ArgumentCheck(args);
|
||||
|
||||
try
|
||||
return base.Execute(args, (_args) =>
|
||||
{
|
||||
var input = args[0].Value;
|
||||
// Try to interpret both numeric and numeric-like string inputs
|
||||
decimal number;
|
||||
if (input.Type == ValueType.Numeric)
|
||||
{
|
||||
number = Convert.ToDecimal(input.Value);
|
||||
}
|
||||
else if (input.Type == ValueType.String && input.Value is string s)
|
||||
{
|
||||
number = Convert.ToDecimal(s);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new FunctionValue(ValueType.None, null);
|
||||
}
|
||||
var input = _args[0].Value;
|
||||
decimal number = Convert.ToDecimal(input.Value);
|
||||
|
||||
return new FunctionValue(ValueType.Numeric, Math.Abs(number));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new FunctionValue(ValueType.None, null);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
30
src/stdlib/functions/Sign.cs
Normal file
30
src/stdlib/functions/Sign.cs
Normal file
|
@ -0,0 +1,30 @@
|
|||
using csmic;
|
||||
using ValueType = csmic.ValueType;
|
||||
|
||||
namespace stdlib.functions
|
||||
{
|
||||
public class Sign : FunctionBase, ICodedFunction
|
||||
{
|
||||
public const decimal POSITIVE = 1;
|
||||
public const decimal NEGATIVE = -1;
|
||||
|
||||
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(ValueType.Numeric, number >= 0 ? POSITIVE : NEGATIVE);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue