Work on new execution start path

This commit is contained in:
2018-11-26 16:55:10 +01:00
parent 15ce180b8c
commit 74da87d936
20 changed files with 197 additions and 264 deletions

View File

@@ -12,6 +12,8 @@ namespace Upsilon.Evaluator
{
public class Script : IDisposable
{
public ScriptOptions Options { get; }
private readonly string _scriptString;
public SourceText ScriptString { get; }
private Evaluator Evaluator { get; }
private BlockStatementSyntax _parsed;
@@ -20,18 +22,18 @@ namespace Upsilon.Evaluator
private Binder.Binder Binder { get; }
private EvaluationScope Scope { get; }
public Script(string scriptString, BoundScope overrideBoundStaticScope = null,
EvaluationScope overrideStaticScope = null )
internal Script(string scriptString, ScriptOptions options )
{
Options = options;
_scriptString = scriptString;
ScriptString = new SourceText(scriptString);
Diagnostics = new Diagnostics(ScriptString);
_parsed = Parser.Parser.Parse(scriptString, Diagnostics);
var staticBoundScope = overrideBoundStaticScope ?? StaticScope.BoundScope;
var staticBoundScope = options.OverrideStaticBoundScope ?? StaticScope.BoundScope;
var boundScope = BoundScope.WithReadOnlyScope(staticBoundScope);
Binder = Upsilon.Binder.Binder.CreateWithSetScope(Diagnostics, boundScope);
var staticScope = overrideStaticScope ?? StaticScope.Scope;
var staticScope = options.OverrideStaticScope ?? StaticScope.Scope;
Scope = EvaluationScope.CreateWithGetOnlyParent(staticScope);
Evaluator = Evaluator.CreateWithSetScope(Diagnostics, Scope);
}
@@ -40,18 +42,24 @@ namespace Upsilon.Evaluator
{
ScriptString = new SourceText(scriptString);
Diagnostics = new Diagnostics(ScriptString);
_parsed = Parser.Parser.Parse(scriptString, Diagnostics);
Binder = new Binder.Binder(Diagnostics, binder.Scope.Variables);
Evaluator = new Evaluator( Diagnostics, evaluator.Scope.Variables);
Scope = Evaluator.Scope;
}
public static Script ContinueWith(Script previousScript, string scriptString)
internal static Script ContinueWith(Script previousScript, string scriptString)
{
var s = new Script(scriptString, previousScript.Binder, previousScript.Evaluator);
return s;
}
internal void Parse()
{
_parsed = Parser.Parser.Parse(_scriptString, Diagnostics);
}
public BoundScript Bind()
{
return _bound ?? (_bound = Binder.BindScript(_parsed));

57
Upsilon/Executor.cs Normal file
View File

@@ -0,0 +1,57 @@
using Upsilon.Evaluator;
namespace Upsilon
{
public static class Executor
{
private static readonly ScriptOptions DefaultOptions = new ScriptOptions();
public static Script ParseInput(string input, ScriptOptions options = null)
{
if (options == null) options = DefaultOptions;
var script = new Script(input, options);
script.Parse();
return script;
}
public static Script ParseInputAndEvaluate(string input, ScriptOptions options = null)
{
if (options == null) options = DefaultOptions;
var script = new Script(input, options);
script.Parse();
script.Evaluate();
return script;
}
public static Script ContinueWith(Script script, string input)
{
var s = Script.ContinueWith(script, input);
s.Parse();
return s;
}
public static T EvaluateScript<T>(string input, ScriptOptions options = null)
{
var script = ParseInput(input, options);
return script.Evaluate<T>();
}
public static object EvaluateScript(string input, ScriptOptions options = null)
{
var script = ParseInput(input, options);
return script.Evaluate();
}
public static T EvaluateFunction<T>(string input, string function, object[] parameters = null, ScriptOptions options = null)
{
var script = ParseInput(input, options);
return script.EvaluateFunction<T>(function, parameters);
}
public static object EvaluateFunction(string input, string function, object[] parameters = null, ScriptOptions options = null)
{
var script = ParseInput(input, options);
return script.EvaluateFunction(function, parameters);
}
}
}

24
Upsilon/ScriptOptions.cs Normal file
View File

@@ -0,0 +1,24 @@
using Upsilon.Binder;
using Upsilon.Evaluator;
namespace Upsilon
{
public class ScriptOptions
{
/// <summary>
/// Whether to persist comments on functions and variables as data or not. This is generally helpful for tools
/// or IDEs, but not for actual runtime.
/// </summary>
public bool SaveDataComments { get; set; } = false;
/// <summary>
/// Whether or not to throw an exception when errors are encountered. Throwing exceptions is useful for general
/// execution, as it will quickly tell you something is wrong, however this will only show the first error in a
/// script, which may not be helpful. IDEs and other tools should use false for this to show all errors in a script.
/// </summary>
public bool ThrowExceptionOnError { get; set; } = true;
public BoundScope OverrideStaticBoundScope { get; set; } = null;
public EvaluationScope OverrideStaticScope { get; set; } = null;
}
}