Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- public class Scanner {
- private int fileIndex;
- private List<object> tokens;
- private TextReader source;
- private Dictionary<char, Symbols> knownSymbols;
- public List<object> Tokens {
- get { return this.tokens; }
- }
- public Scanner(TextReader source) {
- tokens = new List<object>();
- this.source = source;
- knownSymbols = new Dictionary<char, Symbols>();
- knownSymbols.Add(',', Symbols.Comma);
- knownSymbols.Add('+', Symbols.Plus);
- knownSymbols.Add('-', Symbols.Minus);
- knownSymbols.Add(':', Symbols.Colon);
- //knownSymbols.Add('$', Symbols.HexID);
- }
- public void Scan() {
- this.fileIndex = 0;
- while (source.Peek() != -1) {
- char ch = (char)source.Peek();
- Symbols sym = Symbols.Unknown;
- if (char.IsWhiteSpace(ch)) { //If we're at a whitespace character
- source.Read(); //Skip it
- fileIndex++;
- }
- else if (ch.Equals(';')) { //If we've gotten to a comment
- do {
- source.Read(); //Skip a character
- fileIndex++;
- if (source.Peek() == -1) //If we've reached the EOF
- break; //Exit
- ch = (char)source.Peek(); //Get next char
- } while (!ch.Equals('\n')); //Continue until we've reached the EOL
- }
- else if (char.IsLetter(ch) || ch.Equals('_')) { //If we're reading an instruction, var, or register
- StringBuilder data = new StringBuilder(); //Setup our data
- do {
- data.Append(ch); //Append the character to data
- source.Read(); //Consume it
- fileIndex++;
- if (source.Peek() == -1) //If we're at EOF
- break; //Exit
- ch = (char)source.Peek(); //Get next char
- } while (char.IsLetter(ch) || ch.Equals('_')); //Continue
- this.tokens.Add(data.ToString());
- }
- else if (ch.Equals('$')) { //If we need to read a byte or word
- StringBuilder data = new StringBuilder(); //Setup our data
- do {
- data.Append(ch); //Append the character
- source.Read(); //Consume it
- fileIndex++;
- if (source.Peek() == -1) //if we are at the EOF
- break; //Exit
- ch = (char)source.Peek(); //Get next character
- } while (IsHexDigit(ch)); //Continue
- if (data[0] == '$')
- data = data.Remove(0, 1);
- tokens.Add(ushort.Parse(data.ToString()));
- }
- else if (char.IsNumber(ch)) { //If we're reading a number
- StringBuilder data = new StringBuilder(); //Setup our data
- do {
- data.Append(ch); //Append to the data
- source.Read(); //Consume the character
- fileIndex++;
- if (this.source.Peek() == -1) //If we've reached the EOF
- break; //Exit
- ch = (char)source.Peek(); //Get the next char
- } while (char.IsNumber(ch)); //Continue
- tokens.Add(Convert.ToUInt16(data.ToString())); //Add the data
- }
- else if (this.knownSymbols.TryGetValue(ch, out sym))
- {//Try to get a symbol
- tokens.Add(sym); //Add it if we've found it
- source.Read(); //Consume it
- fileIndex++;
- }
- else //Otherwise...
- throw new UnknownSymbolException(ch, this.fileIndex);//We don't know what symbol this is
- }
- } //End .Scan()
- private bool IsHexDigit(char c) {
- return "0123456789ABCDEF".Contains(c.ToString().ToUpper());
- }
- }
- public enum Registers {
- //Notes: These aren't z80 registers
- Unknown = 0,
- A = 4,
- B = 2,
- D = 1,
- X = 16,
- Y = 8
- }
- public enum Symbols {
- Comma,
- Plus,
- Minus,
- Colon,
- Unknown
- }
- [Serializable]
- public class UnknownSymbolException : Exception {
- public UnknownSymbolException() { }
- public UnknownSymbolException(char symbol) :
- base(string.Format("Unknown symbol ('{0}') detected.", symbol)) { }
- public UnknownSymbolException(char symbol, int index) :
- base(string.Format("Unknown symbol ('{0}') detected at index {1}.", symbol, index)) { }
- public UnknownSymbolException(string message) : base(message) { }
- public UnknownSymbolException(string message, Exception inner) : base(message, inner) { }
- protected UnknownSymbolException(
- System.Runtime.Serialization.SerializationInfo info,
- System.Runtime.Serialization.StreamingContext context)
- : base(info, context) { }
- }
- [Serializable]
- public class IncorrectLengthException : Exception {
- public IncorrectLengthException() { }
- public IncorrectLengthException(string message) : base(message) { }
- public IncorrectLengthException(string message, Exception inner) : base(message, inner) { }
- protected IncorrectLengthException(
- System.Runtime.Serialization.SerializationInfo info,
- System.Runtime.Serialization.StreamingContext context)
- : base(info, context) { }
- }
- [Serializable]
- public class UnknownRegisterException : Exception {
- public UnknownRegisterException() { }
- public UnknownRegisterException(string register) :
- base(string.Format("Cannot identify register '{0}'.", register)) { }
- public UnknownRegisterException(string register, Exception inner) :
- base(string.Format("Cannot identify register '{0}'.", register), inner) { }
- protected UnknownRegisterException(
- System.Runtime.Serialization.SerializationInfo info,
- System.Runtime.Serialization.StreamingContext context)
- : base(info, context) { }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement