diff --git a/Upsilon/Evaluator/Script.cs b/Upsilon/Evaluator/Script.cs index 69ccb13..270a855 100644 --- a/Upsilon/Evaluator/Script.cs +++ b/Upsilon/Evaluator/Script.cs @@ -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); } diff --git a/Upsilon/StandardLibraries/StandardLibrary.cs b/Upsilon/StandardLibraries/StandardLibrary.cs deleted file mode 100644 index d895703..0000000 --- a/Upsilon/StandardLibraries/StandardLibrary.cs +++ /dev/null @@ -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.Empty){IsBound = true}), - null); - return (scope, boundScope); - } - } -} \ No newline at end of file diff --git a/Upsilon/StandardLibraries/StaticScope.cs b/Upsilon/StandardLibraries/StaticScope.cs new file mode 100644 index 0000000..0131a07 --- /dev/null +++ b/Upsilon/StandardLibraries/StaticScope.cs @@ -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.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); + } + } +} \ No newline at end of file diff --git a/UpsilonTests/TestClass.cs b/UpsilonTests/TestClass.cs index c776f96..3993b5e 100644 --- a/UpsilonTests/TestClass.cs +++ b/UpsilonTests/TestClass.cs @@ -33,7 +33,7 @@ namespace UpsilonTests public StaticScriptFixture() { - var (evaluationScope, boundScope) = StandardLibrary.Create(); + var (evaluationScope, boundScope) = Upsilon.StandardLibraries.StaticScope.CreateStandardLibrary(); StaticScope = evaluationScope; BoundScope = boundScope; } diff --git a/Ycicle/Program.cs b/Ycicle/Program.cs index 3c3ab27..4fd73bc 100644 --- a/Ycicle/Program.cs +++ b/Ycicle/Program.cs @@ -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();