123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /**
- *
- * @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<string, bool> ReadBoolValue()
- {
- Variable var = blockSentence.ReturnVar;
- if (var == null)
- {
- Result = ScriptResult.ERROR;
- Error = "Not found return sentence or compute error";
- return new Tuple<string, bool>("", false);
- }
- if (var.DataType != VariableDataType.BOOL)
- {
- Result = ScriptResult.ERROR;
- Error = "Return value is not bool";
- return new Tuple<string, bool>("", false);
- }
- Result = ScriptResult.SUCCESS;
- return new Tuple<string, bool>((string)var.Name, (bool)var.BoolValue);
- }
- public Tuple<string, string> ReadStringValue()
- {
- Variable var = blockSentence.ReturnVar;
- if (var == null)
- {
- Result = ScriptResult.ERROR;
- Error = "Not found return sentence or compute error";
- return new Tuple<string, string>("", Error);
- }
- if (var.DataType != VariableDataType.STRING)
- {
- Result = ScriptResult.ERROR;
- Error = "Return value is not string";
- return new Tuple<string, string>("", Error);
- }
- Result = ScriptResult.SUCCESS;
- return new Tuple<string, string>(var.Name, (string)var.StringValue);
- }
- public Tuple<string, double> ReadDoubleValue()
- {
- Variable var = blockSentence.ReturnVar;
- if (var == null)
- {
- Result = ScriptResult.ERROR;
- Error = "Not found return sentence or compute error";
- return new Tuple<string, double>("", -1);
- }
- if (var.DataType != VariableDataType.DOUBLE)
- {
- Result = ScriptResult.ERROR;
- Error = "Return value is not double";
- return new Tuple<string, double>("", -1);
- }
- Result = ScriptResult.SUCCESS;
- return new Tuple<string, double>(var.Name, (double)var.DoubleValue);
- }
- #endregion
- #region 采用DataKey的形式直接读取执行变量池中的结果
- /// <summary>
- /// 根据Key 获取返回值
- /// </summary>
- /// <typeparam name="T">指定类型</typeparam>
- /// <param name="dataKey">指定key</param>
- /// <returns></returns>
- public T ReadValue<T>(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<T>(var);
- }
- private void SetError(string errorMessage)
- {
- Result = ScriptResult.ERROR;
- Error = errorMessage;
- }
- private object GetVariableValue<T>(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
- }
- }
|