Exemplo n.º 1
0
        /// <summary>
        /// Gets the lines of source code. Ignores all empty lines and comments.
        /// </summary>
        /// <param name="options"></param>
        /// <returns></returns>
        private SourceLine[] GetSourceLines(AssemblerOptions options)
        {
            List<SourceLine> sourceLines = new List<SourceLine>();

            var lineNumber = 1;

            using (var sourceReader = new StreamReader(options.SourceFile))
            {
                var line = sourceReader.ReadLine();

                while (line != null)
                {
                    if (string.IsNullOrWhiteSpace(line))
                    {
                        //Do nothing
                    }
                    else
                    {
                        if (!line.TrimStart().StartsWith("#"))
                        {
                            sourceLines.Add(new SourceLine(lineNumber, line));
                        }
                    }

                    lineNumber++;
                    line = sourceReader.ReadLine();
                }
            }

            return sourceLines.ToArray();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var options = new AssemblerOptions()
            {
                SourceFile = "HelloWorld.asm",
                OutputFile = @"C:\Users\rich.quackenbush\Documents\Visual Studio 2013\Projects\EVM\Debug\HelloWorld.evm"
            };

            var assembler = new EvmAssembler();

            var result = assembler.Assemble(options);

            foreach (var message in result.Messages)
            {
                System.Console.WriteLine("{0}:{1} {2}",
                    message.ErrorLevel,
                    message.LineNumber == null ? "" : string.Format("Line [{0}]", message.LineNumber),
                    message.Message);
            }

            if (result.Succeeded)
            {
                System.Console.WriteLine("Succeeded");
            }
            else
            {
                System.Console.WriteLine("Failed.");
            }

            System.Console.ReadKey();
        }
Exemplo n.º 3
0
        public AssemblerResult Assemble(AssemblerOptions options)
        {
            if (options == null)
                throw new ArgumentNullException("options");

            List<AssemblerMessage> messages = new List<AssemblerMessage>();

            try
            {
                //Get the lines
                var sourceLines = GetSourceLines(options);

                //Do the first pass
                var pass1Result = Pass1(messages, sourceLines);

                if (messages.All(m => m.ErrorLevel != ErrorLevel.Error))
                {
                    Pass2(messages, pass1Result, options.OutputFile);
                }
            }
            catch (Exception ex)
            {
                messages.Add(new AssemblerMessage(ErrorLevel.Error, ex.Message));
            }

            //We're done here
            return new AssemblerResult()
            {
                Messages = messages.ToArray(),
                Succeeded = messages.All(m => m.ErrorLevel != ErrorLevel.Error)
            };
        }