예제 #1
0
        public void WriteLine(AssemblyLine line, string comment)
        {
            if (line == null)
            {
                throw new ArgumentNullException(nameof(line));
            }

            switch (line.Type)
            {
            case AssemblyLineType.None:
                break;

            case AssemblyLineType.Label:
                if (IndentLabels)
                {
                    WriteIndentation();
                }

                WriteLabel(line);
                break;

            case AssemblyLineType.Directive:
                if (IndentDirectives)
                {
                    WriteIndentation();
                }

                WriteCommand(line);

                if (line.Parameters.Length > 0)
                {
                    if (IndentParameters)
                    {
                        WriteIndentation();
                    }
                    else
                    {
                        writer.Write(' ');
                    }

                    WriteParameters(line);
                }
                break;

            case AssemblyLineType.Instruction:
                if (IndentInstructions)
                {
                    WriteIndentation();
                }

                WriteCommand(line);

                if (line.Parameters.Length > 0)
                {
                    if (IndentParameters)
                    {
                        WriteIndentation();
                    }
                    else
                    {
                        writer.Write(' ');
                    }

                    WriteParameters(line);
                }
                break;

            default:
                throw new ArgumentException($"Cannot write line of type {line.Type}.", nameof(line));
            }

            if (!string.IsNullOrEmpty(comment))
            {
                //WriteIndentation(); // or space?
                writer.Write(' ');
                WriteComment(comment);
            }

            writer.WriteLine();
        }
예제 #2
0
 public void WriteLine(AssemblyLine line)
 {
     WriteLine(line, null);
 }
예제 #3
0
 private void WriteParameters(AssemblyLine line)
 {
     writer.Write(string.Join(", ", line.Parameters));
 }
예제 #4
0
 private void WriteCommand(AssemblyLine line)
 {
     writer.Write(line.Value);
 }
예제 #5
0
 private void WriteLabel(AssemblyLine line)
 {
     writer.Write(line.Value);
     writer.Write(':');
 }
예제 #6
0
        /// <summary>
        /// Reads the next line from the stream.
        /// </summary>
        /// <returns></returns>
        public AssemblyLine ReadLine()
        {
            if (reader == null)
            {
                return(null);
            }

            // Read the next line from the stream
            var line = reader.ReadLine();

            if (line == null)
            {
                return(null);
            }

            // Trim trailing whitespace
            line = line.Trim();

            // Trim the comment
            line = AssemblyHelpers.RemoveComments(line);

            // Handle empty lines
            if (line.Length == 0)
            {
                return(AssemblyLine.Empty(currentLine++));
            }

            // Handle labels
            var labelIndex = line.IndexOf(Label);

            if (labelIndex >= 0)
            {
                return(AssemblyLine.Label(
                           line.Substring(0, labelIndex),
                           currentLine++
                           ));
            }

            // Handle directives
            var parameterIndex = line.IndexOfAny(WhiteSpace);

            if (parameterIndex >= 0)
            {
                var value      = line.Substring(0, parameterIndex);
                var parameters = AssemblyHelpers.SplitParameters(line.Substring(parameterIndex)).ToArray();

                if (value.Length > 0 && value[0] == '.')
                {
                    return(AssemblyLine.Directive(
                               value,
                               parameters,
                               currentLine++
                               ));
                }
                else
                {
                    return(AssemblyLine.Instruction(
                               value,
                               parameters,
                               currentLine++
                               ));
                }
            }
            else
            {
                return((line.Length > 0 && line[0] == '.') ?  AssemblyLine.Directive(line, currentLine++)
                    : AssemblyLine.Instruction(line, currentLine++));
            }
        }