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 class Script : IDisposable
{ {
public ScriptOptions Options { get; }
private readonly string _scriptString;
public SourceText ScriptString { get; } public SourceText ScriptString { get; }
private Evaluator Evaluator { get; } private Evaluator Evaluator { get; }
private BlockStatementSyntax _parsed; private BlockStatementSyntax _parsed;
@ -20,18 +22,18 @@ namespace Upsilon.Evaluator
private Binder.Binder Binder { get; } private Binder.Binder Binder { get; }
private EvaluationScope Scope { get; } private EvaluationScope Scope { get; }
public Script(string scriptString, BoundScope overrideBoundStaticScope = null, internal Script(string scriptString, ScriptOptions options )
EvaluationScope overrideStaticScope = null )
{ {
Options = options;
_scriptString = scriptString;
ScriptString = new SourceText(scriptString); ScriptString = new SourceText(scriptString);
Diagnostics = new Diagnostics(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); var boundScope = BoundScope.WithReadOnlyScope(staticBoundScope);
Binder = Upsilon.Binder.Binder.CreateWithSetScope(Diagnostics, boundScope); Binder = Upsilon.Binder.Binder.CreateWithSetScope(Diagnostics, boundScope);
var staticScope = overrideStaticScope ?? StaticScope.Scope; var staticScope = options.OverrideStaticScope ?? StaticScope.Scope;
Scope = EvaluationScope.CreateWithGetOnlyParent(staticScope); Scope = EvaluationScope.CreateWithGetOnlyParent(staticScope);
Evaluator = Evaluator.CreateWithSetScope(Diagnostics, Scope); Evaluator = Evaluator.CreateWithSetScope(Diagnostics, Scope);
} }
@ -40,18 +42,24 @@ namespace Upsilon.Evaluator
{ {
ScriptString = new SourceText(scriptString); ScriptString = new SourceText(scriptString);
Diagnostics = new Diagnostics(ScriptString); Diagnostics = new Diagnostics(ScriptString);
_parsed = Parser.Parser.Parse(scriptString, Diagnostics);
Binder = new Binder.Binder(Diagnostics, binder.Scope.Variables); Binder = new Binder.Binder(Diagnostics, binder.Scope.Variables);
Evaluator = new Evaluator( Diagnostics, evaluator.Scope.Variables); Evaluator = new Evaluator( Diagnostics, evaluator.Scope.Variables);
Scope = Evaluator.Scope; 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); var s = new Script(scriptString, previousScript.Binder, previousScript.Evaluator);
return s; return s;
} }
internal void Parse()
{
_parsed = Parser.Parser.Parse(_scriptString, Diagnostics);
}
public BoundScript Bind() public BoundScript Bind()
{ {
return _bound ?? (_bound = Binder.BindScript(_parsed)); 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 Upsilon.Evaluator;
using Xunit; using Xunit;
@ -19,7 +20,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("0.005 + 2.2", 2.205)] [InlineData("0.005 + 2.2", 2.205)]
public void Addition(string input, double expectedOutput) 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); Assert.Equal(expectedOutput, actual, 8);
} }
@ -32,7 +33,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("10.256-2.8546", 7.4014)] [InlineData("10.256-2.8546", 7.4014)]
public void Subtraction(string input, double expectedOutput) 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); Assert.Equal(expectedOutput, actual, 8);
} }
@ -42,7 +43,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("21312 * 41684", 888369408)] [InlineData("21312 * 41684", 888369408)]
public void Multiplication(string input, double expectedOutput) 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); Assert.Equal(expectedOutput, actual, 8);
} }
@ -53,7 +54,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("656486 / 5146.0", 127.57209483)] [InlineData("656486 / 5146.0", 127.57209483)]
public void Divison(string input, double expectedOutput) 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); Assert.Equal(expectedOutput, actual, 8);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,4 @@
using Upsilon;
using Upsilon.Evaluator; using Upsilon.Evaluator;
using Xunit; using Xunit;
@ -14,7 +15,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("(10 + 5) * 5", 75)] [InlineData("(10 + 5) * 5", 75)]
public void Parenthesis(string input, double expectedOutput) 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); Assert.Equal(expectedOutput, actual, 8);
} }
@ -23,7 +24,7 @@ namespace UpsilonTests.GeneralTests
[InlineData("5 + 10 * 5", 55)] [InlineData("5 + 10 * 5", 55)]
public void MultiplicationBeforeAddition(string input, double expectedOutput) 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); Assert.Equal(expectedOutput, actual, 8);
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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