示例#1
0
        public void CreateFunction()
        {
            // Test to create random inline functions
            List <JunkCode.JunkType> junktypes = new List <JunkCode.JunkType>();
            string function = "";

            string[] types =
            {
                "float", "int", "bool", "char*", "char", "DWORD", "void",
            };
            Random rnd = new Random();

            string returnType = types[rnd.Next(0, types.Length)];
            string funcName   = JunkCode.Junk.RandomString();

            // Create the returntype and function name
            function += string.Concat(returnType, " "); // float
            function += string.Concat(funcName, "(");   // float RandomName(

            // Create the parameters
            int paramNum = rnd.Next(1, 10);

            for (int i = 0; i < paramNum; i++)
            {
                function += string.Concat(types[rnd.Next(0, types.Length)], " ");
                function += JunkCode.Junk.RandomString();
                if (i != paramNum - 1)
                {
                    function += ", ";
                    continue;
                }
                else
                {
                    function += string.Concat(") ", Environment.NewLine);
                    break;
                }
            }

            // Open brace
            function += string.Concat("{", Environment.NewLine);

            // Make declarations for lawls
            int decNum = rnd.Next(1, 50);

            for (int i = 0; i < decNum; i++)
            {
                JunkCode.JunkType randType = JunkCode.Junk.GetRandomType();
                function += string.Concat(randType.Definition() + ";", Environment.NewLine);
                junktypes.Add(randType);
            }

            // Check if memory needs to be deleted
            for (int i = 0; i < junktypes.Count; i++)
            {
                if (junktypes[i] is JunkCode.JunkPointer)
                {
                    JunkCode.JunkPointer j = (JunkCode.JunkPointer)junktypes[i];

                    if (j.IsDeleted() != true)
                    {
                        function += j.Cleanup() + ";" + Environment.NewLine;
                    }
                }
            }

            // Return out
            switch (returnType)
            {
            case "int":
            case "DWORD":
            {
                JunkCode.JunkInteger j = new JunkCode.JunkInteger();
                function += "return " + j.Value() + ";" + Environment.NewLine;
            }
            break;

            case "bool":
            {
                JunkCode.JunkBool j = new JunkCode.JunkBool();
                function += "return " + j.Value() + ";" + Environment.NewLine;
            }
            break;

            case "char*":
            {
                JunkCode.JunkString j = new JunkCode.JunkString();
                function += "return " + j.Value() + ";" + Environment.NewLine;
            }
            break;

            case "float":
            {
                JunkCode.JunkFloat j = new JunkCode.JunkFloat();
                function += "return " + j.Value() + ";" + Environment.NewLine;
            }
            break;

            case "char":
            {
                JunkCode.JunkCharacter j = new JunkCode.JunkCharacter();
                function += "return" + j.Value() + ";" + Environment.NewLine;
            }
            break;

            case "void":
            {
                function += "return;" + Environment.NewLine;
            }
            break;
            }


            // Close brace
            function += string.Concat("}", Environment.NewLine);

            textBox1.Text = function;
        }
示例#2
0
        public static string GenerateCode(int minPossibleLines = 5, int maxPossibleLines = 10)
        {
            string returnStr = "";

            int numOfLines = random.Next(minPossibleLines, maxPossibleLines);
            List <JunkInteger> junkInts = new List <JunkInteger>();

            for (int i = 0; i < numOfLines; i++)
            {
                if (junkInts.Count == 0 || random.Next(0, 3) != 0)
                {
                    JunkType randomType = GetRandomType();

                    while (randomType is JunkPointer)
                    {
                        randomType = GetRandomType();
                    }

                    if (randomType is JunkInteger)
                    {
                        junkInts.Add((JunkInteger)randomType);
                    }

                    returnStr = string.Concat(returnStr, randomType.Definition());
                }
                else
                {
                    int         count = junkInts.Count;
                    JunkInteger item  = junkInts[random.Next(count)];
                    if (random.Next(0, 2) == 0)
                    {
                        item = new JunkInteger();
                        junkInts.Add(item);
                        returnStr = string.Concat(returnStr, item.Declaration(), " ");
                    }

                    JunkInteger junkInt = junkInts[random.Next(count)];
                    JunkInteger item1   = junkInts[random.Next(count)];
                    returnStr = string.Concat(returnStr, item.GetName(), " = ", junkInt.GetName());
                    //returnStr = (random.Next(0, 2) != 0 ? string.Concat(returnStr, " - ") : string.Concat(returnStr, " + "));
                    switch (random.Next(0, 3))
                    {
                    case 0:
                    {
                        returnStr = string.Concat(returnStr, " - ");
                    }
                    break;

                    case 1:
                    {
                        returnStr = string.Concat(returnStr, " + ");
                    }
                    break;

                    case 2:
                    {
                        returnStr = string.Concat(returnStr, " * ");
                    }
                    break;
                    }
                    returnStr = string.Concat(returnStr, item1.GetName());
                }

                returnStr = string.Concat(returnStr, ";", Environment.NewLine);
            }

            return(returnStr);
        }