Esempio n. 1
0
        /// <summary>
        /// Emit the C# method call, defined in the context class (codecontext.ContextName).
        /// </summary>
        /// <param name="codecontext"></param>
        /// <returns></returns>
        private ExpressionType TryEmitCsMethodCall(EmitCodeContext codecontext)
        {
            List <MethodDecl> matchingMethods = new List <MethodDecl>();

            foreach (var decl in codecontext.Declarations.Methods)
            {
                if (decl.DeclMethodName == MethodName && decl.BodyCSharp != null)
                {
                    if (decl.MethodArguments != null && decl.MethodArguments.Count != CallArguments.Count)
                    {
                        throw new GeneratorException(Position, "Invalid arguments count in method call " + MethodName);
                    }

                    matchingMethods.Add(decl);
                }
            }

            if (matchingMethods.Count > 1)
            {
                throw new GeneratorException(Position, "Ambiguous C# method call.");
            }

            if (matchingMethods.Count == 1)
            {
                MethodDecl decl = matchingMethods[0];

                codecontext.Write(decl.GeneratedMethodName + "(");

                bool bFirst = true;
                if (CallArguments != null)
                {
                    foreach (var x in CallArguments)
                    {
                        if (!bFirst)
                        {
                            codecontext.Write(", ");
                        }
                        else
                        {
                            bFirst = false;
                        }

                        x.EmitCs(codecontext);
                    }
                }
                codecontext.Write(")");

                return(decl.ReturnType);
            }

            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Init declarations list with one method declaration.
        /// </summary>
        /// <param name="position"></param>
        /// <param name="methoddecl"></param>
        public DeclarationsList(ExprPosition position, DeclarationsList decls, MethodDecl methoddecl)
            : this(position)
        {
            if (decls != null)
            {
                Classes = decls.Classes;
                Methods.AddRange(decls.Methods);
            }

            if (methoddecl != null)
            {
                if (!methoddecl.IsMainMethod)
                {
                    foreach (var m in Methods)
                    {
                        if (m.DeclMethodName == methoddecl.DeclMethodName)
                        { // arguments must match
                            if (m.MethodArguments.Count != methoddecl.MethodArguments.Count)
                            {
                                throw new GeneratorException(position, "Methods " + methoddecl.DeclMethodName + ": Arguments mishmash.");
                            }

                            for (int arg = 0; arg < m.MethodArguments.Count; ++arg)
                            {
                                if (!m.MethodArguments[arg].VariableType.Equals(methoddecl.MethodArguments[arg].VariableType))
                                {
                                    throw new GeneratorException(position, "Methods " + methoddecl.DeclMethodName + ": Arguments mishmash.");
                                }
                                if (m.MethodArguments[arg].VariableName != methoddecl.MethodArguments[arg].VariableName)
                                {
                                    throw new GeneratorException(position, "Methods " + methoddecl.DeclMethodName + ": Arguments mishmash.");
                                }
                            }
                        }
                    }
                }

                Methods.Insert(0, methoddecl);
            }
        }
Esempio n. 3
0
        private ExpressionType TryEmitExtractionMethodCall(EmitCodeContext codecontext)
        {
            List <MethodDecl> matchingMethods = new List <MethodDecl>();

            foreach (var decl in codecontext.Declarations.Methods)
            {
                if (decl.DeclMethodName == MethodName && decl.Body != null)
                {
                    if (decl.IsMainMethod)
                    {
                        throw new GeneratorException(Position, "main method cannot be called!");
                    }

                    if (decl.MethodArguments.Count != CallArguments.Count)
                    {
                        throw new GeneratorException(Position, "Invalid arguments count in method call " + MethodName);
                    }

                    matchingMethods.Add(decl);
                }
            }

            if (matchingMethods.Count > 0)
            {
                codecontext.Write("ActionItem.AddAction( new ActionItem.ExtractionMethod[]{");

                bool bFirstMethod = true;
                foreach (var decl in matchingMethods)
                {
                    if (bFirstMethod)
                    {
                        bFirstMethod = false;
                    }
                    else
                    {
                        codecontext.Write(", ");
                    }
                    codecontext.Write(decl.GeneratedMethodName);
                }

                codecontext.Write("}, " + scopeLocalVarName + ".context, new LocalVariables() {" + codecontext.Output.NewLine);

                codecontext.Level++;

                MethodDecl somedecl            = matchingMethods[0];
                string     cannotAddActionDict = null;
                for (int arg = 0; arg < CallArguments.Count; ++arg)
                {
                    if (arg > 0)
                    {
                        codecontext.Write("," + codecontext.Output.NewLine);
                    }
                    codecontext.Write("{\"" + somedecl.MethodArguments[arg].VariableName + "\", ", codecontext.Level);
                    ExpressionType t = CallArguments[arg].EmitCs(codecontext);
                    codecontext.Write("}");

                    if (!t.Equals(somedecl.MethodArguments[arg].VariableType))
                    {
                        throw new GeneratorException(Position, "Type mishmash.");
                    }

                    // check if the argument is able to add new action
                    VariableUse varuse;
                    if ((varuse = CallArguments[arg] as VariableUse) != null)
                    {
                        if (cannotAddActionDict != null)
                        {
                            cannotAddActionDict += ",";
                        }
                        cannotAddActionDict += "{\"" + somedecl.MethodArguments[arg].VariableName + "\",_parameters.CannotAddActionForVariable(\"" + varuse.VariableName + "\")}";
                    }
                }

                codecontext.Write(" }.SetCannotAddAction(new Dictionary<string,bool>(){" + cannotAddActionDict + "}))");
                codecontext.Level--;

                return(ExpressionType.VoidType);
            }
            else
            {
                return(null);
            }
        }