Example #1
0
        private static void CreateInParallelVariable(syntax_tree_visitor syntax_tree_visitor, out string VarName)
        {
            //сохраняем контекст
            ContextInfo contextInfo = new ContextInfo(syntax_tree_visitor);

            VarName = "$InParallelSection";

            try
            {
                //создание и конвертирование переменной                
                SyntaxTree.var_def_statement vds = new PascalABCCompiler.SyntaxTree.var_def_statement();
                SyntaxTree.variable_definitions var_def = new PascalABCCompiler.SyntaxTree.variable_definitions(vds, null);
                SyntaxTree.ident_list idl = new PascalABCCompiler.SyntaxTree.ident_list();
                vds.vars = idl;
                idl.Add(new SyntaxTree.ident(VarName));
                SyntaxTree.named_type_reference ntr = new PascalABCCompiler.SyntaxTree.named_type_reference();
                vds.vars_type = ntr;
                ntr.names.Add(new PascalABCCompiler.SyntaxTree.ident("boolean"));
                vds.inital_value = new PascalABCCompiler.SyntaxTree.ident("false");
                syntax_tree_visitor.visit(var_def);
            }
            finally
            {
                //восстанавливаем контекст
                contextInfo.RestoreContext(syntax_tree_visitor);
            }
            InParallelSectionCreated = true;
        }
Example #2
0
        private static SyntaxTree.procedure_definition CreateMethod(string MethodName, SyntaxTree.statement Body, string LoopVariableName, SyntaxTree.class_members ClassMember, VarInfoContainer Vars)
        {
            //  генерация метода
            SyntaxTree.procedure_definition ProcDef = new PascalABCCompiler.SyntaxTree.procedure_definition();
            //ClassMember.members.Add(ProcDef);
            SyntaxTree.procedure_header ProcHead = new PascalABCCompiler.SyntaxTree.procedure_header();
            ProcDef.proc_header = ProcHead;
            ProcHead.name = new PascalABCCompiler.SyntaxTree.method_name(null, null, new PascalABCCompiler.SyntaxTree.ident(MethodName), null);
            if (LoopVariableName != "")
            {
                //  параметр, счетчик цикла
                string ParamType = "integer";
                SyntaxTree.formal_parameters FormalParams = new PascalABCCompiler.SyntaxTree.formal_parameters();
                ProcHead.parameters = FormalParams;
                SyntaxTree.typed_parameters TypedParams = new PascalABCCompiler.SyntaxTree.typed_parameters();
                FormalParams.params_list.Add(TypedParams);
                SyntaxTree.ident_list idl = new PascalABCCompiler.SyntaxTree.ident_list();
                TypedParams.idents = idl;
                idl.Add(new SyntaxTree.ident(LoopVariableName));
                SyntaxTree.named_type_reference ntr = new PascalABCCompiler.SyntaxTree.named_type_reference();
                TypedParams.vars_type = ntr;
                ntr.Add(new SyntaxTree.ident(ParamType));
            }

            SyntaxTree.block ProcBlock = new PascalABCCompiler.SyntaxTree.block();
            ProcDef.proc_body = ProcBlock;
            ProcBlock.defs = new PascalABCCompiler.SyntaxTree.declarations();
            if (Vars.Constants.Count > 0)
            {
                SyntaxTree.consts_definitions_list cdl = new PascalABCCompiler.SyntaxTree.consts_definitions_list();
                ProcBlock.defs.defs.Add(cdl);
                //  константы - в методе
                for (int i = 0; i < Vars.Constants.Count; ++i)
                    cdl.Add(CreateClassMember(Vars.Constants[i], "") as SyntaxTree.typed_const_definition);
            }
            if ((Vars.ReductionVariables.Count > 0) || (Vars.PrivateVariables.Count > 0))
            {
                //  переменные редукции - в методе тоже, но без префикса
                SyntaxTree.variable_definitions vds = new PascalABCCompiler.SyntaxTree.variable_definitions();
                ProcBlock.defs.defs.Add(vds);
                for (int i = 0; i < Vars.ReductionVariables.Count; ++i)
                    vds.Add(CreateClassMember(Vars.ReductionVariables[i], "") as SyntaxTree.var_def_statement);
                //  и приватные переменные
                for (int i = 0; i < Vars.PrivateVariables.Count; ++i)
                    vds.Add(CreateClassMember(Vars.PrivateVariables[i], "") as SyntaxTree.var_def_statement);
            }

            if (Body is SyntaxTree.statement_list)
                ProcBlock.program_code = Body as SyntaxTree.statement_list;
            else
            {
                SyntaxTree.statement_list stl = new PascalABCCompiler.SyntaxTree.statement_list();
                stl.subnodes.Add(Body);
                ProcBlock.program_code = stl;
            }

            //присваивания для переменных редукции
            if (Vars.ReductionVariables.Count > 0)
            {
                SyntaxTree.statement_list LoopBodyInit = new PascalABCCompiler.SyntaxTree.statement_list();
                SyntaxTree.statement_list LoopBodyFinal = new PascalABCCompiler.SyntaxTree.statement_list();

                for (int i = 0; i < Vars.ReductionVariables.Count; ++i)
                {
                    //присваивание начального значения
                    SyntaxTree.assign Assign = new PascalABCCompiler.SyntaxTree.assign();
                    Assign.operator_type = PascalABCCompiler.SyntaxTree.Operators.Assignment;
                    Assign.to = new SyntaxTree.ident(Vars.ReductionVariables[i].name);
                    bool isBool = Vars.ReductionVariables[i].type.name.ToLower() == "boolean";
                    switch (Vars.ReductionActions[i])
                    {
                        case ReductionOperations.and:
                            {
                                if (isBool)
                                    Assign.from = new SyntaxTree.bool_const(true);
                                else
                                {
                                    //отрицание нуля
                                    Assign.from = new SyntaxTree.int32_const(0);
                                    LoopBodyInit.subnodes.Add(Assign);
                                    Assign = new PascalABCCompiler.SyntaxTree.assign();
                                    Assign.operator_type = PascalABCCompiler.SyntaxTree.Operators.Assignment;
                                    Assign.to = new SyntaxTree.ident(Vars.ReductionVariables[i].name);
                                    SyntaxTree.un_expr ue = new PascalABCCompiler.SyntaxTree.un_expr();
                                    ue.operation_type = PascalABCCompiler.SyntaxTree.Operators.LogicalNOT;
                                    ue.subnode = new SyntaxTree.ident(Vars.ReductionVariables[i].name);
                                    Assign.from = ue;
                                }
                                break;
                            }
                        case ReductionOperations.or:
                            if (isBool)
                                Assign.from = new SyntaxTree.bool_const(false);
                            else
                            {
                                Assign.from = new SyntaxTree.int32_const(0);
                            }
                            break;
                        case ReductionOperations.xor:  //
                        case ReductionOperations.plus: //см следующую ветку
                        case ReductionOperations.minus: Assign.from = new SyntaxTree.int32_const(0); break;
                        case ReductionOperations.mult: Assign.from = new SyntaxTree.int32_const(1); break;
                    }
                    LoopBodyInit.Add(Assign);

                    //присваивание после итерации
                    Assign = new PascalABCCompiler.SyntaxTree.assign();
                    Assign.operator_type = PascalABCCompiler.SyntaxTree.Operators.Assignment;
                    Assign.to = new SyntaxTree.ident("$" + Vars.ReductionVariables[i].name);
                    SyntaxTree.bin_expr From = new PascalABCCompiler.SyntaxTree.bin_expr();
                    From.left = new SyntaxTree.ident("$" + Vars.ReductionVariables[i].name);
                    From.right = new SyntaxTree.ident(Vars.ReductionVariables[i].name);
                    Assign.from = From;
                    switch (Vars.ReductionActions[i])
                    {
                        case ReductionOperations.and: From.operation_type = PascalABCCompiler.SyntaxTree.Operators.LogicalAND; break;
                        case ReductionOperations.or: From.operation_type = PascalABCCompiler.SyntaxTree.Operators.LogicalOR; break;
                        case ReductionOperations.xor: From.operation_type = PascalABCCompiler.SyntaxTree.Operators.BitwiseXOR; break;
                        case ReductionOperations.plus: From.operation_type = PascalABCCompiler.SyntaxTree.Operators.Plus; break;
                        case ReductionOperations.minus: From.operation_type = PascalABCCompiler.SyntaxTree.Operators.Minus; break;
                        case ReductionOperations.mult: From.operation_type = PascalABCCompiler.SyntaxTree.Operators.Multiplication; break;
                    }
                    LoopBodyFinal.Add(Assign);
                }

                //создаем обьект для блокировки в классе
                SyntaxTree.var_def_statement Lvds = new PascalABCCompiler.SyntaxTree.var_def_statement();
                SyntaxTree.ident_list Lidl = new PascalABCCompiler.SyntaxTree.ident_list();
                Lvds.vars = Lidl;
                Lidl.Add(new SyntaxTree.ident("$ReductionLock"));
                SyntaxTree.named_type_reference Lntr = new PascalABCCompiler.SyntaxTree.named_type_reference();
                Lvds.vars_type = Lntr;
                Lntr.Add(new SyntaxTree.ident("object"));
                SyntaxTree.new_expr Lne = new PascalABCCompiler.SyntaxTree.new_expr();
                Lvds.inital_value = Lne;
                Lne.type = Lntr;
                ClassMember.members.Add(Lvds);

                //создаем lock Statement на обьекте с присваиваниями в конце итерации
                SyntaxTree.lock_stmt reductionLock = new PascalABCCompiler.SyntaxTree.lock_stmt();
                reductionLock.lock_object = new SyntaxTree.ident("$ReductionLock");
                reductionLock.stmt = LoopBodyFinal;

                //собираем все вместе и присваиваем это телу процедуры
                LoopBodyInit.subnodes.AddRange(ProcBlock.program_code.subnodes);
                LoopBodyInit.subnodes.Add(reductionLock);
                ProcBlock.program_code = LoopBodyInit;
            }
            return ProcDef;
        }