Example #1
0
        static int Main(string[] args)
        {
            try
            {
                string sourceFilePath           = "";
                string objectFilePath           = "";
                bool   generateAssemblyFile     = false;
                bool   generateAssemblyComments = false;

                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-a")
                    {
                        generateAssemblyFile = true;
                    }
                    else if (args[i] == "-ac")
                    {
                        generateAssemblyFile     = true;
                        generateAssemblyComments = true;
                    }
                    else if (args[i] == "-o")
                    {
                        i++;
                        objectFilePath = args[i];
                    }
                    else
                    {
                        sourceFilePath = args[i];
                    }
                }

                if (String.IsNullOrEmpty(sourceFilePath))
                {
                    ShowUsage();
                    return(-1);
                }

                CompilationContext context = CmCompiler.CompileFile(sourceFilePath);

                objectFilePath = !String.IsNullOrEmpty(objectFilePath)
                    ? objectFilePath
                    : Path.Combine(
                    Path.GetDirectoryName(sourceFilePath),
                    Path.GetFileNameWithoutExtension(sourceFilePath) + ".o"
                    );

                using (var fs = new FileStream(objectFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    CmCompiler.CreateObjectCode(fs, new RIVMArchitecture(), context.GetIR(), context.GetStringConstants(), context.GetGlobalVariables(), context.GetFunctions());
                }

                if (generateAssemblyFile)
                {
                    string assemblyFilePath = Path.Combine(
                        Path.GetDirectoryName(sourceFilePath),
                        Path.GetFileNameWithoutExtension(sourceFilePath) + ".a"
                        );

                    using (var fs = new FileStream(assemblyFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        CmCompiler.GenerateAssemblyOutput(fs, context.GetIR(), generateAssemblyComments);
                    }
                }

                return(0);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.ToString());
                return(-1);
            }
        }