Keywords.cs 5.3 KB

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