public WriteOperationArgument(Token tok)
		{
			if (tok.Type == TokenType.Identifier)
			{
				if (tok.Value.StartsWith("arg"))
				{
					Type = OperationSourceType.Argument;
					ParentArgIdx = (byte)(Utils.SingleDigitParse(tok.Value[3]) - 1);
				}
				else
				{
					throw new Exception("Unknown identifier for a write operation argument!");
				}
			}
			else
			{
				Type = OperationSourceType.Constant;
				if (tok.Type != TokenType.Number)
					throw new Exception("Expected either an argument or a number!");
				Constant = (byte)tok.NumberValue.Value;
			}
		}
			public BitRange(Token[] toks, ref int curIdx)
			{
				Token tok;
				tok = toks[curIdx];
				if (tok.Type != TokenType.Number)
					throw new Exception("Expected a number for the bit range!");
				EndIdx = tok.NumberValue.Value;
				curIdx++;

				tok = toks[curIdx];
				if (tok.Type == TokenType.Dot)
				{
					curIdx++;

					tok = toks[curIdx];
					if (tok.Type != TokenType.Dot)
						throw new Exception("Expected a second dot for the elips statement!");
					curIdx++;

					tok = toks[curIdx];
					if (tok.Type != TokenType.Number)
						throw new Exception("Expected a number for the start of the bit range!");
					StartIdx = tok.NumberValue.Value;
					curIdx++;

					tok = toks[curIdx];
				}
				else
				{
					StartIdx = 0;
					EndIdx--;
				}

				if (tok.Type != TokenType.RSqBracket)
					throw new Exception("Expected the closing of the square bracket!");
				curIdx++;
			}
		public WriteOperation(Token[] toks, InstructionForm ParentForm)
		{
			// As a note, all operations should be 4 characters,
			// if they are unable to be 4 characters, then they
			// should be sets of 4 characters seperated in
			// some way. (this allows for the write operations to
			// be aligned in the cpud file)
			Token tok = toks[0];
			switch (toks.Length)
			{
				case 1:
					if (tok.Type == TokenType.Number)
					{
						this.Type = WriteOperationType.Byte;
						this.ByteValue = (byte)tok.NumberValue.Value;
					}
					else if (tok.Value.StartsWith("arg"))
					{
						this.ArgIdx = (byte)(Utils.SingleDigitParse(tok.Value[3]) - 1);
						if (!ParentForm[ArgIdx].ArgType.HasSize)
						{
							this.Type = WriteOperationType.Arg;
						}
						else
						{
							switch(ParentForm[ArgIdx].ArgType.Size)
							{
								case 1:
									this.Type = WriteOperationType.Imm8;
									break;
								case 2:
									this.Type = WriteOperationType.Imm16;
									break;
								case 4:
									this.Type = WriteOperationType.Imm32;
									break;
								default:
									throw new Exception("Unknown arg type!");
							}
						}
					}
					else // Prefix
					{
						this.Type = WriteOperationType.Prefix;
						this.SelectedPrefix = PrefixRegistry.GetPrefixName(tok.Value);
						if (this.SelectedPrefix == null)
							throw new Exception("Unknown write operation '" + tok.Value + "'!");
					}
					break;
				case 3:
					this.Type = WriteOperationType.BytePlusArg;
					this.ArgIdx = (byte)(Utils.SingleDigitParse(toks[2].Value[3]) - 1);
					this.ByteValue = (byte)tok.NumberValue.Value;
					break;
				case 4:
					if (tok.Value == "evil")
					{
						if (toks[1].Type != TokenType.LSqBracket)
							throw new Exception("Expected an opening square bracket before the exception's message!");
						if (toks[3].Type != TokenType.RSqBracket)
							throw new Exception("Expected the closing square bracket after the exception's message!");
						this.Type = WriteOperationType.Throw;
						this.MessageThrown = toks[2].Value;
					}
					else if (tok.Value.StartsWith("arg"))
					{
						if (toks[1].Type != TokenType.LSqBracket)
							throw new Exception("Expected an opening square bracket before the argument write operation!");
						if (toks[3].Type != TokenType.RSqBracket)
							throw new Exception("Expected the closing square bracket after the argument write operation!");
						this.ArgIdx = (byte)(Utils.SingleDigitParse(tok.Value[3]) - 1);
						this.Type = WriteOperationType.Arg;
						this.WriteArguments.Add(new WriteOperationArgument(toks[2]));
					}
					else
					{
						throw new Exception("Unknown write operation type!");
					}
					break;
				default:
					if (tok.Value.StartsWith("arg"))
					{
						if (toks[1].Type != TokenType.LSqBracket)
							throw new Exception("Expected an opening square bracket before the argument write operation!");
						this.Type = WriteOperationType.Arg;
						this.ArgIdx = (byte)(Utils.SingleDigitParse(tok.Value[3]) - 1);
						for (int i = 2; i < toks.Length - 1; i += 2)
						{
							this.WriteArguments.Add(new WriteOperationArgument(toks[i]));
							if (i + 1 < toks.Length - 1 && toks[i + 1].Type != TokenType.Comma)
								throw new Exception("Expected a comma!");
						}
						if (toks[toks.Length - 1].Type != TokenType.RSqBracket)
							throw new Exception("Expected the closing square bracket after the argument write operation!");
					}
					else
					{
						throw new Exception("Unknown number of tokens passed in!");
					}
					break;
			}
		}
Beispiel #4
0
		private static void DumpTokens(Token[] toks)
		{
			StreamWriter rtr = new StreamWriter("TokenDump.txt", false);
			rtr.WriteLine("Writing a total of " + toks.Length.ToString() + " tokens.");
			rtr.WriteLine();
			for (int i = 0; i < toks.Length; i++)
			{
				rtr.WriteLine(toks[i].ToString());
			}
			rtr.Flush();
			rtr.Close();
		}
			public BitPatternInvokeArgumentEntry(BitPattern parent, Token[] toks, ref int curIdx)
			{
				Token tok;
				tok = toks[curIdx];
				if (tok.Type != TokenType.Identifier)
					throw new Exception("Expected an identifier for the name of the argument to map to!");
				Name = tok.Value;
				curIdx++;

				tok = toks[curIdx];
				if (tok.Type != TokenType.Equal)
					throw new Exception("Expected an equals sign before the value to pass for the argument!");
				curIdx++;

				ArgValue = new BitPatternPieceOperation(parent, toks, ref curIdx);
			}
		public BitPattern(Token[] toks, ref int curIdx)
		{
			Token tok;
			tok = toks[curIdx];
			if (tok.Type != TokenType.Identifier)
				throw new Exception("Expected an identifier for the name of the bit pattern!");
			this.Name = tok.Value;
			curIdx++;

			tok = toks[curIdx];
			while (tok.Type == TokenType.LParen)
			{
				curIdx++;
				BitPatternPiece pc = new BitPatternPiece(this, toks, ref curIdx);
				TotalLength += pc.TotalLength;
				Pieces.Add(pc);
				tok = toks[curIdx];
			}

			if (tok.Type != TokenType.EOL)
				throw new Exception("Expected an EOL at the end of the bit pattern!");
			curIdx++;
		}
		public BitPatternPiece(BitPattern parent, Token[] toks, ref int curIdx)
		{
			Token tok;
			tok = toks[curIdx];
			while (tok.Type != TokenType.RParen)
			{
				var bpp = new BitPatternPieceOperation(parent, toks, ref curIdx);
				TotalLength += bpp.Length;
				Operations.Add(bpp);
				tok = toks[curIdx];
			}
			curIdx++;
		}
		public BitPatternPieceOperation(BitPattern parent, Token[] toks, ref int curIdx)
		{
			Parent = parent;
			Token tok;
			tok = toks[curIdx];
			if (tok.Type == TokenType.Identifier)
			{
				OpType = OperationType.Arg;
				if (!int.TryParse(tok.Value.Substring(1), out ArgIdx))
					throw new Exception("Unknown argument '" + tok.Value + "'!");
				curIdx++;
				tok = toks[curIdx];
				if (tok.Type != TokenType.LSqBracket)
					throw new Exception("Expected the opening square bracket for the bit pattern operation selector!");
				curIdx++;
				Range = new BitRange(toks, ref curIdx);
				parent.RequestArg(ArgIdx, Range.EndIdx);
			}
			else if (tok.Type == TokenType.Number)
			{
				OpType = OperationType.Literal;
				Literal = tok.NumberValue.Value;
				Number n = tok.NumberValue;
				curIdx++;

				tok = toks[curIdx];
				if (n.Format == NumberFormat.Binary)
				{
					if (tok.Type != TokenType.LSqBracket)
					{
						Range = new BitRange(0, n.LiteralLength - 1);
					}
					else
					{
						curIdx++;
						Range = new BitRange(toks, ref curIdx);
					}
				}
				else if (tok.Type != TokenType.LSqBracket)
				{
					Range = new BitRange(0, Utils.GetHighestBitIndexSet(Literal));
				}
				else
				{
					curIdx++;
					Range = new BitRange(toks, ref curIdx);
				}
			}
			else if (tok.Type == TokenType.LParen)
			{
				curIdx++;
				OpType = OperationType.BinaryOp;

				tok = toks[curIdx];
				switch(tok.Type)
				{
					case TokenType.Star:
						BinaryOp = CodeBinaryOperatorType.Multiply;
						break;
					case TokenType.Plus:
						BinaryOp = CodeBinaryOperatorType.Add;
						break;
					default:
						throw new Exception("Unknown math operation to perform!");
				}
				curIdx++;

				LHand = new BitPatternPieceOperation(parent, toks, ref curIdx);
				RHand = new BitPatternPieceOperation(parent, toks, ref curIdx);

				tok = toks[curIdx];
				if (tok.Type != TokenType.RParen)
					throw new Exception("Expected a closing parenthesis!");
				curIdx++;

				tok = toks[curIdx];
				if (tok.Type != TokenType.LSqBracket)
				{
					Range = new BitRange(0, 32);
				}
				else
				{
					curIdx++;
					Range = new BitRange(toks, ref curIdx);
				}
			}
			else if (tok.Type == TokenType.Cash)
			{
				InvokedPatternArgMapping = new List<BitPatternInvokeArgumentEntry>();
				OpType = OperationType.PatternInvoke;
				curIdx++;

				tok = toks[curIdx];
				if (tok.Type != TokenType.Identifier)
					throw new Exception("Expected an identifier for the bit pattern to invoke!");
				InvokedPattern = BitPatternRegistry.GetPattern(tok.Value);
				curIdx++;

				tok = toks[curIdx];
				if (tok.Type != TokenType.LSqBracket)
					throw new Exception("Expected a left square bracket for the arguments to the bit operation!");
				curIdx++;

				tok = toks[curIdx];
				bool expectsArg = true;
				bool first = true;
				while(tok.Type != TokenType.RSqBracket)
				{
					if (!expectsArg)
						throw new Exception("Expected a closing square bracket for the arguments to the bit operation!");
					InvokedPatternArgMapping.Add(new BitPatternInvokeArgumentEntry(parent, toks, ref curIdx));

					tok = toks[curIdx];
					if (tok.Type == TokenType.Comma)
					{
						curIdx++;
						tok = toks[curIdx];
					}
					else
					{
						expectsArg = false;
					}
					first = false;
				}
				if (expectsArg && !first)
					throw new Exception("Expected another argument!");
				curIdx++;
			}
			else
			{
				throw new Exception("Unsupported bit pattern operation!");
			}
		}
		public CustomArgOperation(InstructionArgType parent, Token[] tokens)
		{
			this.toks = tokens;
			this.ParentArg = parent;
		}