Keywords.cs 11 KB

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