Work on new execution start path

This commit is contained in:
Deukhoofd 2018-11-26 16:55:10 +01:00
parent 15ce180b8c
commit 74da87d936
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
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;
}
}

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -19,7 +20,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("0.005 + 2.2", 2.205)]
public void Addition(string input, double expectedOutput)
{
var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
var actual = Executor.EvaluateScript<double>(input, Options);
Assert.Equal(expectedOutput, actual, 8);
}
@ -32,7 +33,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("10.256-2.8546", 7.4014)]
public void Subtraction(string input, double expectedOutput)
{
var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
var actual = Executor.EvaluateScript<double>(input, Options);
Assert.Equal(expectedOutput, actual, 8);
}
@ -42,7 +43,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("21312 * 41684", 888369408)]
public void Multiplication(string input, double expectedOutput)
{
var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
var actual = Executor.EvaluateScript<double>(input, Options);
Assert.Equal(expectedOutput, actual, 8);
}
@ -53,7 +54,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("656486 / 5146.0", 127.57209483)]
public void Divison(string input, double expectedOutput)
{
var actual = new Script(input, BoundScope, StaticScope).Evaluate<double>();
var actual = Executor.EvaluateScript<double>(input, Options);
Assert.Equal(expectedOutput, actual, 8);
}

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -16,12 +17,8 @@ namespace UpsilonTests.GeneralTests
-- test comment
return 100
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(100, result);
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(100, actual);
}
}

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -19,11 +20,8 @@ for i=0,5 do
end
return a
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(15, result);
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(15, actual);
}
[Fact]
@ -36,11 +34,8 @@ for i=0,10,2 do
end
return a
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(30, result);
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(30, actual);
}
[Fact]
@ -52,11 +47,8 @@ for i=5,0,-1 do
a = a + i
end
return a";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(15, result);
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(15, actual);
}
[Fact]
@ -72,11 +64,8 @@ for i=0,10 do
end
return a
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(15, result);
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(15, actual);
}
[Fact]
@ -90,11 +79,8 @@ for key, val in ipairs(arr) do
end
return value
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(184, result);
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(184, actual);
}
[Fact]
@ -109,11 +95,8 @@ for key, val in ipairs(arr) do
end
return value
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
Assert.Equal(6, result);
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(6, actual);
}
}
}

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -20,10 +21,7 @@ end
a = 50
testFunc()
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.Evaluate();
Assert.Empty(script.Diagnostics.Messages);
var script = Executor.ParseInputAndEvaluate(input, Options);
var a = script.GetVariable<long>("a");
Assert.Equal(100, a);
}
@ -38,10 +36,7 @@ end
a = 50
testFunc(100)
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.Evaluate();
Assert.Empty(script.Diagnostics.Messages);
var script = Executor.ParseInputAndEvaluate(input, Options);
var a = script.GetVariable<long>("a");
Assert.Equal(100, a);
}
@ -55,10 +50,7 @@ function testFunc (var1)
end
testFunc(100)
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.Evaluate();
Assert.Single(script.Diagnostics.Messages);
var script = Executor.ParseInputAndEvaluate(input, Options);
Assert.True(script.HasVariable("testFunc"));
}
@ -70,10 +62,7 @@ function testFunc (var1)
var1 == true
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var val = script.Evaluate();
Assert.Empty(script.Diagnostics.Messages);
var script = Executor.ParseInputAndEvaluate(input, Options);
Assert.True(script.HasVariable("testFunc"));
}
@ -86,10 +75,7 @@ function testFunc ()
end
a = testFunc()
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.Evaluate();
Assert.Empty(script.Diagnostics.Messages);
var script = Executor.ParseInputAndEvaluate(input, Options);
var a = script.GetVariable<long>("a");
Assert.Equal(5, a);
}
@ -104,10 +90,7 @@ function testFunc ()
end
a = testFunc()
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.Evaluate();
Assert.Empty(script.Diagnostics.Messages);
var script = Executor.ParseInputAndEvaluate(input, Options);
var a = script.GetVariable<long>("a");
Assert.Equal(5, a);
}
@ -124,10 +107,7 @@ function testFunc ()
end
a = testFunc()
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.Evaluate();
Assert.Empty(script.Diagnostics.Messages);
var script = Executor.ParseInputAndEvaluate(input, Options);
var a = script.GetVariable<long>("a");
Assert.Equal(5, a);
}
@ -140,10 +120,8 @@ a = 100
return 60
a = 87
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(60, result);
}
@ -155,10 +133,7 @@ function testFunc ()
return 100
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<long>("testFunc");
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<long>(input, "testFunc", options: Options);
Assert.Equal(100, result);
}
@ -174,13 +149,9 @@ function testFunc (b)
end
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<long>("testFunc", new object[] {true});
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<long>(input, "testFunc", new object[] {true}, Options);
Assert.Equal(100, result);
var result2 = script.EvaluateFunction<long>("testFunc", new object[] {false});
Assert.Empty(script.Diagnostics.Messages);
var result2 = Executor.EvaluateFunction<long>(input, "testFunc", new object[] {false}, Options);
Assert.Equal(50, result2);
}
@ -193,10 +164,7 @@ function add (a, b)
return a + b
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<long>("add", new object[] {400, 128});
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<long>(input, "add", new object[] {400,128}, Options);
Assert.Equal(528, result);
}
@ -211,10 +179,7 @@ for key, val in ipairs(arr) do
end
return value
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(6, result);
}
}

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -17,9 +18,7 @@ if true then
return true
end
return false";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var actual = script.Evaluate<bool>();
var actual = Executor.EvaluateScript<bool>(input, Options);
Assert.True(actual);
}
@ -36,9 +35,7 @@ if {condition} then
else
val = {in2}
end";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var actual = script.Evaluate<long>();
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(expected, actual);
}
@ -56,9 +53,7 @@ elseif {condition2} then
else
val = {in3}
end";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var actual = script.Evaluate<long>();
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(expected, actual);
}

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Binder;
using Upsilon.Evaluator;
using Upsilon.StandardLibraries;
@ -16,10 +17,7 @@ namespace UpsilonTests.GeneralTests
[InlineData(@"#{100,50,60,7863,1564,12354,10354}", 7)]
public void Test(string input, long expectedOutput)
{
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var actual = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(expectedOutput, actual);
}

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -14,7 +15,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("(10 + 5) * 5", 75)]
public void Parenthesis(string input, double expectedOutput)
{
var actual = new Script(input, BoundScope, StaticScope).Evaluate<long>();
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(expectedOutput, actual, 8);
}
@ -23,7 +24,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("5 + 10 * 5", 55)]
public void MultiplicationBeforeAddition(string input, double expectedOutput)
{
var actual = new Script(input, BoundScope, StaticScope).Evaluate<long>();
var actual = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(expectedOutput, actual, 8);
}

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -19,10 +20,7 @@ function testFunc()
end
testFunc()
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.Evaluate();
Assert.Empty(script.Diagnostics.Messages);
var script = Executor.ParseInputAndEvaluate(input, Options);
var a = script.GetVariable<long>("a");
Assert.Equal(10, a);
}
@ -37,10 +35,7 @@ if true then
end
b = a
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.Evaluate();
Assert.Empty(script.Diagnostics.Messages);
var script = Executor.ParseInputAndEvaluate(input, Options);
var a = script.GetVariable<long>("a");
Assert.Equal(100, a);
}
@ -56,10 +51,7 @@ for key, val in ipairs(arr) do
end
return value
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(184, result);
}

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -16,10 +17,7 @@ namespace UpsilonTests.GeneralTests
string = ""test""
return string
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<string>();
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<string>(input, Options);
Assert.Equal("test", evaluated);
}
@ -30,10 +28,7 @@ return string
string = ""test""
return string[3]
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<string>();
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<string>(input, Options);
Assert.Equal("s", evaluated);
}
@ -44,10 +39,7 @@ return string[3]
string = ""test"" + ""123""
return string
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<string>();
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<string>(input, Options);
Assert.Equal("test123", evaluated);
}

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -18,10 +19,7 @@ table = {
}
return table[2]
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(200, evaluated);
}
@ -36,10 +34,7 @@ table = {
}
return table[""another""]
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(10_000, evaluated);
}
@ -54,10 +49,7 @@ table = {
}
return table[""test""]
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate();
Assert.Single(script.Diagnostics.Messages);
Executor.EvaluateScript<long>(input, Options);
}
[Fact]
@ -74,10 +66,7 @@ table = {
}
return table[1][1][2]
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(600, evaluated);
}
@ -94,10 +83,7 @@ table = {
}
return table[""test""]()
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(100, evaluated);
}
@ -118,10 +104,7 @@ table = {
}
return table[""func""]()()[1]()
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(100, evaluated);
}
@ -138,10 +121,7 @@ table = {
}
return table[""test""]()
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(400, evaluated);
}
@ -156,10 +136,7 @@ table = {
}
return table[1]()
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(400, evaluated);
}
@ -171,10 +148,7 @@ table = {}
table[1] = 400
return table[1]
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(400, evaluated);
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -20,10 +21,7 @@ function getValue(arr)
return arr[""here""]
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr});
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(1683, evaluated);
}

View File

@ -1,4 +1,5 @@
using System.Collections.Generic;
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -15,10 +16,7 @@ function getValue(arr)
return arr[3]
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr});
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(56, evaluated);
}
@ -31,10 +29,7 @@ function getValue(arr)
return arr[2]
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var evaluated = script.EvaluateFunction<long>("getValue", new[] {arr});
Assert.Empty(script.Diagnostics.Messages);
var evaluated = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(30, evaluated);
}

View File

@ -1,6 +1,6 @@
using System;
using Upsilon;
using Upsilon.BaseTypes.UserData;
using Upsilon.Evaluator;
using Xunit;
// ReSharper disable UnusedMember.Local
@ -74,12 +74,9 @@ function add(o1, o2)
return o1 + o2
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100);
var o2 = new UserDataHelper(215);
var result = script.EvaluateFunction<UserDataHelper>("add", new[] {o1, o2});
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<UserDataHelper>(input, "add", new object[] {o1, o2}, Options);
Assert.Equal(315, result.Value);
}
@ -91,12 +88,9 @@ function add(o1, o2)
return o1 + o2
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100);
const double o2 = 1.5;
var result = script.EvaluateFunction<UserDataHelper>("add", new object[] {o1, o2});
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<UserDataHelper>(input, "add", new object[] {o1, o2}, Options);
Assert.Equal(101.5, result.Value);
}
@ -108,12 +102,9 @@ function subtract(o1, o2)
return o1 - o2
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100);
var o2 = new UserDataHelper(1.5);
var result = script.EvaluateFunction<UserDataHelper>("subtract", new object[] {o1, o2});
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<UserDataHelper>(input, "subtract", new object[] {o1, o2}, Options);
Assert.Equal(98.5, result.Value);
}
@ -125,12 +116,9 @@ function multiply(o1, o2)
return o1 * o2
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100);
var o2 = new UserDataHelper(4);
var result = script.EvaluateFunction<UserDataHelper>("multiply", new object[] {o1, o2});
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<UserDataHelper>(input, "multiply", new object[] {o1, o2}, Options);
Assert.Equal(400, result.Value);
}
@ -142,12 +130,9 @@ function divide(o1, o2)
return o1 / o2
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100);
var o2 = new UserDataHelper(10);
var result = script.EvaluateFunction<UserDataHelper>("divide", new object[] {o1, o2});
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<UserDataHelper>(input, "divide", new object[] {o1, o2}, Options);
Assert.Equal(10, result.Value);
}
@ -159,11 +144,8 @@ function negate(o1)
return -o1
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var o1 = new UserDataHelper(100);
var result = script.EvaluateFunction<UserDataHelper>("negate", new object[] {o1});
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<UserDataHelper>(input, "negate", new object[] {o1}, Options);
Assert.Equal(-100, result.Value);
}

View File

@ -1,4 +1,5 @@
using System;
using Upsilon;
using Upsilon.BaseTypes.UserData;
using Upsilon.Evaluator;
using Xunit;
@ -54,10 +55,7 @@ function test(o)
return o.fieldString
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<string>("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options);
Assert.Equal("TestField", result);
}
@ -70,10 +68,7 @@ function test(o)
o.FieldStringSet = ""Test""
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options);
Assert.Equal("Test", obj.FieldStringSet);
}
@ -86,11 +81,7 @@ function test(o)
o.testMethod()
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages);
Assert.True(obj.TestMethodHasRun);
Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options);
}
[Fact]
@ -102,10 +93,7 @@ function test(o)
return o.add(100, 20)
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.EvaluateFunction<long>("test", new[] {obj});
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateFunction<long>(input, "test", new[] {obj}, Options);
Assert.Equal(120, result);
}
@ -118,10 +106,7 @@ function test(o)
return o._privateTestField
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj});
Assert.Single(script.Diagnostics.Messages);
Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options);
}
[Fact]
@ -133,10 +118,7 @@ function test(o)
o.GetOnly = true
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj});
Assert.Single(script.Diagnostics.Messages);
Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options);
Assert.False(obj.GetOnly);
}
@ -149,10 +131,7 @@ function test(o)
o.PrivateSet = true
end
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
script.EvaluateFunction("test", new[] {obj});
Assert.Single(script.Diagnostics.Messages);
Executor.EvaluateFunction<string>(input, "test", new[] {obj}, Options);
Assert.False(obj.PrivateSet);
}

View File

@ -1,4 +1,5 @@
using System;
using Upsilon;
using Upsilon.Evaluator;
using Xunit;
@ -13,15 +14,14 @@ namespace UpsilonTests.StandardLibraryTests
[Fact]
public void AssertTest()
{
new Script("assert(true)", BoundScope, StaticScope).Evaluate();
Assert.Throws<Exception>(() => new Script("assert(false)", BoundScope, StaticScope).Evaluate());
Executor.EvaluateScript("assert(true)", Options);
Assert.Throws<Exception>(() => Executor.EvaluateScript("assert(true)", Options));
}
[Fact]
public void Error()
{
var e = Assert.Throws<Exception>(() =>
new Script(@"error(""test_error"")", BoundScope, StaticScope).Evaluate());
var e = Assert.Throws<Exception>(() => Executor.EvaluateScript(@"error(""test_error"")", Options));
Assert.Equal("test_error", e.Message);
}
@ -36,10 +36,7 @@ for key, val in ipairs(arr) do
end
return value
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(184, result);
}
@ -49,10 +46,7 @@ return value
const string input = @"
return tonumber(""100"")
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(100, result);
}
@ -67,10 +61,7 @@ for key, val in pairs(arr) do
end
return value
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<long>();
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateScript<long>(input, Options);
Assert.Equal(284, result);
}
@ -80,10 +71,7 @@ return value
const string input = @"
return tostring(100)
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<string>();
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateScript<string>(input, Options);
Assert.Equal("100", result);
}
@ -93,10 +81,7 @@ return tostring(100)
const string input = @"
return type(100)
";
var script = new Script(input, BoundScope, StaticScope);
Assert.Empty(script.Diagnostics.Messages);
var result = script.Evaluate<string>();
Assert.Empty(script.Diagnostics.Messages);
var result = Executor.EvaluateScript<string>(input, Options);
Assert.Equal("Number", result);
}

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Binder;
using Upsilon.Evaluator;
using Xunit;
@ -9,11 +10,17 @@ namespace UpsilonTests
{
protected EvaluationScope StaticScope { get; }
protected BoundScope BoundScope { get; }
protected ScriptOptions Options { get; }
protected TestClass(StaticScriptFixture fix)
{
StaticScope = fix.StaticScope;
BoundScope = fix.BoundScope;
Options = new ScriptOptions()
{
OverrideStaticScope = StaticScope,
OverrideStaticBoundScope = BoundScope,
};
}
}

View File

@ -21,8 +21,8 @@ namespace Ycicle
var input = Console.ReadLine();
if (input == "exit") return;
script = script == null
? new Script(input, boundScope, evaluationScope)
: Script.ContinueWith(script, input);
? Executor.ParseInput(input)
: Executor.ContinueWith(script, input);
if (script.Diagnostics.Messages.Count > 0)
{