Ejemplo n.º 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();
                        }
                    }
                }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 3
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();
                    }
                }
            }
        }
Ejemplo n.º 4
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();
         }
     }
 }