Keywords.cs 5.1 KB

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