123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using NTERA.Engine.Compiler;
- namespace NTERA.Engine.Runtime.Base
- {
- public static class Keywords
- {
- private class KeywordAttribute : Attribute
- {
- public string Name { get; }
- public bool ImplicitString { get; }
- public bool ImplicitFormatted { get; }
- public KeywordAttribute(string name, bool implicitString = false, bool implicitFormatted = false)
- {
- if (implicitFormatted && !implicitString)
- throw new ArgumentException("Keyword cannot support formatting if it does not use implicit strings");
- Name = name;
- ImplicitString = implicitString;
- ImplicitFormatted = implicitFormatted;
- }
- }
- public static Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>> StaticKeywords { get; } = _getKeywords();
- private static Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>> _getKeywords()
- {
- var output = new Dictionary<string, Action<EraRuntime, StackFrame, ExecutionNode>>();
- foreach (MethodInfo method in typeof(Keywords).GetMethods(BindingFlags.Public | BindingFlags.Static))
- {
- var keyword = method.GetCustomAttribute<KeywordAttribute>();
- if (keyword != null)
- output[keyword.Name] = (runtime, set, node) => { method.Invoke(null, new object[] { runtime, set, node }); };
- }
- return output;
- }
- [Keyword("DRAWLINE")]
- public static void DrawLine(EraRuntime runtime, StackFrame set, ExecutionNode node)
- {
- runtime.Console.PrintBar();
- }
- [Keyword("DRAWLINEFORM", true, true)]
- public static void DrawLineForm(EraRuntime runtime, StackFrame set, ExecutionNode node)
- {
- var value = runtime.ComputeExpression(set, node.SubNodes.Single());
- runtime.Console.printCustomBar(value.ToString().Trim());
- }
- [Keyword("PRINTFORML", true, true)]
- public static void PrintLine(EraRuntime runtime, StackFrame set, ExecutionNode node)
- {
- var value = runtime.ComputeExpression(set, node.SubNodes.Single());
- runtime.Console.PrintSingleLine(value.ToString().Trim());
- }
- }
- }
|