예제 #1
0
        static void Run()
        {
            if (!File.Exists(inputFile) || Path.GetExtension(inputFile) != ".vm")
            {
                throw new ArgumentException("Invalid input file");
            }
            string outputFile = Path.GetDirectoryName(inputFile) + "/" + Path.GetFileNameWithoutExtension(inputFile) + ".asm";

            using (var parser = new Parser(inputFile))
                using (var codeWriter = new CodeWriter(outputFile))
                {
                    while (parser.HasMoreCommands())
                    {
                        parser.Advance();
                        var type = parser.CommandType();
                        if (type == CommandTypeEnum.C_ARITHMETIC)
                        {
                            codeWriter.WriteArithmetric(parser.Arg1());
                        }
                        else if (type == CommandTypeEnum.C_PUSH || type == CommandTypeEnum.C_POP)
                        {
                            codeWriter.WritePushPop(type, parser.Arg1(), parser.Arg2());
                        }
                        else
                        {
                            throw new NotImplementedException();
                        }
                    }
                }
        }
예제 #2
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);
            }
        }
예제 #3
0
 static void Process(Parser parser, CodeWriter codeWriter)
 {
     while (parser.HasMoreCommands())
     {
         parser.Advance();
         var type = parser.CommandType();
         if (type == CommandTypeEnum.C_ARITHMETIC)
         {
             codeWriter.WriteArithmetric(parser.Arg1());
         }
         else if (type == CommandTypeEnum.C_PUSH || type == CommandTypeEnum.C_POP)
         {
             codeWriter.WritePushPop(type, parser.Arg1(), parser.Arg2());
         }
         else if (type == CommandTypeEnum.C_LABEL)
         {
             codeWriter.WriteLabel(parser.Arg1());
         }
         else if (type == CommandTypeEnum.C_GOTO)
         {
             codeWriter.WriteGoto(parser.Arg1());
         }
         else if (type == CommandTypeEnum.C_IF)
         {
             codeWriter.WriteIf(parser.Arg1());
         }
         else if (type == CommandTypeEnum.C_FUNCTION)
         {
             codeWriter.WriteFunction(parser.Arg1(), parser.Arg2());
         }
         else if (type == CommandTypeEnum.C_CALL)
         {
             codeWriter.WriteCall(parser.Arg1(), parser.Arg2());
         }
         else
         {
             codeWriter.WriteReturn();
         }
     }
 }