Exemplo n.º 1
0
        /// <summary>
        /// Processes the specified input path.
        /// </summary>
        /// <param name="inputPath">The input path.</param>
        private void ProcessFile(string inputPath)
        {
            using (Parser parser = new Parser(inputPath))
            {
                CodeWriter codeWriter = new CodeWriter(this.assemblyCode);
                codeWriter.VmFileName = Path.GetFileNameWithoutExtension(inputPath);
                while (parser.HasMoreCommands())
                {
                    parser.Advance();
                    CommandType commandType = parser.GetCommandType();

                    if (commandType == CommandType.C_PUSH || commandType == CommandType.C_POP)
                    {
                        codeWriter.WritePushPop(commandType, parser.GetArg1(), int.Parse(parser.GetArg2()));
                    }
                    if (commandType == CommandType.C_ARITHMETIC)
                    {
                        codeWriter.WriteArithmetic(parser.GetArg1());
                    }
                    if (commandType == CommandType.C_LABEL)
                    {
                        codeWriter.WriteLabel(parser.GetArg1());
                    }
                    if (commandType == CommandType.C_GOTO)
                    {
                        codeWriter.WriteGoto(parser.GetArg1());
                    }
                    if (commandType == CommandType.C_IF)
                    {
                        codeWriter.WriteIf(parser.GetArg1());
                    }
                    if (commandType == CommandType.C_CALL)
                    {
                        codeWriter.WriteCall(parser.GetArg1(), int.Parse(parser.GetArg2()));
                    }
                    if (commandType == CommandType.C_FUNCTION)
                    {
                        codeWriter.WriteFunction(parser.GetArg1(), int.Parse(parser.GetArg2()));
                    }
                    if (commandType == CommandType.C_RETURN)
                    {
                        codeWriter.WriteReturn();
                    }
                }
            }
        }
Exemplo n.º 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();
         }
     }
 }