示例#1
0
        public override void GenerateIntermediateCode()
        {
            MainVariable.AlternativeName = "temp_" + (++Counters.temps).ToString(); //присваиваем временной переменной имя
            IntermediateCodeList.addVar(MainVariable);                              //временную переменную - в список переменных
            FirstOperand.GenerateIntermediateCode();
            //вот тут самое веселье
            if (IsUnary)
            {
                //из унарных получается только унарный минус и отрицание
                switch (Value)
                {
                case ("!"):
                {
                    IntermediateCodeList.push(new NegativeInterNode(FirstOperand.MainVariable, MainVariable));
                    break;
                }

                case ("-"):
                {
                    IntermediateCodeList.push(new UnaryMinusInterNode(FirstOperand.MainVariable, MainVariable));
                    break;
                }
                }
            }
            else
            {
                SecondOperand.GenerateIntermediateCode();
                switch (Value)
                {
                case ("+"):
                {
                    IntermediateCodeList.push(new AddInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable));
                    break;
                }

                case ("-"):
                {
                    IntermediateCodeList.push(new SubInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable));
                    break;
                }

                case ("*"):
                {
                    IntermediateCodeList.push(new MulInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable));
                    break;
                }

                case ("/"):
                {
                    IntermediateCodeList.push(new DivInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable));
                    break;
                }

                case ("%"):
                {
                    IntermediateCodeList.push(new ModInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable));
                    break;
                }

                case ("&&"):
                {
                    IntermediateCodeList.push(new AndInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable));
                    break;
                }

                case ("||"):
                {
                    IntermediateCodeList.push(new OrInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable));
                    break;
                }

                case ("&"):
                {
                    IntermediateCodeList.push(new BitAndInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable));
                    break;
                }

                case ("|"):
                {
                    IntermediateCodeList.push(new BitOrInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable));
                    break;
                }

                case ("^"):
                {
                    IntermediateCodeList.push(new BitXorInterNode(FirstOperand.MainVariable, SecondOperand.MainVariable, MainVariable));
                    break;
                }

                //с операторами сравнения посложнее
                case ("=="):
                {
                    //сравниваем два операнда
                    IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable));
                    //если не равно, то переходим дальше
                    IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "jne"));
                    //если равно, то идём дальше
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable));
                    //присвоили и уходим
                    IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp"));
                    //если не равно, идём сюда
                    IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString()));
                    //присваиваем false
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable));
                    //и на выход
                    IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString()));
                    break;
                }

                //с другими операторами сравнения аналогично, только условие другое
                case ("!="):
                {
                    //сравниваем два операнда
                    IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable));
                    //если равно, то переходим дальше
                    IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "je"));
                    //иначе, если всё хорошо, то присваиваем true
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable));
                    //присвоили и уходим
                    IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp"));
                    //если не равно, идём сюда
                    IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString()));
                    //присваиваем false
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable));
                    //и на выход
                    IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString()));
                    break;
                }

                case (">="):
                {
                    //сравниваем два операнда
                    IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable));
                    //если меньше, то переходим дальше
                    IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "jl"));
                    //иначе, если всё хорошо, то присваиваем true
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable));
                    //присвоили и уходим
                    IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp"));
                    //если не равно, идём сюда
                    IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString()));
                    //присваиваем false
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable));
                    //и на выход
                    IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString()));
                    break;
                }

                case ("<="):
                {
                    //сравниваем два операнда
                    IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable));
                    //если больше, то переходим дальше
                    IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "jg"));
                    //иначе, если всё хорошо, то присваиваем true
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable));
                    //присвоили и уходим
                    IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp"));
                    //если не равно, идём сюда
                    IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString()));
                    //присваиваем false
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable));
                    //и на выход
                    IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString()));
                    break;
                }

                case ("<"):
                {
                    //сравниваем два операнда
                    IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable));
                    //если больше, то переходим дальше
                    IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "jge"));
                    //иначе, если всё хорошо, то присваиваем true
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable));
                    //присвоили и уходим
                    IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp"));
                    //если не равно, идём сюда
                    IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString()));
                    //присваиваем false
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable));
                    //и на выход
                    IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString()));
                    break;
                }

                case (">"):
                {
                    //сравниваем два операнда
                    IntermediateCodeList.push(new CmpNode(FirstOperand.MainVariable, SecondOperand.MainVariable));
                    //если больше, то переходим дальше
                    IntermediateCodeList.push(new GoToLabel("comp_label_" + (++Counters.comparsions).ToString(), "jle"));
                    //иначе, если всё хорошо, то присваиваем true
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "true", 0).MainVariable, MainVariable));
                    //присвоили и уходим
                    IntermediateCodeList.push(new GoToLabel("exit_comp_label_" + (Counters.comparsions).ToString(), "jmp"));
                    //если не равно, идём сюда
                    IntermediateCodeList.push(new PutLabel("comp_label_" + (Counters.comparsions).ToString()));
                    //присваиваем false
                    IntermediateCodeList.push(new AssignmentInterNode(new ConstantNode("bool", "false", 0).MainVariable, MainVariable));
                    //и на выход
                    IntermediateCodeList.push(new PutLabel("exit_comp_label_" + (Counters.comparsions).ToString()));
                    break;
                }
                }
            }
        }