public async Task TestScopeNode() { ScriptTree scriptTree = new ScriptTree("foo.bar;"); Scope scope = new Scope(); Scope innerScope = new Scope(); scope.Set("foo", innerScope); innerScope.Set("bar", 5); DynamicReturnValue value = await scriptTree.Evaluate(scope); Console.WriteLine($"result {value.GetValue<int>()}"); Assert.Equal(5, value.GetValue <int>()); innerScope.Set("bar", false); value = await scriptTree.Evaluate(scope); Assert.Equal(false, value.GetValue <bool>()); scriptTree = new ScriptTree("foo.baz.nitch"); Scope lastScope = new Scope(); lastScope.Set("nitch", 101); innerScope.Set("baz", lastScope); value = await scriptTree.Evaluate(scope); Assert.Equal(101, value.GetValue <int>()); }
public async Task <DynamicReturnValue> Evaluate(Scope scope) { string variable = (await Variable.Evaluate(scope)).GetValue <string>(); DynamicReturnValue value = (await Array.Evaluate(scope)); Type type = value.Type; if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>)) { if (type == typeof(List <float>)) { foreach (float val in value.GetValue <List <float> >()) { await EvaluateBlock(scope, variable, val); } } else if (type == typeof(List <int>)) { foreach (int val in value.GetValue <List <int> >()) { await EvaluateBlock(scope, variable, val); } } else if (type == typeof(List <string>)) { foreach (string val in value.GetValue <List <string> >()) { await EvaluateBlock(scope, variable, val); } } } return(new DynamicReturnValue(null)); }
public async Task TestStringLiteral() { ScriptTree scriptTree = new ScriptTree("'testing'"); DynamicReturnValue value = await scriptTree.Evaluate(new Scope()); Assert.Equal("testing", value.GetValue <string>()); scriptTree = new ScriptTree("\"testing double quotes\""); value = await scriptTree.Evaluate(new Scope()); Assert.Equal("testing double quotes", value.GetValue <string>()); }
public async Task TestBooleanLiteral() { ScriptTree scriptTree = new ScriptTree("true"); DynamicReturnValue value = await scriptTree.Evaluate(new Scope()); Assert.Equal(true, value.GetValue <bool>()); scriptTree = new ScriptTree("false"); value = await scriptTree.Evaluate(new Scope()); Assert.Equal(false, value.GetValue <bool>()); }
public async Task TestNumberLiteral() { ScriptTree scriptTree = new ScriptTree("5.52"); DynamicReturnValue value = await scriptTree.Evaluate(new Scope()); Assert.Equal(5.52f, value.GetValue <float>()); scriptTree = new ScriptTree("5"); value = await scriptTree.Evaluate(new Scope()); Assert.Equal(5, value.GetValue <int>()); }
public async Task <DynamicReturnValue> Evaluate(Scope scope) { DynamicReturnValue parent = await Scope.Evaluate(scope); if (parent.IsNull()) { Console.WriteLine("ERROR ERROR BEEP BOOP"); return(new DynamicReturnValue(null)); } string name = await GetName(scope); Type t = parent.GetValue <Scope>().GetType(name); return(new DynamicReturnValue(parent.GetValue <Scope>().Get(name))); }
public async Task TestRootScopeNode() { ScriptTree scriptTree = new ScriptTree("foo"); Scope scope = new Scope(); scope.Set("foo", 5); DynamicReturnValue value = await scriptTree.Evaluate(scope); Assert.Equal(5, value.GetValue <int>()); scriptTree = new ScriptTree("foo"); scope.Set("foo", false); value = await scriptTree.Evaluate(scope); Assert.Equal(false, value.GetValue <bool>()); }
public async Task TestAsyncFunctionCallNode() { Function function = new Function(typeof(FunctionTest), "MyTestAsyncFunction"); ScriptTree scriptTree = new ScriptTree(@"test_function(words)"); Scope scope = new Scope(); string words = "pancakes are super tasty."; scope.Set("words", words); scope.Set("test_function", function); DynamicReturnValue r = await scriptTree.Evaluate(scope); Assert.Equal(words, r.GetValue <string>()); }
public async Task TestFunctionMultipleArgumentsCallNode() { Function function = new Function(typeof(FunctionTest), "MyTestFunctionWithMultipleArguments"); ScriptTree scriptTree = new ScriptTree(@"test_function(words, 'Cheese is also rather tasty.')"); Scope scope = new Scope(); string words = "pancakes are tasty."; scope.Set("words", words); scope.Set("test_function", function); DynamicReturnValue r = await scriptTree.Evaluate(scope); Assert.Equal($"{words} Cheese is also rather tasty.", r.GetValue <string>()); }
public async Task TestScopeAssignmentNode() { ScriptTree scriptTree = new ScriptTree("foo = 150"); Scope scope = new Scope(); await scriptTree.Evaluate(scope); Assert.Equal(150, scope.Get("foo")); scriptTree = new ScriptTree("bar = 1.5;bar;"); DynamicReturnValue value = await scriptTree.Evaluate(scope); Assert.Equal(1.5f, value.GetValue <float>()); scope = new Scope(); scriptTree = new ScriptTree("foo.bar = 7.5;foo.bar;"); Scope innerScope = new Scope(); scope.Set("foo", innerScope); value = await scriptTree.Evaluate(scope); Assert.Equal(7.5f, value.GetValue <float>()); }
public async Task <DynamicReturnValue> Evaluate(Scope scope) { DynamicReturnValue left = await Left.Evaluate(scope); DynamicReturnValue right = await Right.Evaluate(scope); Type leftType = left.Type; Type rightType = right.Type; if (leftType == typeof(int) && rightType == typeof(int)) { switch (Type) { case EScriptTokenType.ADD: return(new DynamicReturnValue(left.GetValue <int>() + right.GetValue <int>())); case EScriptTokenType.SUBTRACT: return(new DynamicReturnValue(left.GetValue <int>() - right.GetValue <int>())); case EScriptTokenType.MULTIPLY: return(new DynamicReturnValue(left.GetValue <int>() * right.GetValue <int>())); case EScriptTokenType.DIVIDE: return(new DynamicReturnValue(left.GetValue <int>() / right.GetValue <int>())); } } else { float leftValue = leftType == typeof(int) ? left.GetValue <int>() : left.GetValue <float>(); float rightValue = rightType == typeof(int) ? right.GetValue <int>() : right.GetValue <float>(); switch (Type) { case EScriptTokenType.ADD: return(new DynamicReturnValue(leftValue + rightValue)); case EScriptTokenType.SUBTRACT: return(new DynamicReturnValue(leftValue - rightValue)); case EScriptTokenType.MULTIPLY: return(new DynamicReturnValue(leftValue * rightValue)); case EScriptTokenType.DIVIDE: return(new DynamicReturnValue(leftValue / rightValue)); } } return(new DynamicReturnValue(null)); }
public async Task <DynamicReturnValue> Evaluate(Scope scope) { string name = await Left.GetName(scope); Scope innerScope = await Left.GetScope(scope); DynamicReturnValue left = await Left.Evaluate(scope); DynamicReturnValue right = await Right.Evaluate(scope); Type leftType = left.Type; Type rightType = right.Type; if (leftType == typeof(int) && rightType == typeof(int)) { int value = default; switch (Type) { case EScriptTokenType.ADD_ASSIGN: value = left.GetValue <int>() + right.GetValue <int>(); break; case EScriptTokenType.SUBTRACT_ASSIGN: value = left.GetValue <int>() - right.GetValue <int>(); break; case EScriptTokenType.MULTIPLY_ASSIGN: value = left.GetValue <int>() * right.GetValue <int>(); break; case EScriptTokenType.DIVIDE_ASSIGN: value = left.GetValue <int>() / right.GetValue <int>(); break; } innerScope.Set(name, value); if ((int)innerScope.Get(name) != value) { Console.WriteLine("System Error: Failed to assign ${name} to ${right}"); return(new DynamicReturnValue(null)); } return(new DynamicReturnValue(value)); } else { float value = default; float leftValue = leftType == typeof(int) ? left.GetValue <int>() : left.GetValue <float>(); float rightValue = rightType == typeof(int) ? right.GetValue <int>() : right.GetValue <float>(); switch (Type) { case EScriptTokenType.ADD_ASSIGN: value = leftValue + rightValue; break; case EScriptTokenType.SUBTRACT_ASSIGN: value = leftValue - rightValue; break; case EScriptTokenType.MULTIPLY_ASSIGN: value = leftValue * rightValue; break; case EScriptTokenType.DIVIDE_ASSIGN: value = leftValue / rightValue; break; } innerScope.Set(name, value); if ((float)innerScope.Get(name) != value) { Console.WriteLine("System Error: Failed to assign ${name} to ${right}"); return(new DynamicReturnValue(null)); } return(new DynamicReturnValue(value)); } }