Upsilon/Upsilon/StandardLibraries/StaticScope.cs

67 lines
2.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2018-11-23 12:28:11 +00:00
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 funcs = new Dictionary<string, ScriptType>();
var boundFuncs = new Dictionary<string, VariableSymbol>();
foreach (var func in basicFunctions)
{
funcs.Add(func.Key, func.Value);
boundFuncs.Add(func.Key, new FunctionVariableSymbol(func.Key, true, ImmutableArray<VariableSymbol>.Empty,
func.Value.ReturnType.GetScriptType(), true){IsBound = true});
}
var scope = new EvaluationScope(funcs);
var boundScope = new BoundScope(boundFuncs, null);
2018-11-23 12:28:11 +00:00
return (scope, boundScope);
}
public static void RegisterStaticVariable(string name, object value)
{
var luaVariable = value.ToScriptType();
2018-11-23 12:28:11 +00:00
var varSymbol = new VariableSymbol(name, luaVariable.Type, false);
2018-11-24 11:42:54 +00:00
BoundScope.AssignToNearest(varSymbol);
Scope.AssignToNearest(varSymbol, luaVariable);
2018-11-23 12:28:11 +00:00
}
}
}