Esempio n. 1
0
        private bool compilerOperandsAndOperations()
        {
            bool bResult = true;

            bResult &= getOperandsAndOperations(processedString);
            if (bResult && Operands.Count > 0 && Operations.Count > 0)
            {
                if (Operands.Count == Operations.Count + 1)
                {
                    #region last one operation
                    if (Operations.Count == 1)
                    {
                        switch (Operations[0].Text)
                        {
                        case "&":
                            method = getBasicOperationMethodInfo("AND", 2);
                            break;

                        case "|":
                            method = getBasicOperationMethodInfo("OR", 2);
                            break;

                        case "*":
                            method = getBasicOperationMethodInfo("MUL", 2);
                            break;

                        case "/":
                            method = getBasicOperationMethodInfo("DIV", 2);
                            break;

                        case "\\%":
                            method = getBasicOperationMethodInfo("MOD", 2);
                            break;

                        case "+":
                            method = getBasicOperationMethodInfo("ADD", 2);
                            break;

                        case "-":
                            method = getBasicOperationMethodInfo("SUB", 2);
                            break;

                        case "==":
                            method = getBasicOperationMethodInfo("EQUALS", 2);
                            break;

                        case "!=":
                            method = getBasicOperationMethodInfo("NOT_EQUALS", 2);
                            break;

                        case ">=":
                            method = getBasicOperationMethodInfo("GREATER_EQUAL_THAN", 2);
                            break;

                        case "<=":
                            method = getBasicOperationMethodInfo("LESS_EQUAL_THAN", 2);
                            break;

                        case "<":
                            method = getBasicOperationMethodInfo("LESS_THAN", 2);
                            break;

                        case ">":
                            method = getBasicOperationMethodInfo("GREATER_THAN", 2);
                            break;

                        default:
                            method = null;
                            break;
                        }
                        if (method == null)
                        {
                            bResult = false;
                            addRuntimeErrorMessages("Unkonw operatsion \"" + Operations[0].Text + "\"");
                        }
                        parameters.Add(Operands[0]);
                        parameters.Add(Operands[1]);
                    }
                    #endregion last one operation
                    else
                    {
                        int iNextOperationIndex  = -1;
                        int iFirstPriorityIndex  = -1;
                        int iSecondPriorityIndex = -1;
                        int iThirdPriorityIndex  = -1;
                        int iLastPriorityIndex   = -1;
                        for (int i = 0; i < Operations.Count; i++)
                        {
                            switch (Operations[i].Text)
                            {
                            case "&":
                            case "|":
                                if (iFirstPriorityIndex < 0)
                                {
                                    iFirstPriorityIndex = i;
                                }
                                break;

                            case "*":
                            case "/":
                            case "\\%":
                                if (iSecondPriorityIndex < 0)
                                {
                                    iSecondPriorityIndex = i;
                                }
                                break;

                            case "+":
                            case "-":
                                if (iThirdPriorityIndex < 0)
                                {
                                    iThirdPriorityIndex = i;
                                }
                                break;

                            case "==":
                            case ">=":
                            case "<=":
                            case "!=":
                            case ">":
                            case "<":
                                if (iLastPriorityIndex < 0)
                                {
                                    iLastPriorityIndex = i;
                                }
                                break;
                            }
                            if (iFirstPriorityIndex >= 0)
                            {
                                break;
                            }
                        }
                        if (iFirstPriorityIndex >= 0)
                        {
                            iNextOperationIndex = iFirstPriorityIndex;
                        }
                        else if (iSecondPriorityIndex >= 0)
                        {
                            iNextOperationIndex = iSecondPriorityIndex;
                        }
                        else if (iThirdPriorityIndex >= 0)
                        {
                            iNextOperationIndex = iThirdPriorityIndex;
                        }
                        else
                        {
                            iNextOperationIndex = iLastPriorityIndex;
                        }
                        if (iNextOperationIndex >= 0)
                        {
                            String     tempCmd      = Operands[iNextOperationIndex].Text + Operations[iNextOperationIndex].Text + Operands[iNextOperationIndex + 1].Text;
                            String     tempVariable = scriptline.Script.GetNewTempVariableName;
                            clsCommand subCmd       = new clsCommand(scriptline, tempCmd, tempVariable);
                            subCommands.Add(subCmd);
                            processedString = processedString.Replace(tempCmd, "%" + tempVariable + "%");
                            bResult        &= compilerOperandsAndOperations();
                        }
                        else
                        {
                            bResult = false;
                            addRuntimeErrorMessages("Invaliable operation express");
                        }
                    }
                }
                else
                {
                    bResult = false;
                    addRuntimeErrorMessages("Invaliable operation express");
                }
            }
            else if (Operations.Count == 0 && Operands.Count == 1)// Just an assignment
            {
                parameters.Add(Operands[0].Text);
            }
            return(bResult);
        }
Esempio n. 2
0
        public bool Compiler()
        {
            bool isOK = false;
            bool isLeftStatementOK  = true;
            bool isRightStatementOK = true;

            String saveResultToVariableName = "";

            spilteString();
            #region Left Statement
            if (leftStatement != null && leftStatement.Length > 0)
            {
                String left = leftStatement.Trim();
                if (left.StartsWith("VAR "))
                {
                    String[] subLeft = left.Split(' ');
                    if (subLeft.Length == 2)
                    {
                        try
                        {
                            saveResultToVariableName = subLeft[1];
                            bool bTemp = script.AddVariable(saveResultToVariableName, "");
                            if (!bTemp)
                            {
                                addRuntimeErrorMessages("Fail to declare variable: \"" + subLeft[1] + "\"");
                            }
                            isLeftStatementOK &= bTemp;
                        }
                        catch
                        {
                            isLeftStatementOK = false;
                            addRuntimeErrorMessages("Fail to declare variable: \"" + subLeft[1] + "\"");
                        }
                    }
                    else
                    {
                        isLeftStatementOK = false;
                        addRuntimeErrorMessages("Fail to declare variable: \"" + left + "\"");
                    }
                }
                else
                {
                    try
                    {
                        saveResultToVariableName = left;
                        object obj = script.FindVariable(saveResultToVariableName);
                        if (obj == null)
                        {
                            isLeftStatementOK = false;
                            addRuntimeErrorMessages("Variable: \"" + left + "\" doesn't exist.");
                        }
                    }
                    catch (Exception ex)
                    {
                        isLeftStatementOK = false;
                        addRuntimeErrorMessages("Fail to find the variable: \"" + left + "\" ");
                    }
                }
            }
            #endregion Left Statement
            #region Right Statement
            Command             = new clsCommand(this, rightStatement.Trim(), saveResultToVariableName);
            isRightStatementOK &= Command.Compiler();
            isOK = isLeftStatementOK & isRightStatementOK;
            #endregion Right Statement
            return(isOK);
        }
Esempio n. 3
0
        public bool Compiler()
        {
            bool bResult = true;

            commandRunner = null;
            MethodInfo method = null;

            processedString = originalString;
            bResult        &= findSpecialSymbolsPairs(processedString);
            bResult        &= checkVariablesInPercentSignPairExist();
            if (bResult)
            {
                #region Functions
                if (parenthesisPairs.Count > 0 && parenthesisPairs.Last().LeftIndex > 0 &&
                    (parenthesisPairs.Last().RightIndex == processedString.Length - 1) &&
                    (processedString.Substring(0, parenthesisPairs.Last().LeftIndex).Split(clsOperationString.OperationSymbols, StringSplitOptions.None).Count() == 1)
                    )
                {
                    #region parse command header
                    String cmdTemp = processedString.Substring(0, parenthesisPairs.Last().LeftIndex).Trim();
                    String deviceName = "", componentName = "", methodName = "";
                    bResult &= getCommandHeader(cmdTemp, ref deviceName, ref componentName, ref methodName);
                    if (scriptline.Script.Package.Devices.ContainsKey(deviceName))
                    {
                        if (deviceName.StartsWith(clsPackage.KEY_Device))
                        {
                            IDevice          aDevice  = (IDevice)scriptline.Script.Package.Devices[deviceName];
                            IDeviceComponent aDevComp = null;
                            if (componentName != null && componentName.Length > 0)
                            {
                                aDevComp = (IDeviceComponent)aDevice.DeviceComponents[componentName];
                            }
                            if (aDevComp != null)
                            {
                                commandRunner = aDevComp;
                            }
                            else
                            {
                                bResult = false;
                            }
                        }
                        else  //External components
                        {
                        }
                    }
                    else if (deviceName.Equals(clsPackage.KEY_BasicOperation))
                    {
                        commandRunner = new clsBasicOperations();
                    }
                    else
                    {
                        bResult = false;
                        scriptline.Script.RuntimeErrorMessages.Add(new clsRuntimeErrorMessage(scriptline.LineNumber, deviceName + " does not exist."));
                    }
                    #endregion parse command header

                    #region parse parameters of command
                    int    start      = parenthesisPairs.Last().LeftIndex;
                    int    length     = parenthesisPairs.Last().RightIndex - start;
                    String paramsTemp = processedString.Substring(start, length).TrimStart(new char[] { '(' }).TrimEnd(new char[] { ')' });
                    bResult &= getParametersOfMethod(start, paramsTemp);
                    #endregion parse parameters of command

                    if (bResult)
                    {
                        method = ((IDeviceComponent)commandRunner).GetMethod(methodName, parameters.ToArray());
                        if (method == null)
                        {
                            bResult = false;
                            scriptline.Script.RuntimeErrorMessages.Add(new clsRuntimeErrorMessage(scriptline.LineNumber, methodName + "(params[" + parameters.Count + "]), does not exist."));
                        }
                    }
                }
                #endregion Functions
                #region Operations
                #region Opertaions in parenthesisPairs is the first priority
                else if (parenthesisPairs.Count > 0)
                {
                    for (int i = parenthesisPairs.Count - 1; i >= 0; i--)
                    {
                        if (parenthesisPairs[i].Text.Length > 0)
                        {
                            String     strTempVariable = scriptline.Script.GetNewTempVariableName;
                            clsCommand subOperation    = new clsCommand(scriptline, parenthesisPairs[i].Text, strTempVariable);
                            subCommands.Add(subOperation);
                            processedString = replaceSubString(processedString, "%" + strTempVariable + "%", parenthesisPairs[i].LeftIndex, parenthesisPairs[i].RightIndex);
                        }
                    }
                }
                #endregion Opertaions in parenthesisPairs is the first priority
                bResult &= compilerOperandsAndOperations();
                if (bResult)
                {
                    commandRunner = scriptline.Script.Package.BasicOperations;
                }
                #endregion Operations

                //#region Assignment

                //#endregion Assignment
            }
            foreach (clsCommand subCommand in subCommands)
            {
                bResult &= subCommand.Compiler();
            }
            return(bResult);
        }