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
|
|
|
|
|
|
|
|
|
protected Function(string symbolName,string functionName, Type returnType, Type[] parameterTypes, Modification[] modifiers) : base(symbolName)
|
2025-06-30 20:20:26 +08:00
|
|
|
|
{
|
2025-07-02 13:11:26 +08:00
|
|
|
|
FunctionInfo = new(functionName, returnType, parameterTypes, modifiers);
|
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)
|
|
|
|
|
: base(symbolName, methodInfo.Name, methodInfo.ReturnType, methodInfo.GetParameters().ToList().ConvertAll(x => x.ParameterType).ToArray(), Array.Empty<Modification>())
|
|
|
|
|
{
|
|
|
|
|
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-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 Modification[] Modifiers;
|
|
|
|
|
public readonly string FullName;
|
|
|
|
|
|
|
|
|
|
public FunctionSymbol(string symbolName, Type returnType, Type[] parameterTypes, Modification[] modifiers)
|
|
|
|
|
{
|
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;
|
|
|
|
|
this.Modifiers = modifiers;
|
|
|
|
|
this.FullName = $"{returnType.FullName} {symbolName}({string.Join(',', parameterTypes.ToList().ConvertAll(x => x.FullName))}) {string.Join(' ', modifiers.ToList().ConvertAll(x => x.ToString()))}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Equals(FunctionSymbol other)
|
|
|
|
|
{
|
|
|
|
|
return other.FullName.Equals(FullName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
return FullName.GetHashCode();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|