Keywords.cs 10 KB

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