123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Reflection;
- using NTERA.Core.Interop;
- using NTERA.Engine.Compiler;
- namespace NTERA.Engine.Runtime.Base
- {
- public static class Keywords
- {
- [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
- 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 keywords = method.GetCustomAttributes<KeywordAttribute>();
- foreach (KeywordAttribute keyword in keywords)
- output[keyword.Name] = (Action<EraRuntime, StackFrame, ExecutionNode>)Delegate.CreateDelegate(typeof(Action<EraRuntime, StackFrame, ExecutionNode>), method);
- }
- return output;
- }
- [Keyword("RESTART")]
- public static void Restart(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- runtime.Initialize(runtime.Console);
- runtime.Call(runtime.ExecutionProvider.DefinedProcedures.First(x => x.Name == "SYSTEM_TITLE"));
- }
- [Keyword("INPUT")]
- public static void Input(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- runtime.InputResetEvent.WaitOne();
- context.Variables["RESULT"][0] = runtime.LastInputValue;
- }
- [Keyword("INPUTS")]
- public static void InputString(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- runtime.InputResetEvent.WaitOne();
- context.Variables["RESULTS"][0] = runtime.LastInputValue;
- }
- [Keyword("LOADGLOBAL")]
- public static void LoadGlobal(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- }
- [Keyword("SAVEGLOBAL")]
- public static void SaveGlobal(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- }
- [Keyword("TIMES")]
- public static void Times(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- Variable variable = runtime.ComputeVariable(context, node[0], out var index);
- variable[index] *= runtime.ComputeExpression(context, node[1]);
- }
- [Keyword("VARSIZE")]
- public static void Varsize(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- Variable variable = runtime.ComputeVariable(context, node[0], out var index);
- int maxArrayDimensions = variable.Max(x => x.Key.Length);
- var resultVariable = runtime.GlobalVariables["RESULT"];
- for (int i = 0; i < maxArrayDimensions; i++)
- {
- resultVariable[i] = variable.Where(x => x.Key.Length > i).Max(x => x.Key[i]) + 1;
- }
- }
- [Keyword("ALIGNMENT")]
- public static void Alignment(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- string alignmentType = runtime.ComputeExpression(context, node[0]).String;
- if (!Enum.TryParse(alignmentType, true, out DisplayLineAlignment alignmentValue))
- throw new EraRuntimeException($"Unable to parse alignment type: '{alignmentType}'");
- runtime.Console.Alignment = alignmentValue;
- }
- private static Color ParseColorArguments(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- if (node.SubNodes.Length == 1)
- {
- uint argb = (uint)runtime.ComputeExpression(context, node[0]).Real;
- argb |= 0xFF000000U;
- return Color.FromArgb((int)argb);
- }
- else if (node.SubNodes.Length == 3)
- {
- int r = (int)runtime.ComputeExpression(context, node[0]).Real;
- int g = (int)runtime.ComputeExpression(context, node[1]).Real;
- int b = (int)runtime.ComputeExpression(context, node[2]).Real;
- return Color.FromArgb(r, g, b);
- }
- else
- throw new EraRuntimeException("Unable to parse color");
- }
- [Keyword("SETCOLOR")]
- public static void SetColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- runtime.Console.SetStringStyle(ParseColorArguments(runtime, context, node));
- }
- [Keyword("RESETCOLOR")]
- public static void ResetColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- runtime.Console.ResetStyle();
- }
- [Keyword("SETBGCOLOR")]
- public static void SetBgColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- runtime.Console.SetBgColor(ParseColorArguments(runtime, context, node));
- }
- [Keyword("RESETBGCOLOR")]
- public static void ResetBgColor(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- runtime.Console.SetBgColor(Color.Black);
- }
- [Keyword("CLEARLINE")]
- public static void ClearLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- runtime.Console.ClearLine((int)runtime.ComputeExpression(context, node.Single()).Real);
- }
- [Keyword("REDRAW")]
- public static void Redraw(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- runtime.Console.SetRedraw((long)runtime.ComputeExpression(context, node.Single()).Real);
- }
- [Keyword("DRAWLINE")]
- public static void DrawLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- runtime.Console.PrintBar();
- }
- [Keyword("DRAWLINEFORM", true, true)]
- public static void DrawLineForm(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- var value = runtime.ComputeExpression(context, node.Single());
- runtime.Console.printCustomBar(value.ToString());
- }
- [Keyword("PRINT_IMG")]
- public static void PrintImg(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- var value = runtime.ComputeExpression(context, node.Single());
- runtime.Console.PrintImg(value.ToString());
- }
- [Keyword("PRINTBUTTON")]
- public static void PrintButton(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- var textValue = runtime.ComputeExpression(context, node[0]);
- var intValue = runtime.ComputeExpression(context, node[1]);
- runtime.Console.PrintButton(textValue.String, (long)intValue.Real);
- }
- [Keyword("HTML_PRINT")]
- public static void PrintHtml(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- var htmlValue = runtime.ComputeExpression(context, node.Single());
- runtime.Console.PrintHtml(htmlValue.String, false);
- }
- [Keyword("PRINT", true, false)]
- [Keyword("PRINTFORM", true, true)]
- public static void Print(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- var value = runtime.ComputeExpression(context, node.Single());
- runtime.Console.Write(value.ToString());
- }
- [Keyword("PRINTFORML", true, true)]
- [Keyword("PRINTL", true, false)]
- public static void PrintLine(EraRuntime runtime, StackFrame context, ExecutionNode node)
- {
- var value = runtime.ComputeExpression(context, node.Single());
- runtime.Console.PrintSingleLine(value.ToString());
- }
- }
- }
|