Files
Convention-CSharp/Convention/[Symbolization]/Detail/Functional.cs

97 lines
3.2 KiB
C#
Raw Normal View History

2025-06-30 20:20:26 +08:00
using System;
using System.Linq;
2025-07-02 13:11:26 +08:00
using System.Reflection;
2025-06-30 20:20:26 +08:00
namespace Convention.Symbolization.Internal
{
2025-07-02 13:11:26 +08:00
public abstract class Function : CloneableVariable<Function>
2025-06-30 20:20:26 +08:00
{
2025-06-30 21:49:33 +08:00
public readonly FunctionSymbol FunctionInfo;
2025-07-02 13:11:26 +08:00
2025-07-04 21:39:02 +08:00
protected Function(string symbolName,string functionName, Type returnType, Type[] parameterTypes) : base(symbolName)
2025-06-30 20:20:26 +08:00
{
2025-07-04 21:39:02 +08:00
FunctionInfo = new(functionName, returnType, parameterTypes);
2025-06-30 20:20:26 +08:00
}
2025-07-02 13:11:26 +08:00
public abstract Variable Invoke(SymbolizationContext context, Variable[] parameters);
2025-06-30 20:20:26 +08:00
}
2025-07-02 13:11:26 +08:00
public sealed class DelegationalFunction : Function
{
public readonly MethodInfo methodInfo;
public DelegationalFunction(string symbolName, MethodInfo methodInfo)
2025-07-04 21:39:02 +08:00
: base(symbolName, methodInfo.Name, methodInfo.ReturnType, methodInfo.GetParameters().ToList().ConvertAll(x => x.ParameterType).ToArray())
2025-07-02 13:11:26 +08:00
{
this.methodInfo = methodInfo!;
}
public override DelegationalFunction CloneVariable(string targetSymbolName)
{
return new DelegationalFunction(targetSymbolName, methodInfo);
}
public override bool Equals(Function other)
{
return other is DelegationalFunction df && df.methodInfo == this.methodInfo;
}
public override Variable Invoke(SymbolizationContext context, Variable[] parameters)
{
Variable invoker = parameters.Length > 0 ? parameters[0] : null;
Variable[] invokerParameters = parameters.Length > 0 ? parameters.Skip(1).ToArray() : Array.Empty<Variable>();
object result = methodInfo.Invoke(invoker, invokerParameters);
return result as Variable;
}
}
2025-07-04 21:39:02 +08:00
public sealed class ScriptFunction : Function
{
public ScriptFunction(string symbolName,
string functionName, Type returnType, Type[] parameterTypes)
: base(symbolName, functionName, returnType, parameterTypes)
{
}
public override Function CloneVariable(string targetSymbolName)
{
throw new NotImplementedException();
}
public override bool Equals(Function other)
{
throw new NotImplementedException();
}
public override Variable Invoke(SymbolizationContext context, Variable[] parameters)
{
throw new NotImplementedException();
}
}
2025-07-02 13:11:26 +08:00
2025-06-30 20:20:26 +08:00
public readonly struct FunctionSymbol
{
2025-07-01 22:21:30 +08:00
public readonly string FunctionName;
2025-06-30 20:20:26 +08:00
public readonly Type ReturnType;
public readonly Type[] ParameterTypes;
public readonly string FullName;
2025-07-04 21:39:02 +08:00
public FunctionSymbol(string symbolName, Type returnType, Type[] parameterTypes)
2025-06-30 20:20:26 +08:00
{
2025-07-01 22:21:30 +08:00
this.FunctionName = symbolName;
2025-06-30 20:20:26 +08:00
this.ReturnType = returnType;
this.ParameterTypes = parameterTypes;
2025-07-04 21:39:02 +08:00
this.FullName = $"{returnType.FullName} {symbolName}({string.Join(',', parameterTypes.ToList().ConvertAll(x => x.FullName))})";
2025-06-30 20:20:26 +08:00
}
public bool Equals(FunctionSymbol other)
{
return other.FullName.Equals(FullName);
}
public override int GetHashCode()
{
return FullName.GetHashCode();
}
}
}