Better handling of Static Scope

This commit is contained in:
Deukhoofd 2018-11-23 13:28:11 +01:00
parent 95f76bc5e3
commit d6e18d9b0c
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
5 changed files with 71 additions and 29 deletions

View File

@ -4,6 +4,7 @@ using System.Collections.Immutable;
using Upsilon.BaseTypes;
using Upsilon.Binder;
using Upsilon.Parser;
using Upsilon.StandardLibraries;
using Upsilon.Text;
using Upsilon.Utilities;
@ -19,15 +20,18 @@ namespace Upsilon.Evaluator
private Binder.Binder Binder { get; }
private EvaluationScope Scope { get; }
public Script(string scriptString, BoundScope boundStaticScope, EvaluationScope staticScope )
public Script(string scriptString, BoundScope overrideBoundStaticScope = null,
EvaluationScope overrideStaticScope = null )
{
ScriptString = new SourceText(scriptString);
Diagnostics = new Diagnostics(ScriptString);
_parsed = Parser.Parser.Parse(scriptString, Diagnostics);
var boundScope = BoundScope.WithReadOnlyScope(boundStaticScope);
var staticBoundScope = overrideBoundStaticScope ?? StaticScope.BoundScope;
var boundScope = BoundScope.WithReadOnlyScope(staticBoundScope);
Binder = Upsilon.Binder.Binder.CreateWithSetScope(Diagnostics, boundScope);
var staticScope = overrideStaticScope ?? StaticScope.Scope;
Scope = EvaluationScope.CreateWithGetOnlyParent(staticScope);
Evaluator = Evaluator.CreateWithSetScope(Diagnostics, Scope);
}

View File

@ -1,23 +0,0 @@
using System.Collections.Immutable;
using System.Linq;
using Upsilon.BaseTypes;
using Upsilon.Binder;
using Upsilon.Evaluator;
namespace Upsilon.StandardLibraries
{
public class StandardLibrary
{
public static (EvaluationScope, BoundScope) Create()
{
var basicFunctions = new BasicFunctions().LoadMethods();
var scope = new EvaluationScope(basicFunctions.ToDictionary(x => x.Key, x => (LuaType)x.Value));
var boundScope =
new BoundScope(
scope.Variables.ToDictionary(x => x.Key
, x => (VariableSymbol)new FunctionVariableSymbol(x.Key, x.Value.Type, false, ImmutableArray<VariableSymbol>.Empty){IsBound = true}),
null);
return (scope, boundScope);
}
}
}

View File

@ -0,0 +1,62 @@
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 => (LuaType)x.Value));
var boundScope =
new BoundScope(
scope.Variables.ToDictionary(x => x.Key
, x => (VariableSymbol)new FunctionVariableSymbol(x.Key, x.Value.Type, false, ImmutableArray<VariableSymbol>.Empty){IsBound = true}),
null);
return (scope, boundScope);
}
public static void RegisterStaticVariable(string name, object value)
{
var luaVariable = value.ToLuaType();
var varSymbol = new VariableSymbol(name, luaVariable.Type, false);
BoundScope.SetVariable(varSymbol);
Scope.Set(varSymbol, luaVariable);
}
}
}

View File

@ -33,7 +33,7 @@ namespace UpsilonTests
public StaticScriptFixture()
{
var (evaluationScope, boundScope) = StandardLibrary.Create();
var (evaluationScope, boundScope) = Upsilon.StandardLibraries.StaticScope.CreateStandardLibrary();
StaticScope = evaluationScope;
BoundScope = boundScope;
}

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Upsilon;
using Upsilon.Evaluator;
@ -14,7 +13,7 @@ namespace Ycicle
{
Console.WriteLine("Upsilon REPL");
Script script = null;
var (evaluationScope, boundScope) = StandardLibrary.Create();
var (evaluationScope, boundScope) = StaticScope.CreateStandardLibrary();
while (true)
{
@ -101,7 +100,7 @@ namespace Ycicle
}
else
{
sb.Append(o.ToString());
sb.Append(o);
}
return sb.ToString();