Keywords.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using NTERA.Engine.Compiler;
  6. namespace NTERA.Engine.Runtime.Base
  7. {
  8. public static class Keywords
  9. {
  10. private class KeywordAttribute : Attribute
  11. {
  12. public string Name { get; }
  13. public bool ImplicitString { get; }
  14. public bool ImplicitFormatted { get; }
  15. public KeywordAttribute(string name, bool implicitString = false, bool implicitFormatted = false)
  16. {
  17. if (implicitFormatted && !implicitString)
  18. throw new ArgumentException("Keyword cannot support formatting if it does not use implicit strings");
  19. Name = name;
  20. ImplicitString = implicitString;
  21. ImplicitFormatted = implicitFormatted;
  22. }
  23. }
  24. public static Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>> StaticKeywords { get; } = _getKeywords();
  25. private static Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>> _getKeywords()
  26. {
  27. var output = new Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>>();
  28. foreach (MethodInfo method in typeof(Keywords).GetMethods(BindingFlags.Public | BindingFlags.Static))
  29. {
  30. var keyword = method.GetCustomAttribute<KeywordAttribute>();
  31. if (keyword != null)
  32. output[keyword.Name] = (runtime, set, node) => { method.Invoke(null, new object[] { runtime, set, node }); };
  33. }
  34. return output;
  35. }
  36. [Keyword("DRAWLINE")]
  37. public static void DrawLine(EraRuntime runtime, StackFrame set, ExecutionNode node)
  38. {
  39. runtime.Console.PrintBar();
  40. }
  41. [Keyword("DRAWLINEFORM", true, true)]
  42. public static void DrawLineForm(EraRuntime runtime, StackFrame set, ExecutionNode node)
  43. {
  44. var value = runtime.ComputeExpression(set, node.SubNodes.Single());
  45. runtime.Console.printCustomBar(value.ToString().Trim());
  46. }
  47. [Keyword("PRINTFORML", true, true)]
  48. public static void PrintLine(EraRuntime runtime, StackFrame set, ExecutionNode node)
  49. {
  50. var value = runtime.ComputeExpression(set, node.SubNodes.Single());
  51. runtime.Console.PrintSingleLine(value.ToString().Trim());
  52. }
  53. }
  54. }