Tweaks for tests, allow for grabbing global scope variables
This commit is contained in:
parent
7e1edbe3f1
commit
d6057ae954
|
@ -82,5 +82,18 @@ namespace Upsilon.BaseTypes.Number
|
|||
|
||||
#endregion
|
||||
|
||||
public static explicit operator double(Number n)
|
||||
{
|
||||
if (n.IsFloat)
|
||||
return ((NumberDouble) n);
|
||||
return ((NumberLong) n).Value;
|
||||
}
|
||||
|
||||
public static explicit operator long(Number n)
|
||||
{
|
||||
if (n.IsFloat)
|
||||
return (long)((NumberDouble) n).Value;
|
||||
return ((NumberLong) n).Value;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -51,5 +51,22 @@ namespace Upsilon.Evaluator
|
|||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool TryGet(string variable, out LuaType obj)
|
||||
{
|
||||
foreach (var luaType in _variables)
|
||||
{
|
||||
if (luaType.Key.Name == variable)
|
||||
{
|
||||
obj = luaType.Value;
|
||||
return true;
|
||||
};
|
||||
}
|
||||
if (_parentScope != null)
|
||||
if (_parentScope.TryGet(variable, out obj))
|
||||
return true;
|
||||
obj = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Upsilon.BaseTypes;
|
||||
using Upsilon.BaseTypes.Number;
|
||||
using Upsilon.Binder;
|
||||
|
@ -12,13 +11,7 @@ namespace Upsilon.Evaluator
|
|||
private LuaType _value;
|
||||
private readonly EvaluationScope _scope;
|
||||
|
||||
public Evaluator(Diagnostics diagnostics, Dictionary<VariableSymbol, LuaType> vars)
|
||||
{
|
||||
_diagnostics = diagnostics;
|
||||
_scope = new EvaluationScope(vars);
|
||||
}
|
||||
|
||||
private Evaluator(Diagnostics diagnostics, EvaluationScope parentScope)
|
||||
internal Evaluator(Diagnostics diagnostics, EvaluationScope parentScope)
|
||||
{
|
||||
_diagnostics = diagnostics;
|
||||
_scope = new EvaluationScope(parentScope);
|
||||
|
|
|
@ -13,6 +13,7 @@ namespace Upsilon.Evaluator
|
|||
private readonly BlockStatementSyntax _parsed;
|
||||
public Diagnostics Diagnostics { get; }
|
||||
private Binder.Binder Binder { get; }
|
||||
public EvaluationScope Scope { get; }
|
||||
|
||||
public Script(string scriptString, Dictionary<VariableSymbol, LuaType> variables = null)
|
||||
{
|
||||
|
@ -22,7 +23,8 @@ namespace Upsilon.Evaluator
|
|||
if (variables == null)
|
||||
variables = new Dictionary<VariableSymbol, LuaType>();
|
||||
Binder = new Binder.Binder(new BoundScope(variables, null), Diagnostics);
|
||||
Evaluator = new Evaluator( Diagnostics, variables);
|
||||
Scope = new EvaluationScope(variables);
|
||||
Evaluator = new Evaluator( Diagnostics, Scope);
|
||||
}
|
||||
|
||||
public BoundScript Bind()
|
||||
|
|
|
@ -17,8 +17,8 @@ namespace UpsilonTests
|
|||
[InlineData("0.005 + 2.2", 2.205)]
|
||||
public void Addition(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input).Evaluate<NumberDouble>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
var actual = new Script(input).Evaluate<Number>();
|
||||
Assert.Equal(expectedOutput, (double)actual, 8);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
@ -30,8 +30,8 @@ namespace UpsilonTests
|
|||
[InlineData("10.256-2.8546", 7.4014)]
|
||||
public void Subtraction(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input).Evaluate<NumberDouble>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
var actual = new Script(input).Evaluate<Number>();
|
||||
Assert.Equal(expectedOutput, (double)actual, 8);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
|
@ -40,18 +40,19 @@ namespace UpsilonTests
|
|||
[InlineData("21312 * 41684", 888369408)]
|
||||
public void Multiplication(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input).Evaluate<NumberDouble>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
var actual = new Script(input).Evaluate<Number>();
|
||||
Assert.Equal(expectedOutput, (double)actual, 8);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("1/1", 1)]
|
||||
[InlineData("1000 / 10", 100)]
|
||||
[InlineData("656486 / 5146", 127.57209483)]
|
||||
[InlineData("656486 / 5146", 127)]
|
||||
[InlineData("656486 / 5146.0", 127.57209483)]
|
||||
public void Divison(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input).Evaluate<NumberDouble>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
var actual = new Script(input).Evaluate<Number>();
|
||||
Assert.Equal(expectedOutput, (double)actual, 8);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ namespace UpsilonTests
|
|||
[InlineData("(10 + 5) * 5", 75)]
|
||||
public void Parenthesis(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input).Evaluate<NumberDouble>();
|
||||
var actual = new Script(input).Evaluate<NumberLong>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ namespace UpsilonTests
|
|||
[InlineData("5 + 10 * 5", 55)]
|
||||
public void MultiplicationBeforeAddition(string input, double expectedOutput)
|
||||
{
|
||||
var actual = new Script(input).Evaluate<NumberDouble>();
|
||||
var actual = new Script(input).Evaluate<NumberLong>();
|
||||
Assert.Equal(expectedOutput, actual, 8);
|
||||
}
|
||||
|
||||
|
|
|
@ -14,13 +14,13 @@ a = 10
|
|||
if true then
|
||||
local a = 100
|
||||
end
|
||||
b = a
|
||||
";
|
||||
var script = new Script(input);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluate = script.Evaluate<NumberLong>();
|
||||
script.Evaluate();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal((long)10, evaluate);
|
||||
Assert.True(script.Scope.TryGet("a", out var obj));
|
||||
Assert.Equal((long)10, (NumberLong)obj);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
@ -35,9 +35,10 @@ b = a
|
|||
";
|
||||
var script = new Script(input);
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
var evaluate = script.Evaluate<NumberLong>();
|
||||
script.Evaluate();
|
||||
Assert.Empty(script.Diagnostics.Messages);
|
||||
Assert.Equal((long)100, evaluate);
|
||||
Assert.True(script.Scope.TryGet("a", out var obj));
|
||||
Assert.Equal((long)100, (NumberLong)obj);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue