Esempio n. 1
0
        static void ProcessFile(string path, string fileName, bool append)
        {
            var writerPath = Path.GetDirectoryName(path) + Path.DirectorySeparatorChar +
                             fileName + ".asm";

            try
            {
                using (var sr = new StreamReader(path))
                {
                    using (var sw = new StreamWriter(writerPath, append))
                    {
                        var parser     = new Parser(sr);
                        var codeWriter = new CodeWriter(sw, path);
                        if (!append)
                        {
                            codeWriter.WriteInit();
                        }
                        while (parser.HasMoreCommands())
                        {
                            parser.Advance();
                            commands[parser.CommandType()].Invoke(parser, codeWriter);
                        }
                    }
                }
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Esempio n. 2
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);
                    }
            }
        }