Beispiel #1
0
        static void Run()
        {
            string outputFile = "";
            var    attr       = File.GetAttributes(inputPath);

            if (attr.HasFlag(FileAttributes.Directory))
            {
                // handle directory
                if (!Directory.Exists(inputPath))
                {
                    throw new ArgumentException("Invalid input path");
                }
                var dirName = new DirectoryInfo(inputPath).Name;
                outputFile = $"{inputPath}/{dirName}.asm";
                var vmFiles = Directory.GetFiles(inputPath, "*.vm", SearchOption.TopDirectoryOnly);
                using (var codeWriter = new CodeWriter(outputFile))
                {
                    var flag = false;
                    foreach (var vmFile in vmFiles)
                    {
                        if (vmFile.EndsWith("Sys.vm"))
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (flag)
                    {
                        codeWriter.WriteInit();
                    }
                    foreach (var vmFile in vmFiles)
                    {
                        using (var parser = new Parser(vmFile))
                        {
                            codeWriter.SetFileName(Path.GetFileNameWithoutExtension(vmFile));
                            Process(parser, codeWriter);
                        }
                    }
                }
            }
            else
            {
                // handle a single source file
                if (!File.Exists(inputPath) || Path.GetExtension(inputPath) != ".vm")
                {
                    throw new ArgumentException("Invalid input path");
                }
                outputFile = Path.ChangeExtension(inputPath, ".asm");
                using (var parser = new Parser(inputPath))
                    using (var codeWriter = new CodeWriter(outputFile))
                    {
                        Process(parser, codeWriter);
                    }
            }
        }