/** * * @author seagle * @date 2024-7-22 * @Description 脚本解释器入口 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace MECF.Framework.UI.Core.DxfScript { public enum ScriptResult { SUCCESS, ERROR, EMPTY }; public class Script { private BlockSentence blockSentence = null; private StringReader reader = null; public ScriptResult Result { get; set; } = ScriptResult.EMPTY; public string Error { get; set; } = null; public Script(string scriptContent) { reader = new StringReader(scriptContent); try { blockSentence = new BlockSentence(); blockSentence.Create(reader); } catch (Exception ex) { Error = ex.Message; Result = ScriptResult.ERROR; //TODO:记录日志 return; } if (blockSentence.SentencesCount <= 0) { Result = ScriptResult.EMPTY; } else { Result = ScriptResult.SUCCESS; } } public void Execute() { if (blockSentence != null) { try { blockSentence.Execute(); } catch (Exception e) { Error = e.Message; Result = ScriptResult.ERROR; //TODO:记录日志 return; } Result = ScriptResult.SUCCESS; } } #region 采用返回值 Variable的形式读取每一个Sentence对应的执行结果 public Tuple ReadBoolValue() { Variable var = blockSentence.ReturnVar; if (var == null) { Result = ScriptResult.ERROR; Error = "Not found return sentence or compute error"; return new Tuple("", false); } if (var.DataType != VariableDataType.BOOL) { Result = ScriptResult.ERROR; Error = "Return value is not bool"; return new Tuple("", false); } Result = ScriptResult.SUCCESS; return new Tuple((string)var.Name, (bool)var.BoolValue); } public Tuple ReadStringValue() { Variable var = blockSentence.ReturnVar; if (var == null) { Result = ScriptResult.ERROR; Error = "Not found return sentence or compute error"; return new Tuple("", Error); } if (var.DataType != VariableDataType.STRING) { Result = ScriptResult.ERROR; Error = "Return value is not string"; return new Tuple("", Error); } Result = ScriptResult.SUCCESS; return new Tuple(var.Name, (string)var.StringValue); } public Tuple ReadDoubleValue() { Variable var = blockSentence.ReturnVar; if (var == null) { Result = ScriptResult.ERROR; Error = "Not found return sentence or compute error"; return new Tuple("", -1); } if (var.DataType != VariableDataType.DOUBLE) { Result = ScriptResult.ERROR; Error = "Return value is not double"; return new Tuple("", -1); } Result = ScriptResult.SUCCESS; return new Tuple(var.Name, (double)var.DoubleValue); } #endregion #region 采用DataKey的形式直接读取执行变量池中的结果 /// /// 根据Key 获取返回值 /// /// 指定类型 /// 指定key /// public T ReadValue(string dataKey) { Variable var = ScriptVariables.GetVariableByName(dataKey); if (var == null) { SetError("Not found return sentence or compute error"); return default(T); } Result = ScriptResult.SUCCESS; return (T)GetVariableValue(var); } private void SetError(string errorMessage) { Result = ScriptResult.ERROR; Error = errorMessage; } private object GetVariableValue(Variable var) { if (typeof(T) == typeof(string)) return var.StringValue; else if (typeof(T) == typeof(double)) return var.DoubleValue; else if (typeof(T) == typeof(bool)) return var.BoolValue; else throw new ArgumentException("Unsupported variable type"); } #endregion } }