Compare commits
3 commits
eeb243b727
...
ba44e0ed48
| Author | SHA1 | Date | |
|---|---|---|---|
| ba44e0ed48 | |||
| 2a7077eedd | |||
| 3ec3858ff2 |
8 changed files with 120 additions and 3 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -409,3 +409,4 @@ FodyWeavers.xsd
|
||||||
/src/core/cocor/Scanner.cs.old
|
/src/core/cocor/Scanner.cs.old
|
||||||
/src/core/cocor/Parser.cs
|
/src/core/cocor/Parser.cs
|
||||||
/src/core/cocor/Scanner.cs
|
/src/core/cocor/Scanner.cs
|
||||||
|
/src/.github
|
||||||
|
|
|
||||||
|
|
@ -6,15 +6,32 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace CSMic
|
namespace CSMic
|
||||||
{
|
{
|
||||||
|
/// <summary> An encapsulated function argument. </summary>
|
||||||
public class FunctionArgument
|
public class FunctionArgument
|
||||||
{
|
{
|
||||||
|
#region Properties
|
||||||
|
|
||||||
|
/// <summary> Gets or sets the name. </summary>
|
||||||
|
/// <value> The name. </value>
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
/// <summary> Gets or sets the value. </summary>
|
||||||
|
/// <value> The value. </value>
|
||||||
public FunctionValue Value { get; set; }
|
public FunctionValue Value { get; set; }
|
||||||
|
|
||||||
public FunctionArgument(string name, FunctionValue fv)
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
|
||||||
|
/// <summary> Constructor. </summary>
|
||||||
|
/// <param name="name"> The name. </param>
|
||||||
|
/// <param name="fv"> The fv. </param>
|
||||||
|
public FunctionArgument(string name, FunctionValue fv)
|
||||||
{
|
{
|
||||||
this.Name = name;
|
this.Name = name;
|
||||||
this.Value = fv;
|
this.Value = fv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,28 +7,77 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace CSMic
|
namespace CSMic
|
||||||
{
|
{
|
||||||
|
/// <summary> An encapsulated function value. </summary>
|
||||||
public class FunctionValue
|
public class FunctionValue
|
||||||
{
|
{
|
||||||
|
#region Properties
|
||||||
|
|
||||||
|
/// <summary> Gets or sets the value type of a function. </summary>
|
||||||
|
/// <value> The type. </value>
|
||||||
public FunctionValueType Type { get; set; }
|
public FunctionValueType Type { get; set; }
|
||||||
|
|
||||||
|
/// <summary> Gets or sets the value. </summary>
|
||||||
|
/// <value> The value. </value>
|
||||||
public object? Value { get; set; }
|
public object? Value { get; set; }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constants
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// (Immutable) A defined <see cref="FunctionValue"/> that represents a <c>true</c> <see cref="bool"/>
|
||||||
|
/// whose value is <c>1</c>.
|
||||||
|
/// </summary>
|
||||||
public static readonly FunctionValue TRUE = new FunctionValue(FunctionValueType.Numeric, 1m);
|
public static readonly FunctionValue TRUE = new FunctionValue(FunctionValueType.Numeric, 1m);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// (Immutable) A defined <see cref="FunctionValue"/> that represents a <c>false</c> <see cref="bool"/>
|
||||||
|
/// whose value is <c>0</c>.
|
||||||
|
/// </summary>
|
||||||
public static readonly FunctionValue FALSE = new FunctionValue(FunctionValueType.Numeric, 0m);
|
public static readonly FunctionValue FALSE = new FunctionValue(FunctionValueType.Numeric, 0m);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// (Immutable) A defined <see cref="FunctionValue"/> that represents a undefined value.
|
||||||
|
/// </summary>
|
||||||
public static readonly FunctionValue NONE = new FunctionValue(FunctionValueType.None, null);
|
public static readonly FunctionValue NONE = new FunctionValue(FunctionValueType.None, null);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// (Immutable) A defined <see cref="FunctionValue"/> that represents a numeric value. The
|
||||||
|
/// default value is <c>0</c>.
|
||||||
|
/// </summary>
|
||||||
public static readonly FunctionValue NUMBER = new FunctionValue(FunctionValueType.Numeric, 0m);
|
public static readonly FunctionValue NUMBER = new FunctionValue(FunctionValueType.Numeric, 0m);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// (Immutable) A defined <see cref="FunctionValue"/> that represents a string value. The default
|
||||||
|
/// value is <see cref="string.Empty"/>.
|
||||||
|
/// </summary>
|
||||||
public static readonly FunctionValue STRING = new FunctionValue(FunctionValueType.String, string.Empty);
|
public static readonly FunctionValue STRING = new FunctionValue(FunctionValueType.String, string.Empty);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// (Immutable) A defined <see cref="FunctionValue"/> that represents the number zero.
|
||||||
|
/// </summary>
|
||||||
public static readonly FunctionValue ZERO = new FunctionValue(FunctionValueType.Numeric, 0m);
|
public static readonly FunctionValue ZERO = new FunctionValue(FunctionValueType.Numeric, 0m);
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Constructors
|
||||||
|
|
||||||
|
/// <summary> Default constructor. </summary>
|
||||||
public FunctionValue()
|
public FunctionValue()
|
||||||
{
|
{
|
||||||
this.Type = FunctionValueType.None;
|
this.Type = FunctionValueType.None;
|
||||||
this.Value = null;
|
this.Value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Constructor. </summary>
|
||||||
|
/// <param name="type"> The type. </param>
|
||||||
|
/// <param name="value"> The value. </param>
|
||||||
public FunctionValue(FunctionValueType type, object? value)
|
public FunctionValue(FunctionValueType type, object? value)
|
||||||
{
|
{
|
||||||
this.Type = type;
|
this.Type = type;
|
||||||
this.Value = value;
|
this.Value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,10 +6,14 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace CSMic
|
namespace CSMic
|
||||||
{
|
{
|
||||||
|
/// <summary> Values that represent function value types. </summary>
|
||||||
public enum FunctionValueType
|
public enum FunctionValueType
|
||||||
{
|
{
|
||||||
|
/// <summary> An enum constant representing an undefined or unsupported function value type. </summary>
|
||||||
None,
|
None,
|
||||||
|
/// <summary> An enum constant representing a numeric function value type. </summary>
|
||||||
Numeric,
|
Numeric,
|
||||||
|
/// <summary> An enum constant representing a string function value type. </summary>
|
||||||
String
|
String
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,18 +6,32 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace CSMic
|
namespace CSMic
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Interface for a coded function that can be created at compiletime and invoked at runtime.
|
||||||
|
/// </summary>
|
||||||
public interface ICodedFunction
|
public interface ICodedFunction
|
||||||
{
|
{
|
||||||
#region Properties
|
#region Properties
|
||||||
|
|
||||||
|
/// <summary> Gets the name of the function. </summary>
|
||||||
|
/// <value> The name. </value>
|
||||||
string Name { get; }
|
string Name { get; }
|
||||||
|
|
||||||
|
/// <summary> Gets the expected arguments of the function. </summary>
|
||||||
|
/// <value> The expected arguments. </value>
|
||||||
IEnumerable<FunctionArgument> ExpectedArguments { get; }
|
IEnumerable<FunctionArgument> ExpectedArguments { get; }
|
||||||
|
|
||||||
|
/// <summary> Gets the return value of the function. </summary>
|
||||||
|
/// <value> The return value. </value>
|
||||||
FunctionValue ReturnValue { get; }
|
FunctionValue ReturnValue { get; }
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Methods
|
#region Methods
|
||||||
|
|
||||||
|
/// <summary> Executes the function with the given arguments. </summary>
|
||||||
|
/// <param name="args"> A variable-length parameters list containing arguments. </param>
|
||||||
|
/// <returns> A FunctionValue representing the result of the function execution. </returns>
|
||||||
FunctionValue Execute(params FunctionArgument[] args);
|
FunctionValue Execute(params FunctionArgument[] args);
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
|
||||||
|
|
@ -6,20 +6,26 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace CSMic
|
namespace CSMic
|
||||||
{
|
{
|
||||||
|
/// <summary> An encapsulated variable that names a runtime value. </summary>
|
||||||
public class Variable
|
public class Variable
|
||||||
{
|
{
|
||||||
#region Members
|
#region Members
|
||||||
|
|
||||||
|
/// <summary> The type of the variable. </summary>
|
||||||
private VariableType type;
|
private VariableType type;
|
||||||
|
|
||||||
|
/// <summary> The name of the variable. </summary>
|
||||||
private string name;
|
private string name;
|
||||||
|
|
||||||
|
/// <summary> The value assigned to the variable. </summary>
|
||||||
private object? value;
|
private object? value;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Properties
|
#region Properties
|
||||||
|
|
||||||
|
/// <summary> Gets the variable type. </summary>
|
||||||
|
/// <value> The type. </value>
|
||||||
public VariableType Type
|
public VariableType Type
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
@ -28,6 +34,8 @@ namespace CSMic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Gets the variable name. </summary>
|
||||||
|
/// <value> The name. </value>
|
||||||
public string Name
|
public string Name
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
@ -36,6 +44,8 @@ namespace CSMic
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Gets the assigned value. </summary>
|
||||||
|
/// <value> The assigned value. </value>
|
||||||
public object? Value
|
public object? Value
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
@ -48,6 +58,7 @@ namespace CSMic
|
||||||
|
|
||||||
#region Constructor
|
#region Constructor
|
||||||
|
|
||||||
|
/// <summary> Default constructor. </summary>
|
||||||
public Variable()
|
public Variable()
|
||||||
{
|
{
|
||||||
this.type = VariableType.None;
|
this.type = VariableType.None;
|
||||||
|
|
@ -55,6 +66,10 @@ namespace CSMic
|
||||||
this.value = null;
|
this.value = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Constructor. </summary>
|
||||||
|
/// <param name="type"> The type of the variable. </param>
|
||||||
|
/// <param name="name"> The name of the variable. </param>
|
||||||
|
/// <param name="value"> The value assigned to the variable. </param>
|
||||||
public Variable(VariableType type, string name, object? value)
|
public Variable(VariableType type, string name, object? value)
|
||||||
{
|
{
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
|
|
||||||
|
|
@ -6,11 +6,16 @@ using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace CSMic
|
namespace CSMic
|
||||||
{
|
{
|
||||||
|
/// <summary> Values that represent variable types supported by the parser. </summary>
|
||||||
public enum VariableType
|
public enum VariableType
|
||||||
{
|
{
|
||||||
|
/// <summary> An enum constant representing an unknown or unsupported type. </summary>
|
||||||
None,
|
None,
|
||||||
|
/// <summary> An enum constant representing a numeric type backed by a <see cref="decimal"/>. </summary>
|
||||||
Numeric,
|
Numeric,
|
||||||
|
/// <summary> An enum constant representing numeric array backed by a <see cref="Array"/> of <see cref="decimal"/>. </summary>
|
||||||
NumericArray,
|
NumericArray,
|
||||||
|
/// <summary> An enum constant representing an expression type backed by a <see cref="string"/> and interpreted by the parser at runtime. </summary>
|
||||||
Expression,
|
Expression,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,15 @@
|
||||||
|
|
||||||
namespace CSMic.StandardLibrary.Functions
|
namespace CSMic.StandardLibrary.Functions
|
||||||
{
|
{
|
||||||
|
/// <summary> A base class that handles base function handling. </summary>
|
||||||
public abstract class FunctionBase
|
public abstract class FunctionBase
|
||||||
{
|
{
|
||||||
|
/// <summary> Gets the expected arguments. </summary>
|
||||||
|
/// <value> The expected arguments. </value>
|
||||||
public virtual IEnumerable<FunctionArgument> ExpectedArguments { get; }
|
public virtual IEnumerable<FunctionArgument> ExpectedArguments { get; }
|
||||||
|
|
||||||
|
/// <summary> Gets the return value. </summary>
|
||||||
|
/// <value> The return value. </value>
|
||||||
public virtual FunctionValue ReturnValue
|
public virtual FunctionValue ReturnValue
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
|
|
@ -15,6 +20,9 @@ namespace CSMic.StandardLibrary.Functions
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Checks the provided arguments to ensure the function contract is honored. </summary>
|
||||||
|
/// <param name="args"> A variable-length parameters list containing arguments. </param>
|
||||||
|
/// <returns> True if it succeeds, false if it fails. </returns>
|
||||||
public bool ArgumentCheck(params FunctionArgument[] args)
|
public bool ArgumentCheck(params FunctionArgument[] args)
|
||||||
{
|
{
|
||||||
// Top level sanity checks.
|
// Top level sanity checks.
|
||||||
|
|
@ -49,6 +57,10 @@ namespace CSMic.StandardLibrary.Functions
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary> Executes a standard library function. </summary>
|
||||||
|
/// <param name="args"> A variable-length parameters list containing arguments. </param>
|
||||||
|
/// <param name="action"> The functions action body. </param>
|
||||||
|
/// <returns> A <see cref="FunctionValue"/>. </returns>
|
||||||
public FunctionValue Execute(FunctionArgument[] args, Func<FunctionArgument[], FunctionValue> action)
|
public FunctionValue Execute(FunctionArgument[] args, Func<FunctionArgument[], FunctionValue> action)
|
||||||
{
|
{
|
||||||
if (!ArgumentCheck(args))
|
if (!ArgumentCheck(args))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue