Example #1
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);
        }
        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));
            }
        }