コード例 #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 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();
         }
     }
 }