Advertisement
BlakPilar

Assembly Scanner

Mar 3rd, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5.  
  6.     public class Scanner {
  7.  
  8.         private int fileIndex;
  9.         private List<object> tokens;
  10.         private TextReader source;
  11.         private Dictionary<char, Symbols> knownSymbols;
  12.  
  13.         public List<object> Tokens {
  14.             get { return this.tokens; }
  15.         }
  16.  
  17.         public Scanner(TextReader source) {
  18.             tokens = new List<object>();
  19.             this.source = source;
  20.             knownSymbols = new Dictionary<char, Symbols>();
  21.  
  22.             knownSymbols.Add(',', Symbols.Comma);
  23.             knownSymbols.Add('+', Symbols.Plus);
  24.             knownSymbols.Add('-', Symbols.Minus);
  25.             knownSymbols.Add(':', Symbols.Colon);
  26.             //knownSymbols.Add('$', Symbols.HexID);
  27.         }
  28.  
  29.         public void Scan() {
  30.             this.fileIndex = 0;
  31.  
  32.             while (source.Peek() != -1) {
  33.                 char ch = (char)source.Peek();
  34.                 Symbols sym = Symbols.Unknown;
  35.  
  36.                 if (char.IsWhiteSpace(ch)) {                        //If we're at a whitespace character
  37.                     source.Read();                                  //Skip it
  38.                     fileIndex++;
  39.                 }
  40.  
  41.                 else if (ch.Equals(';')) {                          //If we've gotten to a comment
  42.                     do {
  43.                         source.Read();                              //Skip a character
  44.                         fileIndex++;
  45.                         if (source.Peek() == -1)                    //If we've reached the EOF
  46.                             break;                                  //Exit
  47.                         ch = (char)source.Peek();                   //Get next char
  48.                     } while (!ch.Equals('\n'));                     //Continue until we've reached the EOL
  49.                 }
  50.  
  51.                 else if (char.IsLetter(ch) || ch.Equals('_')) {     //If we're reading an instruction, var, or register
  52.                     StringBuilder data = new StringBuilder();       //Setup our data
  53.                     do {
  54.                         data.Append(ch);                            //Append the character to data
  55.                         source.Read();                              //Consume it
  56.                         fileIndex++;
  57.                         if (source.Peek() == -1)                    //If we're at EOF
  58.                             break;                                  //Exit
  59.                         ch = (char)source.Peek();                   //Get next char
  60.                     } while (char.IsLetter(ch) || ch.Equals('_'));  //Continue
  61.                     this.tokens.Add(data.ToString());
  62.                 }
  63.  
  64.                 else if (ch.Equals('$')) {                          //If we need to read a byte or word
  65.                     StringBuilder data = new StringBuilder();       //Setup our data
  66.                     do {
  67.                         data.Append(ch);                            //Append the character
  68.                         source.Read();                              //Consume it
  69.                         fileIndex++;
  70.                         if (source.Peek() == -1)                    //if we are at the EOF
  71.                             break;                                  //Exit
  72.                         ch = (char)source.Peek();                   //Get next character
  73.                     } while (IsHexDigit(ch));                       //Continue
  74.  
  75.                     if (data[0] == '$')
  76.                         data = data.Remove(0, 1);
  77.                     tokens.Add(ushort.Parse(data.ToString()));
  78.                 }
  79.  
  80.                 else if (char.IsNumber(ch)) {                       //If we're reading a number
  81.                     StringBuilder data = new StringBuilder();       //Setup our data
  82.                     do {
  83.                         data.Append(ch);                            //Append to the data
  84.                         source.Read();                              //Consume the character
  85.                         fileIndex++;
  86.                         if (this.source.Peek() == -1)               //If we've reached the EOF
  87.                             break;                                  //Exit
  88.                         ch = (char)source.Peek();                   //Get the next char
  89.                     } while (char.IsNumber(ch));                    //Continue
  90.                     tokens.Add(Convert.ToUInt16(data.ToString()));  //Add the data
  91.                 }
  92.  
  93.                 else if (this.knownSymbols.TryGetValue(ch, out sym))
  94.                 {//Try to get a symbol
  95.                     tokens.Add(sym);                                //Add it if we've found it
  96.                     source.Read();                                  //Consume it
  97.                     fileIndex++;
  98.                 }
  99.  
  100.                 else                                                //Otherwise...
  101.                     throw new UnknownSymbolException(ch, this.fileIndex);//We don't know what symbol this is
  102.             }
  103.         } //End .Scan()
  104.  
  105.         private bool IsHexDigit(char c) {
  106.             return "0123456789ABCDEF".Contains(c.ToString().ToUpper());
  107.         }
  108.     }
  109.  
  110.     public enum Registers {
  111.         //Notes: These aren't z80 registers
  112.         Unknown = 0,
  113.         A = 4,
  114.         B = 2,
  115.         D = 1,
  116.         X = 16,
  117.         Y = 8
  118.     }
  119.  
  120.     public enum Symbols {
  121.         Comma,
  122.         Plus,
  123.         Minus,
  124.         Colon,
  125.         Unknown
  126.     }
  127.  
  128.     [Serializable]
  129.     public class UnknownSymbolException : Exception {
  130.         public UnknownSymbolException() { }
  131.  
  132.         public UnknownSymbolException(char symbol) :
  133.             base(string.Format("Unknown symbol ('{0}') detected.", symbol)) { }
  134.  
  135.         public UnknownSymbolException(char symbol, int index) :
  136.             base(string.Format("Unknown symbol ('{0}') detected at index {1}.", symbol, index)) { }
  137.  
  138.         public UnknownSymbolException(string message) : base(message) { }
  139.  
  140.         public UnknownSymbolException(string message, Exception inner) : base(message, inner) { }
  141.  
  142.         protected UnknownSymbolException(
  143.           System.Runtime.Serialization.SerializationInfo info,
  144.           System.Runtime.Serialization.StreamingContext context)
  145.             : base(info, context) { }
  146.     }
  147.  
  148.     [Serializable]
  149.     public class IncorrectLengthException : Exception {
  150.         public IncorrectLengthException() { }
  151.  
  152.         public IncorrectLengthException(string message) : base(message) { }
  153.  
  154.         public IncorrectLengthException(string message, Exception inner) : base(message, inner) { }
  155.  
  156.         protected IncorrectLengthException(
  157.           System.Runtime.Serialization.SerializationInfo info,
  158.           System.Runtime.Serialization.StreamingContext context)
  159.             : base(info, context) { }
  160.     }
  161.  
  162.     [Serializable]
  163.     public class UnknownRegisterException : Exception {
  164.         public UnknownRegisterException() { }
  165.  
  166.         public UnknownRegisterException(string register) :
  167.             base(string.Format("Cannot identify register '{0}'.", register)) { }
  168.  
  169.         public UnknownRegisterException(string register, Exception inner) :
  170.             base(string.Format("Cannot identify register '{0}'.", register), inner) { }
  171.  
  172.         protected UnknownRegisterException(
  173.           System.Runtime.Serialization.SerializationInfo info,
  174.           System.Runtime.Serialization.StreamingContext context)
  175.             : base(info, context) { }
  176.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement