using System.Collections.Immutable; using System.Linq; using Upsilon.BaseTypes; using Upsilon.Binder; using Upsilon.Evaluator; namespace Upsilon.StandardLibraries { public static class StaticScope { private static EvaluationScope _staticScope; private static BoundScope _staticBoundScope; public static EvaluationScope Scope { get { if (_staticScope != null) return _staticScope; var (evaluationScope, boundScope) = CreateStandardLibrary(); _staticScope = evaluationScope; _staticBoundScope = boundScope; return _staticScope; } } public static BoundScope BoundScope { get { if (_staticBoundScope != null) return _staticBoundScope; var (evaluationScope, boundScope) = CreateStandardLibrary(); _staticScope = evaluationScope; _staticBoundScope = boundScope; return _staticBoundScope; } } public static (EvaluationScope, BoundScope) CreateStandardLibrary() { var basicFunctions = new BasicFunctions().LoadMethods(); var scope = new EvaluationScope(basicFunctions.ToDictionary(x => x.Key, x => (ScriptType)x.Value)); var boundScope = new BoundScope( scope.Variables.ToDictionary(x => x.Key, x => (VariableSymbol) new FunctionVariableSymbol(x.Key, false, ImmutableArray.Empty, x.Value.Type) {IsBound = true}), null); return (scope, boundScope); } public static void RegisterStaticVariable(string name, object value) { var luaVariable = value.ToScriptType(); var varSymbol = new VariableSymbol(name, luaVariable.Type, false); BoundScope.AssignToNearest(varSymbol); Scope.AssignToNearest(varSymbol, luaVariable); } } }