Exemple #1
0
        private void AddToBoard()
        {
            Path     = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            MainPath = Path.Replace("\\bin\\Debug\\netcoreapp3.1", "");
            MainPath = MainPath.Replace("file:\\", "");


            // Asetetaan nappulat paikoilleen ja asetetaan niille kuvakkeet
            foreach (Piece piece in Pieces)
            {
                int x = piece.XPos;
                int y = piece.YPos;

                PBoxGrid.Grid[x, y].Tag = piece.Tag;

                if (!(piece is Special))
                {
                    PBoxGrid.Grid[x, y].Image = Image.FromFile(MainPath + piece.IconPath);
                }
            }
        }
Exemple #2
0
        // Main method - Takes input, processes, and provides output.
        static void Main(string[] args)
        {
            // This will be the main method's fully qualified name once it is found. The last step in the C++ code gen is to add the C++ main method which calls this.
            MainPath = "";

            // This ensures the command has admin. There's a helpful command you can call anywhere called cmd4 which autocalls cmd with admin.
            if (!HasAdministratorPrivileges())
            {
                Console.WriteLine("Please only use this command in cmd4! To open cmd4, open cmd and run cmd4, or press win+r and enter cmd4.");
                return;
            }

            // This is a test user's can do to check if C4 was installed, AKA if the app is running, which we know it is.
            if (args.Length >= 1 && args[0] == "detonate")
            {
                ConsoleColor color = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine("KABOOM!!! C4 is now installed on your machine!");
                Console.ForegroundColor = color;
            }

            // Give help.
            if (args.Length <= 0 || (args.Length >= 1 &&
                                     args[0].ToLower().StartsWith("help") ||
                                     args[0].StartsWith(@"/?") || args[0].StartsWith(@"\?") || args[0].StartsWith(@"?")))
            {
                Console.WriteLine("Build and run: C4 run filename.C4");
                Console.WriteLine("Build only: C4 build filename.C4");
            }
            else if (args.Length >= 1)
            {
                // Run a C4 source file (Build and run) (TODO)
                if (args[0] == "run")
                {
                    if (args.Length >= 2)
                    {
                        string fn = args[1];
                        //TODO
                        Console.WriteLine("//TODO");
                    }
                    else
                    {
                        ConsoleColor color = Console.ForegroundColor;
                        Console.Write("Build and run: C4 run ");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("-->");
                        Console.ForegroundColor = color;
                        Console.WriteLine("filename.C4");
                    }
                }
                // Build a C4 source file. (Should eventually build the whole program, using just the main file.)
                if (args[0] == "build")
                {
                    if (args.Length >= 2)
                    {
                        string fn = args[1];
                        if (args.Length >= 3)
                        {
                            // This option only transpiles to C++
                            if (args[2] == "-CPP")
                            {
                                // Read C4 file.
                                string C4_SRC = File.ReadAllText(fn);

                                // This is the code without using C#'s unsafe option. Eventually, there will be a step to convert the methods to unsafe so we can use pointers.
                                string CS_SRC_SAFE = "";

                                // Allow C++ preprocessor directives to be parsed. This allows us to convert them back later. Right now, they are seen as global function statements.
                                foreach (string ln in C4_SRC.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries))
                                {
                                    if (ln.StartsWith(@"#"))
                                    {
                                        CS_SRC_SAFE += "CPP_SPECIAL_CHAR(\"HASH\", " + ToLiteral(ln) + ");" + Environment.NewLine;
                                    }
                                    else
                                    {
                                        CS_SRC_SAFE += ln + Environment.NewLine;
                                    }
                                }

                                // Now the C# can be parsed even if it's invalid, so we get the abstract syntax tree.
                                var AST = CSharpSyntaxTree.ParseText(CS_SRC_SAFE);
                                CSharpSyntaxNode ROOT = (CSharpSyntaxNode)AST.GetRoot();

                                // C++ Code gen
                                string CPP_SRC = "";

                                // Generate actual C++ (debug = false)
                                debug = false;
                                AST2CPP(ROOT, ref CPP_SRC);
                                CPP_SRC += Environment.NewLine + Environment.NewLine +
                                           "int main() {" + Environment.NewLine + MainPath.Replace(".", "::") +
                                           "();" + Environment.NewLine + "}" + Environment.NewLine;
                                File.WriteAllText("output.cpp", CPP_SRC);

                                // Generate AST file for debugging (debug = true)
                                CPP_SRC = "";
                                debug   = true;
                                AST2CPP(ROOT, ref CPP_SRC);
                                File.WriteAllText("output.ast", CPP_SRC);
                            }
                            else
                            {
                                Console.WriteLine("Invalid argument: " + args[2]);
                            }
                        }
                        else
                        {
                            //TODO
                            Console.WriteLine("//TODO");
                        }
                    }
                    else
                    {
                        // Error no filename
                        ConsoleColor color = Console.ForegroundColor;
                        Console.Write("Build and run: C4 build ");
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.Write("-->");
                        Console.ForegroundColor = color;
                        Console.WriteLine("filename.C4");
                    }
                }
            }
        }