Keywords.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.Initialize(runtime.Console);
  45. runtime.Call(runtime.ExecutionProvider.DefinedProcedures.First(x => x.Name == "SYSTEM_TITLE"));
  46. }
  47. [Keyword("GOTO", true)]
  48. public static void Goto(EraRuntime runtime, StackFrame context, ExecutionNode node)
  49. {
  50. string target = runtime.ComputeExpression(context, node[0]).String;
  51. var nodeTarget = context.ExecutionNodes.FirstOrDefault(x => x.Anchor == target);
  52. if (nodeTarget == null)
  53. throw new EraRuntimeException($"Could not find GOTO anchor '{target}'");
  54. context.ExecutionIndex = context.ExecutionNodes.IndexOf(nodeTarget);
  55. }
  56. [Keyword("INPUT")]
  57. public static void Input(EraRuntime runtime, StackFrame context, ExecutionNode node)
  58. {
  59. runtime.InputResetEvent.WaitOne();
  60. context.Variables["RESULT"][0] = runtime.LastInputValue;
  61. }
  62. [Keyword("INPUTS")]
  63. public static void InputString(EraRuntime runtime, StackFrame context, ExecutionNode node)
  64. {
  65. runtime.InputResetEvent.WaitOne();
  66. context.Variables["RESULTS"][0] = runtime.LastInputValue;
  67. }
  68. [Keyword("LOADGLOBAL")]
  69. public static void LoadGlobal(EraRuntime runtime, StackFrame context, ExecutionNode node)
  70. {
  71. }
  72. [Keyword("SAVEGLOBAL")]
  73. public static void SaveGlobal(EraRuntime runtime, StackFrame context, ExecutionNode node)
  74. {
  75. }
  76. [Keyword("TIMES")]
  77. public static void Times(EraRuntime runtime, StackFrame context, ExecutionNode node)
  78. {
  79. Variable variable = runtime.ComputeVariable(context, node[0], out var index);
  80. variable[index] *= runtime.ComputeExpression(context, node[1]);
  81. }
  82. [Keyword("VARSIZE")]
  83. public static void Varsize(EraRuntime runtime, StackFrame context, ExecutionNode node)
  84. {
  85. Variable variable = runtime.ComputeVariable(context, node[0], out var index);
  86. int maxArrayDimensions = variable.Max(x => x.Key.Length);
  87. var resultVariable = runtime.GlobalVariables["RESULT"];
  88. for (int i = 0; i < maxArrayDimensions; i++)
  89. {
  90. resultVariable[i] = variable.Where(x => x.Key.Length > i).Max(x => x.Key[i]) + 1;
  91. }
  92. }
  93. [Keyword("ALIGNMENT")]
  94. public static void Alignment(EraRuntime runtime, StackFrame context, ExecutionNode node)
  95. {
  96. string alignmentType = runtime.ComputeExpression(context, node[0]).String;
  97. if (!Enum.TryParse(alignmentType, true, out DisplayLineAlignment alignmentValue))
  98. throw new EraRuntimeException($"Unable to parse alignment type: '{alignmentType}'");
  99. runtime.Console.Alignment = alignmentValue;
  100. }
  101. private static Color ParseColorArguments(EraRuntime runtime, StackFrame context, ExecutionNode node)
  102. {
  103. if (node.SubNodes.Length == 1)
  104. {
  105. uint argb = (uint)runtime.ComputeExpression(context, node[0]).Real;
  106. argb |= 0xFF000000U;
  107. return Color.FromArgb((int)argb);
  108. }
  109. else if (node.SubNodes.Length == 3)
  110. {
  111. int r = (int)runtime.ComputeExpression(context, node[0]).Real;
  112. int g = (int)runtime.ComputeExpression(context, node[1]).Real;
  113. int b = (int)runtime.ComputeExpression(context, node[2]).Real;
  114. return Color.FromArgb(r, g, b);
  115. }
  116. else
  117. throw new EraRuntimeException("Unable to parse color");
  118. }
  119. [Keyword("SETCOLOR")]
  120. public static void SetColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  121. {
  122. runtime.Console.SetStringStyle(ParseColorArguments(runtime, context, node));
  123. }
  124. [Keyword("RESETCOLOR")]
  125. public static void ResetColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  126. {
  127. runtime.Console.ResetStyle();
  128. }
  129. [Keyword("SETBGCOLOR")]
  130. public static void SetBgColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  131. {
  132. runtime.Console.SetBgColor(ParseColorArguments(runtime, context, node));
  133. }
  134. [Keyword("RESETBGCOLOR")]
  135. public static void ResetBgColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  136. {
  137. runtime.Console.SetBgColor(Color.Black);
  138. }
  139. [Keyword("CLEARLINE")]
  140. public static void ClearLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  141. {
  142. runtime.Console.ClearLine((int)runtime.ComputeExpression(context, node.Single()).Real);
  143. }
  144. [Keyword("REDRAW")]
  145. public static void Redraw(EraRuntime runtime, StackFrame context, ExecutionNode node)
  146. {
  147. runtime.Console.SetRedraw((long)runtime.ComputeExpression(context, node.Single()).Real);
  148. }
  149. [Keyword("DRAWLINE")]
  150. public static void DrawLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  151. {
  152. runtime.Console.PrintBar();
  153. }
  154. [Keyword("CUSTOMDRAWLINE", true)]
  155. public static void CustomDrawLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  156. {
  157. if (node.SubNodes.Length == 1)
  158. {
  159. var value = runtime.ComputeExpression(context, node.Single());
  160. runtime.Console.printCustomBar(value.ToString());
  161. }
  162. else {
  163. throw new ArgumentException("Missing argument to CUSTOMDRAWLINE");
  164. }
  165. }
  166. [Keyword("DRAWLINEFORM", true, true)]
  167. public static void DrawLineForm(EraRuntime runtime, StackFrame context, ExecutionNode node)
  168. {
  169. var value = runtime.ComputeExpression(context, node.Single());
  170. runtime.Console.printCustomBar(value.ToString());
  171. }
  172. [Keyword("PRINT_IMG")]
  173. public static void PrintImg(EraRuntime runtime, StackFrame context, ExecutionNode node)
  174. {
  175. var value = runtime.ComputeExpression(context, node.Single());
  176. runtime.Console.PrintImg(value.ToString());
  177. }
  178. [Keyword("PRINTBUTTON")]
  179. public static void PrintButton(EraRuntime runtime, StackFrame context, ExecutionNode node)
  180. {
  181. var textValue = runtime.ComputeExpression(context, node[0]);
  182. var intValue = runtime.ComputeExpression(context, node[1]);
  183. runtime.Console.PrintButton(textValue.String, (long)intValue.Real);
  184. }
  185. [Keyword("HTML_PRINT")]
  186. public static void PrintHtml(EraRuntime runtime, StackFrame context, ExecutionNode node)
  187. {
  188. var htmlValue = runtime.ComputeExpression(context, node.Single());
  189. runtime.Console.PrintHtml(htmlValue.String, false);
  190. }
  191. private static PrintFlags ParsePrintFlags(string opts)
  192. {
  193. return new PrintFlags {
  194. NewLine = opts.Contains("L") || opts.Contains("W"),
  195. Wait = opts.Contains("W"),
  196. ForceKana = opts.Contains("K"),
  197. NoColor = opts.Contains("D")
  198. };
  199. }
  200. [Keyword("PRINTFORM", true, true)]
  201. [Keyword("PRINTFORMK", true, true)]
  202. [Keyword("PRINTFORMD", true, true)]
  203. [Keyword("PRINTFORML", true, true)]
  204. [Keyword("PRINTFORMW", true, true)]
  205. [Keyword("PRINTFORMK", true, true)]
  206. [Keyword("PRINTFORMKL", true, true)]
  207. [Keyword("PRINTFORMKW", true, true)]
  208. [Keyword("PRINTFORMD", true, true)]
  209. [Keyword("PRINTFORMDL", true, true)]
  210. [Keyword("PRINTFORMDW", true, true)]
  211. public static void PrintForm(EraRuntime runtime, StackFrame context, ExecutionNode node)
  212. {
  213. var flags = ParsePrintFlags(node["name"].Substring(9));
  214. var value = runtime.ComputeExpression(context, node.Single());
  215. runtime.Console.Write(value.ToString(), flags);
  216. if (flags.Wait) {
  217. runtime.InputResetEvent.WaitOne();
  218. }
  219. }
  220. [Keyword("PRINT", true, false)]
  221. [Keyword("PRINTL", true, false)]
  222. [Keyword("PRINTW", true, false)]
  223. [Keyword("PRINTK", true, false)]
  224. [Keyword("PRINTKL", true, false)]
  225. [Keyword("PRINTKW", true, false)]
  226. [Keyword("PRINTD", true, false)]
  227. [Keyword("PRINTDL", true, false)]
  228. [Keyword("PRINTDW", true, false)]
  229. public static void Print(EraRuntime runtime, StackFrame context, ExecutionNode node)
  230. {
  231. var flags = ParsePrintFlags(node["name"].Substring(5));
  232. var value = runtime.ComputeExpression(context, node.Single());
  233. runtime.Console.Write(value.ToString(), flags);
  234. if (flags.Wait) {
  235. runtime.InputResetEvent.WaitOne();
  236. }
  237. }
  238. private class PrintDataOptions
  239. {
  240. public PrintFlags Flags = new PrintFlags();
  241. public int Count = 0;
  242. public ExecutionNode ChosenNode;
  243. }
  244. static private PrintDataOptions CurrentPrintDataOptions;
  245. [Keyword("PRINTDATA")]
  246. [Keyword("PRINTDATAL")]
  247. [Keyword("PRINTDATAW")]
  248. [Keyword("PRINTDATAK")]
  249. [Keyword("PRINTDATAKL")]
  250. [Keyword("PRINTDATAKW")]
  251. [Keyword("PRINTDATAD")]
  252. [Keyword("PRINTDATADL")]
  253. [Keyword("PRINTDATADW")]
  254. public static void PrintData(EraRuntime runtime, StackFrame context, ExecutionNode node)
  255. {
  256. /* This function doesn't do anything itself, but lets you specify a bunch of
  257. DATAFORMs followed by a ENDDATA and it picks one at random and prints that.
  258. */
  259. CurrentPrintDataOptions = new PrintDataOptions();
  260. CurrentPrintDataOptions.Flags = ParsePrintFlags(node["name"].Substring(9));
  261. }
  262. [Keyword("DATA", true, true)]
  263. [Keyword("DATAFORM", true, true)]
  264. public static void DataForm(EraRuntime runtime, StackFrame context, ExecutionNode node)
  265. {
  266. CurrentPrintDataOptions.Count++;
  267. if (random.Next(0, CurrentPrintDataOptions.Count) == 0)
  268. CurrentPrintDataOptions.ChosenNode = node.Single();
  269. }
  270. [Keyword("ENDDATA")]
  271. public static void EndData(EraRuntime runtime, StackFrame context, ExecutionNode node)
  272. {
  273. if (CurrentPrintDataOptions.ChosenNode == null) {
  274. throw new ArgumentException("No DATAFORM found before ENDDATA");
  275. }
  276. var value = runtime.ComputeExpression(context, CurrentPrintDataOptions.ChosenNode);
  277. var flags = CurrentPrintDataOptions.Flags;
  278. runtime.Console.Write(value.ToString(), flags);
  279. if (flags.Wait) {
  280. runtime.InputResetEvent.WaitOne();
  281. }
  282. }
  283. }
  284. }