Keywords.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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("INPUT")]
  40. public static void Input(EraRuntime runtime, StackFrame context, ExecutionNode node)
  41. {
  42. runtime.InputResetEvent.WaitOne();
  43. context.Variables["RESULT"][0] = runtime.LastInputValue;
  44. }
  45. [Keyword("INPUTS")]
  46. public static void InputString(EraRuntime runtime, StackFrame context, ExecutionNode node)
  47. {
  48. runtime.InputResetEvent.WaitOne();
  49. context.Variables["RESULTS"][0] = runtime.LastInputValue;
  50. }
  51. [Keyword("TIMES")]
  52. public static void Times(EraRuntime runtime, StackFrame context, ExecutionNode node)
  53. {
  54. Variable variable = runtime.ComputeVariable(context, node[0], out var index);
  55. variable[index] *= runtime.ComputeExpression(context, node[1]);
  56. }
  57. [Keyword("VARSIZE")]
  58. public static void Varsize(EraRuntime runtime, StackFrame context, ExecutionNode node)
  59. {
  60. Variable variable = runtime.ComputeVariable(context, node[0], out var index);
  61. int maxArrayDimensions = variable.Max(x => x.Key.Length);
  62. var resultVariable = runtime.GlobalVariables["RESULT"];
  63. for (int i = 0; i < maxArrayDimensions; i++)
  64. {
  65. resultVariable[i] = variable.Where(x => x.Key.Length > i).Max(x => x.Key[i]) + 1;
  66. }
  67. }
  68. [Keyword("ALIGNMENT")]
  69. public static void Alignment(EraRuntime runtime, StackFrame context, ExecutionNode node)
  70. {
  71. string alignmentType = runtime.ComputeExpression(context, node[0]).String;
  72. if (!Enum.TryParse(alignmentType, true, out DisplayLineAlignment alignmentValue))
  73. throw new EraRuntimeException($"Unable to parse alignment type: '{alignmentType}'");
  74. runtime.Console.Alignment = alignmentValue;
  75. }
  76. [Keyword("SETCOLOR")]
  77. public static void SetColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  78. {
  79. if (node.SubNodes.Length == 1)
  80. {
  81. uint argb = (uint)runtime.ComputeExpression(context, node[0]).Real;
  82. argb |= 0xFF000000U;
  83. Color c = Color.FromArgb((int)argb);
  84. runtime.Console.SetStringStyle(c);
  85. }
  86. else if (node.SubNodes.Length == 3)
  87. {
  88. int r = (int)runtime.ComputeExpression(context, node[0]).Real;
  89. int g = (int)runtime.ComputeExpression(context, node[1]).Real;
  90. int b = (int)runtime.ComputeExpression(context, node[2]).Real;
  91. runtime.Console.SetStringStyle(Color.FromArgb(r, g, b));
  92. }
  93. else
  94. throw new EraRuntimeException("Unable to parse color");
  95. }
  96. [Keyword("RESETCOLOR")]
  97. public static void ResetColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
  98. {
  99. runtime.Console.ResetStyle();
  100. }
  101. [Keyword("DRAWLINE")]
  102. public static void DrawLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  103. {
  104. runtime.Console.PrintBar();
  105. }
  106. [Keyword("DRAWLINEFORM", true, true)]
  107. public static void DrawLineForm(EraRuntime runtime, StackFrame context, ExecutionNode node)
  108. {
  109. var value = runtime.ComputeExpression(context, node.Single());
  110. runtime.Console.printCustomBar(value.ToString());
  111. }
  112. [Keyword("PRINT_IMG")]
  113. public static void PrintImg(EraRuntime runtime, StackFrame context, ExecutionNode node)
  114. {
  115. var value = runtime.ComputeExpression(context, node.Single());
  116. runtime.Console.PrintImg(value.ToString());
  117. }
  118. [Keyword("PRINTBUTTON")]
  119. public static void PrintButton(EraRuntime runtime, StackFrame context, ExecutionNode node)
  120. {
  121. var textValue = runtime.ComputeExpression(context, node[0]);
  122. var intValue = runtime.ComputeExpression(context, node[1]);
  123. runtime.Console.PrintButton(textValue.String, (long)intValue.Real);
  124. }
  125. [Keyword("HTML_PRINT")]
  126. public static void PrintHtml(EraRuntime runtime, StackFrame context, ExecutionNode node)
  127. {
  128. var htmlValue = runtime.ComputeExpression(context, node.Single());
  129. runtime.Console.PrintHtml(htmlValue.String, false);
  130. }
  131. [Keyword("PRINT", true, false)]
  132. [Keyword("PRINTFORM", true, true)]
  133. public static void Print(EraRuntime runtime, StackFrame context, ExecutionNode node)
  134. {
  135. var value = runtime.ComputeExpression(context, node.Single());
  136. runtime.Console.Write(value.ToString());
  137. }
  138. [Keyword("PRINTFORML", true, true)]
  139. [Keyword("PRINTL", true, false)]
  140. public static void PrintLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
  141. {
  142. var value = runtime.ComputeExpression(context, node.Single());
  143. runtime.Console.PrintSingleLine(value.ToString());
  144. }
  145. }
  146. }