using System; using System.Collections.Generic; using System.Reflection; namespace NTERA.Engine.Runtime.Base { public static class Functions { private class FunctionAttribute : Attribute { public string Name { get; } public FunctionAttribute(string name) { Name = name; } } public static Dictionary, Value>> StaticFunctions { get; } = _getFunctions(); private static Dictionary, Value>> _getFunctions() { var output = new Dictionary, Value>>(); foreach (MethodInfo method in typeof(Functions).GetMethods(BindingFlags.Public | BindingFlags.Static)) { var function = method.GetCustomAttribute(); if (function != null) output[function.Name] = (runtime, set, parameters) => (Value)method.Invoke(null, new object[] { runtime, set, parameters }); } return output; } [Function("__FORMAT")] public static Value Format(EraRuntime runtime, StackFrame context, IList parameters) { if (parameters.Count == 1) return parameters[0].String; throw new NotImplementedException("Advanced formatting hasn't been implemented yet"); } [Function("TOSTR")] public static Value ToStr(EraRuntime runtime, StackFrame context, IList parameters) { if (parameters.Count == 1) return parameters[0].ToString(); if (parameters.Count == 2) return ((int)parameters[0]).ToString(parameters[1].String); throw new EraRuntimeException("Invalid amount of parameters"); } private static Random _random = new Random(); [Function("RAND")] public static Value Rand(EraRuntime runtime, StackFrame context, IList parameters) { return _random.Next((int)parameters[0].Real, (int)parameters[1].Real); } } }