Script.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. *
  3. * @author seagle
  4. * @date 2024-7-22
  5. * @Description 脚本解释器入口
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. namespace MECF.Framework.UI.Core.DxfScript
  14. {
  15. public enum ScriptResult { SUCCESS, ERROR, EMPTY };
  16. public class Script
  17. {
  18. private BlockSentence blockSentence = null;
  19. private StringReader reader = null;
  20. public ScriptResult Result { get; set; } = ScriptResult.EMPTY;
  21. public string Error { get; set; } = null;
  22. public Script(string scriptContent)
  23. {
  24. reader = new StringReader(scriptContent);
  25. try
  26. {
  27. blockSentence = new BlockSentence();
  28. blockSentence.Create(reader);
  29. }
  30. catch (Exception ex)
  31. {
  32. Error = ex.Message;
  33. Result = ScriptResult.ERROR;
  34. //TODO:记录日志
  35. return;
  36. }
  37. if (blockSentence.SentencesCount <= 0)
  38. {
  39. Result = ScriptResult.EMPTY;
  40. }
  41. else
  42. {
  43. Result = ScriptResult.SUCCESS;
  44. }
  45. }
  46. public void Execute()
  47. {
  48. if (blockSentence != null)
  49. {
  50. try
  51. {
  52. blockSentence.Execute();
  53. }
  54. catch (Exception e)
  55. {
  56. Error = e.Message;
  57. Result = ScriptResult.ERROR;
  58. //TODO:记录日志
  59. return;
  60. }
  61. Result = ScriptResult.SUCCESS;
  62. }
  63. }
  64. #region 采用返回值 Variable的形式读取每一个Sentence对应的执行结果
  65. public Tuple<string, bool> ReadBoolValue()
  66. {
  67. Variable var = blockSentence.ReturnVar;
  68. if (var == null)
  69. {
  70. Result = ScriptResult.ERROR;
  71. Error = "Not found return sentence or compute error";
  72. return new Tuple<string, bool>("", false);
  73. }
  74. if (var.DataType != VariableDataType.BOOL)
  75. {
  76. Result = ScriptResult.ERROR;
  77. Error = "Return value is not bool";
  78. return new Tuple<string, bool>("", false);
  79. }
  80. Result = ScriptResult.SUCCESS;
  81. return new Tuple<string, bool>((string)var.Name, (bool)var.BoolValue);
  82. }
  83. public Tuple<string, string> ReadStringValue()
  84. {
  85. Variable var = blockSentence.ReturnVar;
  86. if (var == null)
  87. {
  88. Result = ScriptResult.ERROR;
  89. Error = "Not found return sentence or compute error";
  90. return new Tuple<string, string>("", Error);
  91. }
  92. if (var.DataType != VariableDataType.STRING)
  93. {
  94. Result = ScriptResult.ERROR;
  95. Error = "Return value is not string";
  96. return new Tuple<string, string>("", Error);
  97. }
  98. Result = ScriptResult.SUCCESS;
  99. return new Tuple<string, string>(var.Name, (string)var.StringValue);
  100. }
  101. public Tuple<string, double> ReadDoubleValue()
  102. {
  103. Variable var = blockSentence.ReturnVar;
  104. if (var == null)
  105. {
  106. Result = ScriptResult.ERROR;
  107. Error = "Not found return sentence or compute error";
  108. return new Tuple<string, double>("", -1);
  109. }
  110. if (var.DataType != VariableDataType.DOUBLE)
  111. {
  112. Result = ScriptResult.ERROR;
  113. Error = "Return value is not double";
  114. return new Tuple<string, double>("", -1);
  115. }
  116. Result = ScriptResult.SUCCESS;
  117. return new Tuple<string, double>(var.Name, (double)var.DoubleValue);
  118. }
  119. #endregion
  120. #region 采用DataKey的形式直接读取执行变量池中的结果
  121. /// <summary>
  122. /// 根据Key 获取返回值
  123. /// </summary>
  124. /// <typeparam name="T">指定类型</typeparam>
  125. /// <param name="dataKey">指定key</param>
  126. /// <returns></returns>
  127. public T ReadValue<T>(string dataKey)
  128. {
  129. Variable var = ScriptVariables.GetVariableByName(dataKey);
  130. if (var == null)
  131. {
  132. SetError("Not found return sentence or compute error");
  133. return default(T);
  134. }
  135. Result = ScriptResult.SUCCESS;
  136. return (T)GetVariableValue<T>(var);
  137. }
  138. private void SetError(string errorMessage)
  139. {
  140. Result = ScriptResult.ERROR;
  141. Error = errorMessage;
  142. }
  143. private object GetVariableValue<T>(Variable var)
  144. {
  145. if (typeof(T) == typeof(string))
  146. return var.StringValue;
  147. else if (typeof(T) == typeof(double))
  148. return var.DoubleValue;
  149. else if (typeof(T) == typeof(bool))
  150. return var.BoolValue;
  151. else
  152. throw new ArgumentException("Unsupported variable type");
  153. }
  154. #endregion
  155. }
  156. }