Пример #1
0
        /// <summary>
        /// Deals with turning the CppFunction types into V types, function name to snake case, params
        /// to snake case and any other data transformations we will need for the V functions
        /// </summary>
        static ParsedFunction ParsedFunctionFromCppFunction(CppFunction cFunc, Config config)
        {
            var f = new ParsedFunction
            {
                Name  = cFunc.Name,
                VName = V.ToSnakeCase(config.StripFunctionPrefix(cFunc.Name))
            };

            // hack to fix ghetto forced 'init' module function
            if (f.VName == "init")
            {
                f.VName = config.ModuleName + "_" + f.VName;
            }

            if (cFunc.ReturnType.GetDisplayName() != "void")
            {
                f.RetType  = cFunc.ReturnType.GetDisplayName();
                f.VRetType = V.GetVType(cFunc.ReturnType);
            }

            foreach (var param in cFunc.Parameters)
            {
                var p = new ParsedParameter
                {
                    Name  = param.Name.EscapeReserved(),
                    VName = V.ToSnakeCase(config.StripFunctionPrefix(param.Name)).EscapeReserved(),
                    Type  = param.Type.GetDisplayName(),
                    VType = V.GetVType(param.Type)
                };
                f.Parameters.Add(p);
            }

            return(f);
        }
Пример #2
0
        static void WriteCFunction(StreamWriter writer, ParsedFunction func)
        {
            writer.Write($"fn C.{func.Name}(");
            foreach (var p in func.Parameters)
            {
                writer.Write($"{p.Name} {p.VType}");
                if (func.Parameters.Last() != p)
                {
                    writer.Write(", ");
                }
            }
            writer.Write(")");

            if (func.RetType != null)
            {
                writer.Write($" {func.VRetType}");
            }
            writer.WriteLine();
        }
Пример #3
0
        static void WriteVFunction(StreamWriter writer, ParsedFunction func)
        {
            // first, V function def
            writer.WriteLine("[inline]");
            writer.Write($"pub fn {func.VName}(");
            foreach (var p in func.Parameters)
            {
                // special case for byteptr which we convert to string for the function def
                if (p.VType == "byteptr")
                {
                    writer.Write($"{p.VName} string");
                }
                else
                {
                    writer.Write($"{p.VName} {p.VType}");
                }

                if (func.Parameters.Last() != p)
                {
                    writer.Write(", ");
                }
            }
            writer.Write(")");

            if (func.VRetType != null)
            {
                if (func.VRetType == "byteptr")
                {
                    writer.WriteLine(" string {");
                }
                else
                {
                    writer.WriteLine($" {func.VRetType} {{");
                }
            }
            else
            {
                writer.WriteLine(" {");
            }

            // now the function body calling the C function
            writer.Write("\t");
            if (func.VRetType != null)
            {
                writer.Write("return ");

                // special case for byteptr, which we cast to string
                if (func.VRetType == "byteptr")
                {
                    writer.Write("string(");
                }
            }

            writer.Write($"C.{func.Name}(");
            foreach (var p in func.Parameters)
            {
                // special case for byteptr which was converted to string above
                if (p.VType == "byteptr")
                {
                    writer.Write($"{p.VName}.str");
                }
                else
                {
                    writer.Write($"{p.VName}");
                }

                if (func.Parameters.Last() != p)
                {
                    writer.Write(", ");
                }
            }

            // close the string cast if we are returning a byteptr cast to string
            if (func.VRetType == "byteptr")
            {
                writer.Write(")");
            }

            writer.WriteLine(")");
            writer.WriteLine("}");

            writer.WriteLine();
        }