96 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using Upsilon.BaseTypes;
using Upsilon.BaseTypes.Number;
using Upsilon.BaseTypes.ScriptTypeInterfaces;
using Upsilon.Binder;
using Upsilon.Binder.VariableSymbols;
using Upsilon.Evaluator;
using Type = Upsilon.BaseTypes.Type;
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.MethodInfoFunction);
var functionSymbol = new InternalFunctionVariableSymbol(func.Key, true,
func.Value.MethodInfoFunction.ReturnType.GetScriptType(),
func.Value.MethodInfoFunction.GetParameterInfo().Select(typeInfo =>
{
var derivedType = DeriveValidTypes(typeInfo.Type);
return new InternalFunctionVariableSymbol.InternalFunctionParameter(derivedType,
typeInfo.IsOptional);
}).ToArray())
{
CommentValue = func.Value.CommentValue?.Split('\n')
};
boundFuncs.Add(func.Key, functionSymbol);
}
var scope = new EvaluationScope(funcs);
var boundScope = new BoundScope(boundFuncs, 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);
}
private static Type DeriveValidTypes(System.Type type)
{
if (type == typeof(ScriptString))
return Type.String;
if (typeof(ScriptNumber).IsAssignableFrom(type))
return Type.Number;
if (type == typeof(ScriptBoolean))
return Type.Boolean;
if (type == typeof(IIterable))
return Type.Table | Type.UserData;
if (typeof(ScriptTable).IsAssignableFrom(type))
return Type.Table;
return Type.UserData;
}
}
}