using Serein.Library; using Serein.Script.Node; using Serein.Script.Node.FlowControl; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Serein.Script { public class SereinScript { /// /// 类型分析 /// public SereinScriptTypeAnalysis TypeAnalysis { get; set; } = new SereinScriptTypeAnalysis(); private ProgramNode? programNode; public Type ParserScript(Dictionary argTypes, string script) { SereinScriptParser parser = new SereinScriptParser(); var programNode = parser.Parse(script); TypeAnalysis.NodeSymbolInfos.Clear(); // 清空符号表 TypeAnalysis.LoadSymbol(argTypes); // 提前加载脚本节点定义的符号 TypeAnalysis.Analysis(programNode); // 分析节点类型 var returnType = TypeAnalysis.NodeSymbolInfos[programNode]; // 获取返回类型 this.programNode = programNode; return returnType; // 脚本返回类型 } public async Task InterpreterAsync(IScriptInvokeContext context) { if(programNode is null) { throw new ArgumentNullException(nameof(programNode)); } Dictionary symbolInfos = TypeAnalysis.NodeSymbolInfos.ToDictionary(); SereinScriptInterpreter Interpreter = new SereinScriptInterpreter(symbolInfos); return await Interpreter.InterpretAsync(context, programNode); } /// /// 挂载的函数 /// public static Dictionary FunctionDelegates { get; private set; } = []; /// /// 挂载方法的信息 /// public static Dictionary FunctionInfos { get; private set; } = []; /// /// 挂载的类型 /// public static Dictionary MountType = new Dictionary(); /// /// 挂载的函数调用的对象(用于解决函数需要实例才能调用的场景) /// public static Dictionary> DelegateInstances = new Dictionary>(); /// /// 挂载静态函数 /// /// /// public static void AddStaticFunction(string functionName, MethodInfo methodInfo) { FunctionDelegates[functionName] = new DelegateDetails(methodInfo); FunctionInfos[functionName] = methodInfo; } /// /// 挂载函数 /// /// 函数名称 /// 方法信息 public static void AddFunction(string functionName, MethodInfo methodInfo, Func? callObj = null) { if (!methodInfo.IsStatic && callObj is null) { SereinEnv.WriteLine(InfoType.WARN, "函数挂载失败:试图挂载非静态的函数,但没有传入相应的获取实例的方法。"); return; } if (!methodInfo.IsStatic && callObj is not null && !DelegateInstances.ContainsKey(functionName)) { // 非静态函数需要给定类型 DelegateInstances.Add(functionName, callObj); } if (!FunctionDelegates.ContainsKey(functionName)) { FunctionDelegates[functionName] = new DelegateDetails(methodInfo); } } /// /// 挂载类型 /// /// 函数名称 /// 指定类型名称 public static void AddClassType(Type type, string typeName = "") { if (string.IsNullOrEmpty(typeName)) { typeName = type.Name; } if (!MountType.ContainsKey(typeName)) { MountType[typeName] = type; } } } }