Keywords.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Reflection;
  6. using NTERA.Core.Interop;
  7. using NTERA.Engine.Compiler;
  8. namespace NTERA.Engine.Runtime.Base
  9. {
  10. public static class Keywords
  11. {
  12. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
  13. private class KeywordAttribute : Attribute
  14. {
  15. public string Name { get; }
  16. public bool ImplicitString { get; }
  17. public bool ImplicitFormatted { get; }
  18. public KeywordAttribute(string name, bool implicitString = false, bool implicitFormatted = false)
  19. {
  20. if (implicitFormatted && !implicitString)
  21. throw new ArgumentException("Keyword cannot support formatting if it does not use implicit strings");
  22. Name = name;
  23. ImplicitString = implicitString;
  24. ImplicitFormatted = implicitFormatted;
  25. }
  26. }
  27. public static Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>> StaticKeywords { get; } = _getKeywords();
  28. private static Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>> _getKeywords()
  29. {
  30. var output = new Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>>();
  31. foreach (MethodInfo method in typeof(Keywords).GetMethods(BindingFlags.Public | BindingFlags.Static))
  32. {
  33. var keywords = method.GetCustomAttributes<KeywordAttribute>();
  34. foreach (KeywordAttribute keyword in keywords)
  35. output[keyword.Name] = (Action<EraRuntime, StackFrame, ExecutionNode>)Delegate.CreateDelegate(typeof(Action<EraRuntime, StackFrame, ExecutionNode>), method);
  36. }
  37. return output;
  38. }
  39. [Keyword("RESTART")]
  40. public static void Restart(EraRuntime runtime, StackFrame context, ExecutionNode node)
  41. {
  42. runtime.Initialize(runtime.Console);
  43. runtime.Call(runtime.ExecutionProvider.DefinedProcedures.First(x => x.Name == "SYSTEM_TITLE"));
  44. }
  45. [Keyword("INPUT")]
  46. public static void Input(EraRuntime runtime, StackFrame context, ExecutionNode node)
  47. {
  48. runtime.InputResetEvent.WaitOne();
  49. context.Variables["RESULT"][0] = runtime.LastInputValue;
  50. }
  51. [Keyword("INPUTS")]
  52. public static void InputString(EraRuntime runtime, StackFrame context, ExecutionNode node)
  53. {
  54. runtime.InputResetEvent.WaitOne();
  55. context.Variables["RESULTS"][0] = runtime.LastInputValue;
  56. }
  57. [Keyword("LOADGLOBAL")]
  58. public static void LoadGlobal(EraRuntime runtime, StackFrame context, ExecutionNode node)
  59. {
  60. }
  61. [Keyword("SAVEGLOBAL")]
  62. public static void SaveGlobal(EraRuntime runtime, StackFrame context, ExecutionNode node)
  63. {
  64. }
  65. [Keyword("TIMES")]
  66. public static void Times(EraRuntime runtime, StackFrame context, ExecutionNode node)
  67. {
  68. Variable variable = runtime.ComputeVariable(context, node[0], out var index);
  69. variable[index] *= runtime.ComputeExpression(context, node[1]);
  70. }
  71. [Keyword("VARSIZE")]
  72. public static void Varsize(EraRuntime runtime, StackFrame context, ExecutionNode node)
  73. {
  74. Variable variable = runtime.ComputeVariable(context, node[0], out var index);
  75. int maxArrayDimensions = variable.Max(x => x.Key.Length);
  76. var resultVariable = runtime.GlobalVariables["RESULT"];
  77. for (int i = 0; i < maxArrayDimensions; i++)
  78. {
  79. resultVariable[i] = variable.Where(x => x.Key.Length > i).Max(x => x.Key[i]) + 1;
  80. }
  81. }
  82. [Keyword("ALIGNMENT")]
  83. public static void Alignment(EraRuntime runtime, StackFrame context, ExecutionNode node)
  84. {
  85. string alignmentType = runtime.ComputeExpression(context, node[0]).String;
  86. if (!Enum.TryParse(alignmentType, true, out DisplayLineAlignment alignmentValue))
  87. throw new EraRuntimeException($"Unable to parse alignment type: '{alignmentType}'");
  88. runtime.Console.Alignment = alignmentValue;
  89. }
  90. private static Color ParseColorArguments(EraRuntime runtime, StackFrame context, ExecutionNode node)
  91. {
  92. if (node.SubNodes.Length == 1)
  93. {
  94. uint argb = (uint)runtime.ComputeExpression(context, node[0]).Real;
  95. argb |= 0xFF000000U;
  96. return Color.FromArgb((int)argb);
  97. }
  98. else if (node.SubNodes.Length == 3)
  99. {
  100. int r = (int)runtime.ComputeExpression(context, node[0]).Real;
  101. int g = (int)runtime.ComputeExpression(context, node[1]).Real;
  102. int b = (int)runtime.ComputeExpression(context, node[2]).Real;
  103. return Color.FromArgb(r, g, b);
  104. }
  105. else
  106. throw new EraRuntimeException("Unable to parse color");
  107. }
  108. [Keyword("SETCOLOR")]
  109. public static void SetColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  110. {
  111. runtime.Console.SetStringStyle(ParseColorArguments(runtime, context, node));
  112. }
  113. [Keyword("RESETCOLOR")]
  114. public static void ResetColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  115. {
  116. runtime.Console.ResetStyle();
  117. }
  118. [Keyword("SETBGCOLOR")]
  119. public static void SetBgColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  120. {
  121. runtime.Console.SetBgColor(ParseColorArguments(runtime, context, node));
  122. }
  123. [Keyword("RESETBGCOLOR")]
  124. public static void ResetBgColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  125. {
  126. runtime.Console.SetBgColor(Color.Black);
  127. }
  128. [Keyword("CLEARLINE")]
  129. public static void ClearLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  130. {
  131. runtime.Console.ClearLine((int)runtime.ComputeExpression(context, node.Single()).Real);
  132. }
  133. [Keyword("REDRAW")]
  134. public static void Redraw(EraRuntime runtime, StackFrame context, ExecutionNode node)
  135. {
  136. runtime.Console.SetRedraw((long)runtime.ComputeExpression(context, node.Single()).Real);
  137. }
  138. [Keyword("DRAWLINE")]
  139. public static void DrawLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  140. {
  141. runtime.Console.PrintBar();
  142. }
  143. [Keyword("DRAWLINEFORM", true, true)]
  144. public static void DrawLineForm(EraRuntime runtime, StackFrame context, ExecutionNode node)
  145. {
  146. var value = runtime.ComputeExpression(context, node.Single());
  147. runtime.Console.printCustomBar(value.ToString());
  148. }
  149. [Keyword("PRINT_IMG")]
  150. public static void PrintImg(EraRuntime runtime, StackFrame context, ExecutionNode node)
  151. {
  152. var value = runtime.ComputeExpression(context, node.Single());
  153. runtime.Console.PrintImg(value.ToString());
  154. }
  155. [Keyword("PRINTBUTTON")]
  156. public static void PrintButton(EraRuntime runtime, StackFrame context, ExecutionNode node)
  157. {
  158. var textValue = runtime.ComputeExpression(context, node[0]);
  159. var intValue = runtime.ComputeExpression(context, node[1]);
  160. runtime.Console.PrintButton(textValue.String, (long)intValue.Real);
  161. }
  162. [Keyword("HTML_PRINT")]
  163. public static void PrintHtml(EraRuntime runtime, StackFrame context, ExecutionNode node)
  164. {
  165. var htmlValue = runtime.ComputeExpression(context, node.Single());
  166. runtime.Console.PrintHtml(htmlValue.String, false);
  167. }
  168. [Keyword("PRINT", true, false)]
  169. [Keyword("PRINTFORM", true, true)]
  170. public static void Print(EraRuntime runtime, StackFrame context, ExecutionNode node)
  171. {
  172. var value = runtime.ComputeExpression(context, node.Single());
  173. runtime.Console.Write(value.ToString());
  174. }
  175. [Keyword("PRINTFORML", true, true)]
  176. [Keyword("PRINTL", true, false)]
  177. public static void PrintLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  178. {
  179. var value = runtime.ComputeExpression(context, node.Single());
  180. runtime.Console.PrintSingleLine(value.ToString());
  181. }
  182. }
  183. }