Keywords.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. private static Random random = new Random();
  13. [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
  14. private class KeywordAttribute : Attribute
  15. {
  16. public string Name { get; }
  17. public bool ImplicitString { get; }
  18. public bool ImplicitFormatted { get; }
  19. public KeywordAttribute(string name, bool implicitString = false, bool implicitFormatted = false)
  20. {
  21. if (implicitFormatted && !implicitString)
  22. throw new ArgumentException("Keyword cannot support formatting if it does not use implicit strings");
  23. Name = name;
  24. ImplicitString = implicitString;
  25. ImplicitFormatted = implicitFormatted;
  26. }
  27. }
  28. public static Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>> StaticKeywords { get; } = _getKeywords();
  29. private static Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>> _getKeywords()
  30. {
  31. var output = new Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>>();
  32. foreach (MethodInfo method in typeof(Keywords).GetMethods(BindingFlags.Public | BindingFlags.Static))
  33. {
  34. var keywords = method.GetCustomAttributes<KeywordAttribute>();
  35. foreach (KeywordAttribute keyword in keywords)
  36. output[keyword.Name] = (Action<EraRuntime, StackFrame, ExecutionNode>)Delegate.CreateDelegate(typeof(Action<EraRuntime, StackFrame, ExecutionNode>), method);
  37. }
  38. return output;
  39. }
  40. [Keyword("RESTART")]
  41. public static void Restart(EraRuntime runtime, StackFrame context, ExecutionNode node)
  42. {
  43. runtime.Initialize(runtime.Console);
  44. runtime.Call(runtime.ExecutionProvider.DefinedProcedures.First(x => x.Name == "SYSTEM_TITLE"));
  45. }
  46. [Keyword("INPUT")]
  47. public static void Input(EraRuntime runtime, StackFrame context, ExecutionNode node)
  48. {
  49. runtime.InputResetEvent.WaitOne();
  50. context.Variables["RESULT"][0] = runtime.LastInputValue;
  51. }
  52. [Keyword("INPUTS")]
  53. public static void InputString(EraRuntime runtime, StackFrame context, ExecutionNode node)
  54. {
  55. runtime.InputResetEvent.WaitOne();
  56. context.Variables["RESULTS"][0] = runtime.LastInputValue;
  57. }
  58. [Keyword("LOADGLOBAL")]
  59. public static void LoadGlobal(EraRuntime runtime, StackFrame context, ExecutionNode node)
  60. {
  61. }
  62. [Keyword("SAVEGLOBAL")]
  63. public static void SaveGlobal(EraRuntime runtime, StackFrame context, ExecutionNode node)
  64. {
  65. }
  66. [Keyword("TIMES")]
  67. public static void Times(EraRuntime runtime, StackFrame context, ExecutionNode node)
  68. {
  69. Variable variable = runtime.ComputeVariable(context, node[0], out var index);
  70. variable[index] *= runtime.ComputeExpression(context, node[1]);
  71. }
  72. [Keyword("VARSIZE")]
  73. public static void Varsize(EraRuntime runtime, StackFrame context, ExecutionNode node)
  74. {
  75. Variable variable = runtime.ComputeVariable(context, node[0], out var index);
  76. int maxArrayDimensions = variable.Max(x => x.Key.Length);
  77. var resultVariable = runtime.GlobalVariables["RESULT"];
  78. for (int i = 0; i < maxArrayDimensions; i++)
  79. {
  80. resultVariable[i] = variable.Where(x => x.Key.Length > i).Max(x => x.Key[i]) + 1;
  81. }
  82. }
  83. [Keyword("ALIGNMENT")]
  84. public static void Alignment(EraRuntime runtime, StackFrame context, ExecutionNode node)
  85. {
  86. string alignmentType = runtime.ComputeExpression(context, node[0]).String;
  87. if (!Enum.TryParse(alignmentType, true, out DisplayLineAlignment alignmentValue))
  88. throw new EraRuntimeException($"Unable to parse alignment type: '{alignmentType}'");
  89. runtime.Console.Alignment = alignmentValue;
  90. }
  91. private static Color ParseColorArguments(EraRuntime runtime, StackFrame context, ExecutionNode node)
  92. {
  93. if (node.SubNodes.Length == 1)
  94. {
  95. uint argb = (uint)runtime.ComputeExpression(context, node[0]).Real;
  96. argb |= 0xFF000000U;
  97. return Color.FromArgb((int)argb);
  98. }
  99. else if (node.SubNodes.Length == 3)
  100. {
  101. int r = (int)runtime.ComputeExpression(context, node[0]).Real;
  102. int g = (int)runtime.ComputeExpression(context, node[1]).Real;
  103. int b = (int)runtime.ComputeExpression(context, node[2]).Real;
  104. return Color.FromArgb(r, g, b);
  105. }
  106. else
  107. throw new EraRuntimeException("Unable to parse color");
  108. }
  109. [Keyword("SETCOLOR")]
  110. public static void SetColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  111. {
  112. runtime.Console.SetStringStyle(ParseColorArguments(runtime, context, node));
  113. }
  114. [Keyword("RESETCOLOR")]
  115. public static void ResetColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  116. {
  117. runtime.Console.ResetStyle();
  118. }
  119. [Keyword("SETBGCOLOR")]
  120. public static void SetBgColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  121. {
  122. runtime.Console.SetBgColor(ParseColorArguments(runtime, context, node));
  123. }
  124. [Keyword("RESETBGCOLOR")]
  125. public static void ResetBgColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  126. {
  127. runtime.Console.SetBgColor(Color.Black);
  128. }
  129. [Keyword("CLEARLINE")]
  130. public static void ClearLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  131. {
  132. runtime.Console.ClearLine((int)runtime.ComputeExpression(context, node.Single()).Real);
  133. }
  134. [Keyword("REDRAW")]
  135. public static void Redraw(EraRuntime runtime, StackFrame context, ExecutionNode node)
  136. {
  137. runtime.Console.SetRedraw((long)runtime.ComputeExpression(context, node.Single()).Real);
  138. }
  139. [Keyword("DRAWLINE")]
  140. public static void DrawLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  141. {
  142. runtime.Console.PrintBar();
  143. }
  144. [Keyword("CUSTOMDRAWLINE", true)]
  145. public static void CustomDrawLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  146. {
  147. if (node.SubNodes.Length == 1)
  148. {
  149. var value = runtime.ComputeExpression(context, node.Single());
  150. runtime.Console.printCustomBar(value.ToString());
  151. }
  152. else {
  153. throw new ArgumentException("Missing argument to CUSTOMDRAWLINE");
  154. }
  155. }
  156. [Keyword("DRAWLINEFORM", true, true)]
  157. public static void DrawLineForm(EraRuntime runtime, StackFrame context, ExecutionNode node)
  158. {
  159. var value = runtime.ComputeExpression(context, node.Single());
  160. runtime.Console.printCustomBar(value.ToString());
  161. }
  162. [Keyword("PRINT_IMG")]
  163. public static void PrintImg(EraRuntime runtime, StackFrame context, ExecutionNode node)
  164. {
  165. var value = runtime.ComputeExpression(context, node.Single());
  166. runtime.Console.PrintImg(value.ToString());
  167. }
  168. [Keyword("PRINTBUTTON")]
  169. public static void PrintButton(EraRuntime runtime, StackFrame context, ExecutionNode node)
  170. {
  171. var textValue = runtime.ComputeExpression(context, node[0]);
  172. var intValue = runtime.ComputeExpression(context, node[1]);
  173. runtime.Console.PrintButton(textValue.String, (long)intValue.Real);
  174. }
  175. [Keyword("HTML_PRINT")]
  176. public static void PrintHtml(EraRuntime runtime, StackFrame context, ExecutionNode node)
  177. {
  178. var htmlValue = runtime.ComputeExpression(context, node.Single());
  179. runtime.Console.PrintHtml(htmlValue.String, false);
  180. }
  181. [Keyword("PRINT", true, false)]
  182. [Keyword("PRINTFORM", true, true)]
  183. public static void Print(EraRuntime runtime, StackFrame context, ExecutionNode node)
  184. {
  185. var value = runtime.ComputeExpression(context, node.Single());
  186. runtime.Console.Write(value.ToString());
  187. }
  188. [Keyword("PRINTFORML", true, true)]
  189. [Keyword("PRINTL", true, false)]
  190. public static void PrintLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  191. {
  192. var value = runtime.ComputeExpression(context, node.Single());
  193. runtime.Console.PrintSingleLine(value.ToString());
  194. }
  195. private class PrintDataOptions
  196. {
  197. public bool PrintLine = false;
  198. public bool Wait = false;
  199. public bool ForceKana = false;
  200. public bool NoColor = false;
  201. public int Count = 0;
  202. public ExecutionNode ChosenNode;
  203. }
  204. static private PrintDataOptions CurrentPrintDataOptions;
  205. [Keyword("PRINTDATA")]
  206. [Keyword("PRINTDATAL")]
  207. [Keyword("PRINTDATAW")]
  208. [Keyword("PRINTDATAK")]
  209. [Keyword("PRINTDATAKL")]
  210. [Keyword("PRINTDATAKW")]
  211. [Keyword("PRINTDATAD")]
  212. [Keyword("PRINTDATADL")]
  213. [Keyword("PRINTDATADW")]
  214. public static void PrintData(EraRuntime runtime, StackFrame context, ExecutionNode node)
  215. {
  216. /* This function doesn't do anything itself, but lets you specify a bunch of
  217. DATAFORMs followed by a ENDDATA and it picks one at random and prints that.
  218. */
  219. CurrentPrintDataOptions = new PrintDataOptions();
  220. var opts = node["name"].Substring(9);
  221. if (opts.Contains("L"))
  222. CurrentPrintDataOptions.PrintLine = true;
  223. if (opts.Contains("W"))
  224. CurrentPrintDataOptions.Wait = true;
  225. if (opts.Contains("K"))
  226. CurrentPrintDataOptions.ForceKana = true;
  227. if (opts.Contains("D"))
  228. CurrentPrintDataOptions.NoColor = true;
  229. }
  230. [Keyword("DATA", true, true)]
  231. [Keyword("DATAFORM", true, true)]
  232. public static void DataForm(EraRuntime runtime, StackFrame context, ExecutionNode node)
  233. {
  234. CurrentPrintDataOptions.Count++;
  235. if (random.Next(0, CurrentPrintDataOptions.Count) == 0)
  236. CurrentPrintDataOptions.ChosenNode = node.Single();
  237. }
  238. [Keyword("ENDDATA")]
  239. public static void EndData(EraRuntime runtime, StackFrame context, ExecutionNode node)
  240. {
  241. if (CurrentPrintDataOptions.ChosenNode == null) {
  242. throw new ArgumentException("No DATAFORM found before ENDDATA");
  243. }
  244. var value = runtime.ComputeExpression(context, CurrentPrintDataOptions.ChosenNode);
  245. if (CurrentPrintDataOptions.PrintLine)
  246. runtime.Console.PrintSingleLine(value.ToString());
  247. else
  248. runtime.Console.Write(value.ToString());
  249. }
  250. }
  251. }