Ejemplo n.º 1
0
        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>());
        }
Ejemplo n.º 2
0
        public async Task <DynamicReturnValue> Evaluate(Scope scope)
        {
            DynamicReturnValue left = await Left.Evaluate(scope);

            DynamicReturnValue right = await Right.Evaluate(scope);

            switch (Type)
            {
            case EScriptTokenType.EQUALS:
                return(new DynamicReturnValue(left == right));

            case EScriptTokenType.NOT_EQUALS:
                return(new DynamicReturnValue(left != right));

            case EScriptTokenType.GREATER_THAN:
                return(new DynamicReturnValue(left > right));

            case EScriptTokenType.LESS_THAN:
                return(new DynamicReturnValue(left < right));

            case EScriptTokenType.GREATER_THAN_EQUAL:
                return(new DynamicReturnValue(left >= right));

            case EScriptTokenType.LESS_THAN_EQUAL:
                return(new DynamicReturnValue(left <= right));
            }

            return(new DynamicReturnValue(false));
        }
Ejemplo n.º 3
0
        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));
        }
Ejemplo n.º 4
0
        public async Task <DynamicReturnValue> Evaluate(Scope scope)
        {
            DynamicReturnValue returnValue = await this.Condition.Evaluate(scope);

            if (!returnValue.IsNull())
            {
                DynamicReturnValue two = await this.Block.Evaluate(scope);

                return(two);
            }
            return(returnValue);
        }
Ejemplo n.º 5
0
        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));
        }
Ejemplo n.º 6
0
        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>());
        }
Ejemplo n.º 7
0
        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>());
        }
Ejemplo n.º 8
0
        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>());
        }
Ejemplo n.º 9
0
        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>());
        }
Ejemplo n.º 10
0
        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>());
        }
Ejemplo n.º 11
0
        private async Task <List <T> > BuildList <T>(Scope scope)
        {
            List <T> values = new List <T>();

            foreach (AstTreeNode val in _array)
            {
                DynamicReturnValue value = await val.Evaluate(scope);

                values.Add((T)value.Value);
            }

            return(values);
        }
Ejemplo n.º 12
0
        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)));
        }
Ejemplo n.º 13
0
        public async Task <DynamicReturnValue> Evaluate(Scope scope)
        {
            foreach (ConditionalNode condition in Nodes)
            {
                DynamicReturnValue uno = await condition.Condition.Evaluate(scope);

                if (uno.Value != null && (bool)uno.Value)
                {
                    DynamicReturnValue dose = await condition.Block.Evaluate(scope);

                    return(dose);
                }
            }

            return(new DynamicReturnValue(null));
        }
Ejemplo n.º 14
0
        public async Task <DynamicReturnValue> Evaluate(Scope scope)
        {
            string name = await RootNode.GetName(scope);

            DynamicReturnValue value = await ToAssign.Evaluate(scope);

            Scope innerScope = await RootNode.GetScope(scope);

            innerScope.Set(name, value.Value);
            if (innerScope.Get(name) != value.Value)
            {
                Console.WriteLine($"System Error: Failed to assign ${name} to ${value}");
            }

            return(value);
        }
Ejemplo n.º 15
0
        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>());
        }
Ejemplo n.º 16
0
        public async Task <DynamicReturnValue> Evaluate(Scope scope)
        {
            DynamicReturnValue returnValue = new DynamicReturnValue(null);

            if (Statements == null)
            {
                return(returnValue);
            }

            for (int i = 0; i < Statements.Count; i++)
            {
                returnValue = await Statements[i].Evaluate(scope);
            }

            return(returnValue);
        }
Ejemplo n.º 17
0
        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>());
        }
Ejemplo n.º 18
0
        public async Task <DynamicReturnValue> Evaluate(Scope scope)
        {
            DynamicReturnValue first = await _array[0].Evaluate(scope);
            Type t = first.Type;

            if (t == typeof(int))
            {
                return(new DynamicReturnValue(await BuildList <int>(scope)));
            }

            if (t == typeof(float))
            {
                return(new DynamicReturnValue(await BuildList <float>(scope)));
            }

            if (t == typeof(string))
            {
                return(new DynamicReturnValue(await BuildList <string>(scope)));
            }

            return(new DynamicReturnValue(BuildList <object>(scope)));
        }
        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));
            }
        }