Keywords.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using NTERA.Engine.Compiler;
  8. namespace NTERA.Engine.Runtime.Base
  9. {
  10. public 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 class Keywords
  25. {
  26. public static Dictionary<string, Action<EraRuntime, ExecutionSet, ExecutionNode>> StaticKeywords { get; } = _getKeywords();
  27. private static Dictionary<string, Action<EraRuntime, ExecutionSet, ExecutionNode>> _getKeywords()
  28. {
  29. var output = new Dictionary<string, Action<EraRuntime, ExecutionSet, ExecutionNode>>();
  30. foreach (MethodInfo method in typeof(Keywords).GetMethods(BindingFlags.Public | BindingFlags.Static))
  31. {
  32. var keyword = method.GetCustomAttribute<KeywordAttribute>();
  33. if (keyword != null)
  34. output[keyword.Name] = (runtime, set, node) => { method.Invoke(null, new object[] { runtime, set, node }); };
  35. }
  36. return output;
  37. }
  38. [Keyword("DRAWLINE")]
  39. public static void DrawLine(EraRuntime runtime, ExecutionSet set, ExecutionNode node)
  40. {
  41. runtime.Console.PrintBar();
  42. }
  43. [Keyword("DRAWLINEFORM", true, true)]
  44. public static void DrawLineForm(EraRuntime runtime, ExecutionSet set, ExecutionNode node)
  45. {
  46. var value = runtime.ComputeExpression(set, node.SubNodes.Single());
  47. runtime.Console.printCustomBar(value.ToString().Trim());
  48. }
  49. }
  50. }