예제 #1
0
        public bool GenerateXGRSCode(string xgrsName, String package, String xgrsStr,
                                     String[] paramNames, GrGenType[] paramTypes,
                                     String[] defToBeYieldedToNames, GrGenType[] defToBeYieldedToTypes,
                                     SourceBuilder source, int lineNr)
        {
            Dictionary <String, String> varDecls = new Dictionary <String, String>();

            for (int i = 0; i < paramNames.Length; ++i)
            {
                varDecls.Add(paramNames[i], TypesHelper.DotNetTypeToXgrsType(paramTypes[i]));
            }
            for (int i = 0; i < defToBeYieldedToNames.Length; ++i)
            {
                varDecls.Add(defToBeYieldedToNames[i], TypesHelper.DotNetTypeToXgrsType(defToBeYieldedToTypes[i]));
            }

            Sequence seq;

            try
            {
                SequenceParserEnvironmentCompiled parserEnv = new SequenceParserEnvironmentCompiled(package, actionNames, model);
                List <string> warnings = new List <string>();
                seq = SequenceParser.ParseSequence(xgrsStr, parserEnv, varDecls, warnings);
                foreach (string warning in warnings)
                {
                    Console.Error.WriteLine("The exec statement \"" + xgrsStr
                                            + "\" given on line " + lineNr + " reported back:\n" + warning);
                }
                seq.Check(env);
                seq.SetNeedForProfilingRecursive(emitProfiling);
            }
            catch (ParseException ex)
            {
                Console.Error.WriteLine("The exec statement \"" + xgrsStr
                                        + "\" given on line " + lineNr + " caused the following error:\n" + ex.Message);
                return(false);
            }
            catch (SequenceParserException ex)
            {
                Console.Error.WriteLine("The exec statement \"" + xgrsStr
                                        + "\" given on line " + lineNr + " caused the following error:\n");
                HandleSequenceParserException(ex);
                return(false);
            }

            source.Append("\n");
            source.AppendFront("public static bool ApplyXGRS_" + xgrsName + "(GRGEN_LGSP.LGSPGraphProcessingEnvironment procEnv");
            for (int i = 0; i < paramNames.Length; ++i)
            {
                source.Append(", " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(paramTypes[i]), model) + " var_");
                source.Append(paramNames[i]);
            }
            for (int i = 0; i < defToBeYieldedToTypes.Length; ++i)
            {
                source.Append(", ref " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(defToBeYieldedToTypes[i]), model) + " var_");
                source.Append(defToBeYieldedToNames[i]);
            }
            source.Append(")\n");
            source.AppendFront("{\n");
            source.Indent();

            source.AppendFront("GRGEN_LGSP.LGSPGraph graph = procEnv.graph;\n");
            source.AppendFront("GRGEN_LGSP.LGSPActions actions = procEnv.curActions;\n");

            neededEntitiesEmitter.Reset();

            if (fireDebugEvents)
            {
                source.AppendFrontFormat("procEnv.DebugEntering(\"{0}\", \"{1}\");\n",
                                         InjectExec(xgrsName), xgrsStr.Replace("\\", "\\\\").Replace("\"", "\\\""));
            }

            neededEntitiesEmitter.EmitNeededVarAndRuleEntities(seq, source);

            seqGen.EmitSequence(seq, source);

            if (fireDebugEvents)
            {
                source.AppendFrontFormat("procEnv.DebugExiting(\"{0}\");\n",
                                         InjectExec(xgrsName));
            }

            source.AppendFront("return " + seqGen.GetSequenceResult(seq) + ";\n");
            source.Unindent();
            source.AppendFront("}\n");

            List <SequenceExpressionConstructor>        constructors = new List <SequenceExpressionConstructor>();
            Dictionary <SequenceVariable, SetValueType> variables    = new Dictionary <SequenceVariable, SetValueType>();

            seq.GetLocalVariables(variables, constructors, null);
            foreach (SequenceExpressionConstructor cc in constructors)
            {
                SequenceContainerConstructorEmitter.GenerateConstructor(env, model, cc, source);
            }
            source.Append(seqGen.exprGen.perElementMethodSource.ToString());
            seqGen.exprGen.perElementMethodSource.Reset();

            return(true);
        }
예제 #2
0
        private void GenerateInternalDefinedSequenceApplicationMethod(SourceBuilder source, DefinedSequenceInfo sequence, Sequence seq)
        {
            source.AppendFront("public static bool ApplyXGRS_" + sequence.Name + "(GRGEN_LGSP.LGSPGraphProcessingEnvironment procEnv");
            for (int i = 0; i < sequence.Parameters.Length; ++i)
            {
                source.Append(", " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(sequence.ParameterTypes[i]), model) + " var_");
                source.Append(sequence.Parameters[i]);
            }
            for (int i = 0; i < sequence.OutParameters.Length; ++i)
            {
                source.Append(", ref " + TypesHelper.XgrsTypeToCSharpType(TypesHelper.DotNetTypeToXgrsType(sequence.OutParameterTypes[i]), model) + " var_");
                source.Append(sequence.OutParameters[i]);
            }
            source.Append(")\n");
            source.AppendFront("{\n");
            source.Indent();

            source.AppendFront("GRGEN_LGSP.LGSPGraph graph = procEnv.graph;\n");
            source.AppendFront("GRGEN_LGSP.LGSPActions actions = procEnv.curActions;\n");

            neededEntitiesEmitter.Reset();

            if (fireDebugEvents)
            {
                source.AppendFrontFormat("procEnv.DebugEntering(\"{0}\"", sequence.Name);
                for (int i = 0; i < sequence.Parameters.Length; ++i)
                {
                    source.Append(", var_");
                    source.Append(sequence.Parameters[i]);
                }
                source.Append(");\n");
            }

            neededEntitiesEmitter.EmitNeededVarAndRuleEntities(seq, source);

            seqGen.EmitSequence(seq, source);

            if (fireDebugEvents)
            {
                source.AppendFrontFormat("procEnv.DebugExiting(\"{0}\"", sequence.Name);
                for (int i = 0; i < sequence.OutParameters.Length; ++i)
                {
                    source.Append(", var_");
                    source.Append(sequence.OutParameters[i]);
                }
                source.Append(");\n");
            }

            source.AppendFront("return " + seqGen.GetSequenceResult(seq) + ";\n");
            source.Unindent();
            source.AppendFront("}\n");

            List <SequenceExpressionConstructor>        constructors = new List <SequenceExpressionConstructor>();
            Dictionary <SequenceVariable, SetValueType> variables    = new Dictionary <SequenceVariable, SetValueType>();

            seq.GetLocalVariables(variables, constructors, null);
            foreach (SequenceExpressionContainerConstructor cc in constructors)
            {
                SequenceContainerConstructorEmitter.GenerateConstructor(env, model, cc, source);
            }
        }