private void BuildMethod(FunctionImplementation implementation)
        {
            string name    = VMTranslations.RemovePrefixes(pluginNameVar, implementation.Name.Identifier, false);
            string retType = VMTranslations.RemovePrefixes(pluginNameVar, implementation.ReturnType.Declaration);

            List <string> funcParams = new List <string>();

            foreach (FunctionParameter param in implementation.Parameters)
            {
                string paramType = VMTranslations.RemovePrefixes(pluginNameVar, param.m_Type.Declaration);
                string paramName = Output_CSharp.GetSafeVariableName(param.m_Lvalue.Identifier);

                string paramStr = paramType + " " + paramName;

                if (param is FunctionParameterWithDefault def)
                {
                    string defaultAsStr = Output_CSharp.GetValueAsString(def.m_Default, true);
                    paramStr += " = " + defaultAsStr;
                }

                funcParams.Add(paramStr);
            }

            string parameters = funcParams.Count == 0 ? "" : funcParams.Aggregate((a, b) => a + ", " + b);

            stringBuilder.AppendLine($"{Output_CSharp.GetIndent(2)}public static {retType} {name}({parameters})");
            stringBuilder.AppendLine($"{Output_CSharp.GetIndent(2)}{{");

            BuildImplementation(implementation);

            stringBuilder.AppendLine($"{Output_CSharp.GetIndent(2)}}}");
            stringBuilder.AppendLine();
        }
示例#2
0
        private void BuildMethod(FunctionDeclaration funcDecl)
        {
            string name = funcDecl.Name.Identifier;

            if (customImplementations.ContainsKey(name))
            {
                stringBuilder.AppendLine(customImplementations[name]);
                internalCallId++;
                stringBuilder.AppendLine();
                return;
            }

            string retType = funcDecl.ReturnType.Declaration;

            List <string> funcParams = new List <string>();

            foreach (FunctionParameter param in funcDecl.Parameters)
            {
                string paramType = param.m_Type.Declaration;
                string paramName = param.m_Lvalue.Identifier;
                string paramStr  = paramType + " " + paramName;

                if (param is FunctionParameterWithDefault def)
                {
                    string defaultAsStr = Output_CSharp.GetValueAsString(def.m_Default, false);
                    paramStr += " = " + defaultAsStr;
                }

                funcParams.Add(paramStr);
            }

            string parameters = funcParams.Count == 0 ? "" : funcParams.Aggregate((a, b) => a + ", " + b);

            stringBuilder.AppendLine($"{Output_CSharp.GetIndent(2)}public static {retType} {name}({parameters})");
            stringBuilder.AppendLine($"{Output_CSharp.GetIndent(2)}{{");

            for (int i = funcDecl.Parameters.Count - 1; i >= 0; --i)
            {
                FunctionParameter param = funcDecl.Parameters[i];
                stringBuilder.AppendLine(Output_CSharp.GetIndent(3) + Output_CSharp.GetStackPush(param.m_Type, param.m_Lvalue, false) + ";");
            }

            stringBuilder.AppendLine(Output_CSharp.GetIndent(3) + Output_CSharp.GetInternalCall(internalCallId++) + ";");

            if (funcDecl.ReturnType.GetType() != typeof(VoidType))
            {
                stringBuilder.AppendLine($"{Output_CSharp.GetIndent(3)}return " + Output_CSharp.GetStackPop(funcDecl.ReturnType) + ";");
            }

            stringBuilder.AppendLine($"{Output_CSharp.GetIndent(2)}}}");
            stringBuilder.AppendLine();
        }