Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var myClass = new ClassDeclaration("CMyClass");

            // Add a private field
            myClass.Fields.Add(new FieldDeclaration("m_someInt", Primitive.Int, AccessLevel.Private));

            // Define our custom type
            var myType = new TypeReference("CSomeOtherClass").MakePointer();

            // And store a private pointer with a basic comment
            myClass.Fields.Add(new FieldDeclaration("m_pSomePointer", myType, AccessLevel.Private,
                new Comment("Test comment")));

            // Add getters
            myClass.Functions.Add(new FunctionDeclaration("GetInteger", Primitive.Int));
            myClass.Functions.Add(new FunctionDeclaration("GetOtherClass", myType));

            // And setters
            var intArg = new VariableDeclaration("value", Primitive.Int);
            var classPtrArg = new VariableDeclaration("value", myType);

            myClass.Functions.Add(new FunctionDeclaration("SetInteger", Primitive.Void,
                AccessLevel.Public, false, intArg));
            myClass.Functions.Add(new FunctionDeclaration("SetOtherClass", Primitive.Void,
                AccessLevel.Public, false, classPtrArg));

            // Add a virtual function with a multiline comment
            var comment = new MultilineComment("This is a function",
                "that should be overridden",
                "just because it can be");

            var function = new FunctionDeclaration("OverrideMe", Primitive.Char.MakePointer().MakeConst(),
                AccessLevel.Public, true);

            function.Comment = comment;
            myClass.Functions.Add(function);

            var classSource = myClass.CreateSource();

            Console.Write(myClass.CreateSource());
            Console.ReadLine();
        }
        /// <summary>
        /// Creates a new FunctionDeclaration given a source string and access level.
        /// </summary>
        /// <param name="declaration">The source code for this function's declaration.</param>
        /// <param name="accessModifier">The access level for this function. Functions are typically parsed from a class/struct declaration and have no knowledge of their own access level.</param>
        /// <returns></returns>
        public static FunctionDeclaration Create(string declaration, AccessLevel accessModifier)
        {
            var tokens = declaration.Trim().Split(" ".ToCharArray()).ToList();

            var tempVar = VariableDeclaration.Create(declaration.Remove(declaration.IndexOf("(")));
            var func = new FunctionDeclaration(tempVar.Name, tempVar.Type, accessModifier, tokens.Contains("virtual"));

            var firstIndex = declaration.IndexOf("(") + 1;
            var arguments = declaration.Substring(firstIndex, declaration.IndexOf(")") - firstIndex).Split(ArgSeparator);

            foreach(var arg in arguments.Where(arg => !string.IsNullOrEmpty(arg)))
                func.Arguments.Add(VariableDeclaration.Create(arg));

            return func;
        }