Ejemplo n.º 1
0
 /// <summary>
 /// Main constructor
 /// </summary>
 /// <param name="superBlock">Super block</param>
 /// <param name="name">Method name</param>
 /// <param name="returnType">Method return type</param>
 /// <param name="parameters">Method parameters</param>
 public Method(Block superBlock, string name, VariableType returnType, Parameter[] parameters) : base(superBlock)
 {
     Name = name;
     ReturnType = returnType;
     Parameters = parameters;
     ReturnValue = null;
 }
        /// <summary>
        /// Apply operator
        /// </summary>
        /// <param name="left">Left value</param>
        /// <param name="right">Right value</param>
        /// <param name="op">Operator</param>
        /// <returns>Result value</returns>
        Value Calculate(Value left, Value right, TokenType op)
        {
            switch (left.VariableType)
            {
                case VariableType.BOOL:
                    //TODO Bool calculate
                    break;

                case VariableType.STRING:
                    if (CanBe.Converted(typeof(string), right.ObjectValue))
                    {
                        switch (op)
                        {
                            case TokenType.PLUS:
                                return new Value(VariableType.STRING, left.ValueToString() + right.ValueToString());
                            default:
                                break;
                        }
                    }
                    break;

                case VariableType.INT:
                case VariableType.FLOAT:
                    if (CanBe.Converted(typeof(float), right.ObjectValue))
                    {
                        switch (op)
                        {
                            case TokenType.PLUS:
                                return new Value(VariableType.FLOAT, left.ValueToFloat() + right.ValueToFloat());
                            case TokenType.MINUS:
                                return new Value(VariableType.FLOAT, left.ValueToFloat() - right.ValueToFloat());
                            case TokenType.MULTIPLY:
                                return new Value(VariableType.FLOAT, left.ValueToFloat() * right.ValueToFloat());
                            case TokenType.DIVIDE:
                                return new Value(VariableType.FLOAT, left.ValueToFloat() / right.ValueToFloat());
                            default:
                                break;
                        }
                    }
                    else if (right.VariableType == VariableType.STRING && CanBe.Converted(typeof(string), left.ObjectValue))
                    {
                        switch (op)
                        {
                            case TokenType.PLUS:
                                return new Value(VariableType.STRING, left.ValueToString() + right.ValueToString());
                        }
                    }
                    break;

                case VariableType.CUSTOM:
                    Logger.Error("Cannot apply operator on custom object for now.");
                    break;
            }

            Logger.Error($"Cant apply this operator {op} on {left.VariableType} and {right.VariableType}");
            return null;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Set value
        /// </summary>
        /// <param name="newValue">New value</param>
        public void SetValue(Value newValue, AssignType op)
        {
            switch (newValue.VariableType)
            {

                #region STRING

                case VariableType.STRING:
                    if (VariableType == VariableType.STRING)
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToString();
                                break;
                            case AssignType.PLUSEQUAL:
                                ObjectValue = ValueToString() + newValue.ValueToString();
                                break;
                            default:
                                Logger.Error(LocaString.Get("UnhandledError") + " : Value.cs #1");
                                break;
                        }
                    else if (VariableType == VariableType.BOOL && CanBe.Converted(typeof(bool), newValue.ValueToString()))
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToBool();
                                break;
                            default:
                                Logger.Error(LocaString.Get("UnhandledError") + " : Value.cs #2");
                                break;
                        }
                    else if (VariableType == VariableType.FLOAT && CanBe.Converted(typeof(float), newValue.ValueToString()))
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToFloat();
                                break;
                            case AssignType.PLUSEQUAL:
                                ObjectValue = ValueToFloat() + newValue.ValueToFloat();
                                break;
                            case AssignType.MINUSEQUAL:
                                ObjectValue = ValueToFloat() - newValue.ValueToFloat();
                                break;
                            case AssignType.MULTIPLYEQUAL:
                                ObjectValue = ValueToFloat() * newValue.ValueToFloat();
                                break;
                            case AssignType.DIVIDEEQUAL:
                                ObjectValue = ValueToFloat() / newValue.ValueToFloat();
                                break;
                            case AssignType.PLUSPLUS:
                                ObjectValue = ValueToFloat() + 1;
                                break;
                            case AssignType.MINUSMINUS:
                                ObjectValue = ValueToFloat() - 1;
                                break;
                        }
                    else if (VariableType == VariableType.INT && CanBe.Converted(typeof(int), newValue.ValueToString()))
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToInt();
                                break;
                            case AssignType.PLUSEQUAL:
                                ObjectValue = ValueToInt() + newValue.ValueToInt();
                                break;
                            case AssignType.MINUSEQUAL:
                                ObjectValue = ValueToInt() - newValue.ValueToInt();
                                break;
                            case AssignType.MULTIPLYEQUAL:
                                ObjectValue = ValueToInt() * newValue.ValueToInt();
                                break;
                            case AssignType.DIVIDEEQUAL:
                                ObjectValue = ValueToInt() / newValue.ValueToInt();
                                break;
                            case AssignType.PLUSPLUS:
                                ObjectValue = ValueToInt() + 1;
                                break;
                            case AssignType.MINUSMINUS:
                                ObjectValue = ValueToInt() - 1;
                                break;
                        }
                    else
                        Logger.Error($"You cant assign a string value to a {VariableType} variable.");
                    break;

                #endregion

                #region INT

                case VariableType.INT:
                    if (VariableType == VariableType.INT)
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToInt();
                                break;
                            case AssignType.PLUSEQUAL:
                                ObjectValue = ValueToInt() + newValue.ValueToInt();
                                break;
                            case AssignType.MINUSEQUAL:
                                ObjectValue = ValueToInt() - newValue.ValueToInt();
                                break;
                            case AssignType.MULTIPLYEQUAL:
                                ObjectValue = ValueToInt() * newValue.ValueToInt();
                                break;
                            case AssignType.DIVIDEEQUAL:
                                ObjectValue = ValueToInt() / newValue.ValueToInt();
                                break;
                            case AssignType.PLUSPLUS:
                                ObjectValue = ValueToInt() + 1;
                                break;
                            case AssignType.MINUSMINUS:
                                ObjectValue = ValueToInt() - 1;
                                break;
                        }
                    else if (VariableType == VariableType.FLOAT)
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToFloat();
                                break;
                            case AssignType.PLUSEQUAL:
                                ObjectValue = ValueToFloat() + newValue.ValueToFloat();
                                break;
                            case AssignType.MINUSEQUAL:
                                ObjectValue = ValueToFloat() - newValue.ValueToFloat();
                                break;
                            case AssignType.MULTIPLYEQUAL:
                                ObjectValue = ValueToFloat() * newValue.ValueToFloat();
                                break;
                            case AssignType.DIVIDEEQUAL:
                                ObjectValue = ValueToFloat() / newValue.ValueToFloat();
                                break;
                            case AssignType.PLUSPLUS:
                                ObjectValue = ValueToFloat() + 1;
                                break;
                            case AssignType.MINUSMINUS:
                                ObjectValue = ValueToFloat() - 1;
                                break;
                        }
                    else if (VariableType == VariableType.BOOL && CanBe.Converted(typeof(bool), newValue.ValueToInt()))
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToBool();
                                break;
                            default:
                                Logger.Error(LocaString.Get("UnhandledError") + " : Value.cs #3");
                                break;
                        }
                    else if (VariableType == VariableType.STRING && CanBe.Converted(typeof(string), newValue.ValueToInt()))
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToString();
                                break;
                            case AssignType.PLUSEQUAL:
                                ObjectValue = ValueToString() + newValue.ValueToString();
                                break;
                            default:
                                Logger.Error(LocaString.Get("UnhandledError") + " : Value.cs #4");
                                break;
                        }
                    else
                        Logger.Error($"You cant assign a int value to a {VariableType} variable.");
                    break;

                #endregion

                #region FLOAT

                case VariableType.FLOAT:
                    if (VariableType == VariableType.FLOAT)
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToFloat();
                                break;
                            case AssignType.PLUSEQUAL:
                                ObjectValue = ValueToFloat() + newValue.ValueToFloat();
                                break;
                            case AssignType.MINUSEQUAL:
                                ObjectValue = ValueToFloat() - newValue.ValueToFloat();
                                break;
                            case AssignType.MULTIPLYEQUAL:
                                ObjectValue = ValueToFloat() * newValue.ValueToFloat();
                                break;
                            case AssignType.DIVIDEEQUAL:
                                ObjectValue = ValueToFloat() / newValue.ValueToFloat();
                                break;
                            case AssignType.PLUSPLUS:
                                ObjectValue = ValueToFloat() + 1;
                                break;
                            case AssignType.MINUSMINUS:
                                ObjectValue = ValueToFloat() + 1;
                                break;
                        }
                    else if (VariableType == VariableType.INT)
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToInt();
                                break;
                            case AssignType.PLUSEQUAL:
                                ObjectValue = ValueToInt() + newValue.ValueToInt();
                                break;
                            case AssignType.MINUSEQUAL:
                                ObjectValue = ValueToInt() - newValue.ValueToInt();
                                break;
                            case AssignType.MULTIPLYEQUAL:
                                ObjectValue = ValueToInt() * newValue.ValueToInt();
                                break;
                            case AssignType.DIVIDEEQUAL:
                                ObjectValue = ValueToInt() / newValue.ValueToInt();
                                break;
                            case AssignType.PLUSPLUS:
                                ObjectValue = ValueToInt() + 1;
                                break;
                            case AssignType.MINUSMINUS:
                                ObjectValue = ValueToInt() + 1;
                                break;
                        }
                    else if (VariableType == VariableType.BOOL && CanBe.Converted(typeof(bool), newValue.ValueToFloat()))
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToBool();
                                break;
                            default:
                                Logger.Error(LocaString.Get("UnhandledError") + " : Value.cs #5");
                                break;
                        }
                    else if (VariableType == VariableType.STRING && CanBe.Converted(typeof(string), newValue.ValueToFloat()))
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToString();
                                break;
                            case AssignType.PLUSEQUAL:
                                ObjectValue = ValueToString() + newValue.ValueToString();
                                break;
                            default:
                                Logger.Error(LocaString.Get("UnhandledError") + " : Value.cs #6");
                                break;
                        }
                    else
                        Logger.Error($"You cant assign a float value to a {VariableType} variable.");
                    break;

                #endregion

                #region BOOL

                case VariableType.BOOL:
                    if (VariableType == VariableType.BOOL)
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToBool();
                                break;
                            default:
                                Logger.Error(LocaString.Get("UnhandledError") + " : Value.cs #7");
                                break;
                        }
                    else if (VariableType == VariableType.FLOAT && CanBe.Converted(typeof(float), newValue.ValueToBool()))
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToFloat();
                                break;
                            default:
                                Logger.Error(LocaString.Get("UnhandledError") + " : Value.cs #8");
                                break;
                        }
                    else if (VariableType == VariableType.INT && CanBe.Converted(typeof(float), newValue.ValueToInt()))
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToInt();
                                break;
                            default:
                                Logger.Error(LocaString.Get("UnhandledError") + " : Value.cs #9");
                                break;
                        }
                    else if (VariableType == VariableType.STRING && CanBe.Converted(typeof(float), newValue.ValueToString()))
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                ObjectValue = newValue.ValueToString();
                                break;
                            default:
                                Logger.Error(LocaString.Get("UnhandledError") + " : Value.cs #10");
                                break;
                        }
                    else
                        Logger.Error($"You cant assign a bool value to a {VariableType} variable.");
                    break;

                #endregion

                #region CUSTOM

                case VariableType.CUSTOM:
                    if (VariableType == VariableType.CUSTOM)
                        switch (op)
                        {
                            case AssignType.EQUAL:
                                if (newValue.CustomType == CustomType)
                                    ObjectValue = newValue.ObjectValue;
                                else
                                    Logger.Error(LocaString.Get("WrongCustomObjectType"));
                                break;
                            default:
                                Logger.Error(LocaString.Get("UnhandledError") + " : Value.cs #11");
                                break;
                        }
                    else
                        Logger.Error($"You cant assign a custom value to a {VariableType} variable.");
                    break;

                    #endregion

            }

            CheckType();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Invoke the method
        /// </summary>
        /// <param name="values">Params values</param>
        /// <returns>Return value</returns>
        public Value Invoke(params Value[] values)
        {
            if (values.Length != Parameters.Length)
                Logger.Error("Wrong number of argument");

            for (int i = 0; i < values.Length; i++)
            {
                Parameter p = Parameters[i];
                Value v = values[i];
                if (p.VariableType != v.VariableType)
                {
                    if (!CanBe.Converted(p.VariableType.GetNativeType(), v.ObjectValue))
                        Logger.Error("Wrong type of argument");
                    else
                        LocalVariables.Add(new Variable(this, p.Name, p.VariableType, Convert.ChangeType(v.ObjectValue, p.VariableType.GetNativeType())));
                }
                else
                    LocalVariables.Add(new Variable(this, p.Name, p.VariableType, v.ObjectValue));
            }

            foreach (var b in SubBlocks)
            {
                if (b is Statement)
                    b.Execute();
                if (ReturnValue != null)
                    break;
            }

            if (ReturnValue == null && ReturnType != VariableType.VOID)
                Logger.Error("No returned value");

            LocalVariables.Clear();

            Value localReturnValue = ReturnValue;
            ReturnValue = null;
            return localReturnValue;
        }