private void handleOperator(Token token) {
			//Test precedence
			//If the current op has higher precedence, add to the stack
			//true if the last operator on the stack has precedence over the current operator
			while (operatorStack.Count() > 0 && operatorStack.First().TokenType == TokenType.arithmeticOp
				&& precedenceTest(operatorStack.First().TokenString, token.TokenString)) {
				tokens.Add(operatorStack.Pop());
			}
			operatorStack.Push(token);
		}
示例#2
0
 private void testForOperationInference(Token tokenToAdd)
 {
     if (tokens.Count() > 0) {
         //When a minus or plus sign is read as a negative number, add a plus sign before the number
         if (tokenToAdd.TokenType == TokenType.number
             && (tokens.Last().TokenType == TokenType.number || tokens.Last().TokenType == TokenType.closedBrace)
             && (tokenToAdd.TokenString[0] == '-' || tokenToAdd.TokenString[0] == '+')) {
             tokens.Add(new Token("+", TokenType.arithmeticOp));
         }
         //Infer a multiplication sign between two sets of parenthesis
         if (tokenToAdd.TokenType == TokenType.openBrace && tokens.Last().TokenType == TokenType.closedBrace) {
             tokens.Add(new Token("*", TokenType.arithmeticOp));
         }
         //Infer a multiplication sign between parenthesis and a number (that doesn't start with a minus sign)
         if (tokenToAdd.TokenType == TokenType.openBrace && tokens.Last().TokenType == TokenType.number) {
             tokens.Add(new Token("*", TokenType.arithmeticOp));
         }
         if (tokenToAdd.TokenType == TokenType.number && tokens.Last().TokenType == TokenType.closedBrace && tokenToAdd.TokenString[0] != '-') {
             tokens.Add(new Token("*", TokenType.arithmeticOp));
         }
     }
 }
示例#3
0
 public Token AddChar(currentChar c)
 {
     Token tokenToReturn  = null;
     //check if the char is legal in this context
     if (syntaxIllegalCharTypes.Contains(c.currentCharTokenType)) {
         throw new Exception("Illegal char placement");
     }
     //The char is legal. Check we if we should publish the current string
     if (!charsThatAppendToCurrentString.Contains(c.currentCharTokenType) && tokenString.Count() > 0) {
         tokenToReturn = new Token(tokenString, currentStringTokenType);
         tokenString = string.Empty;
     }
     //Append Char
     tokenString += c.val;
     //Depending on the current char type:
     //Set the local string type and other currenttoken state values
     //Set publication types and char legal types
     switch (c.currentCharTokenType) {
         case CharType.arithmeticOp:
             //Set local string type
             if (c.val == '=')
                 currentStringTokenType = TokenType.equalSign;
             else
                 currentStringTokenType = TokenType.arithmeticOp;
             //Set the value of the publication type, etc.
             //Publish on every char type
             charsThatAppendToCurrentString = new HashSet<CharType>() {};
             syntaxIllegalCharTypes = new HashSet<CharType>() { CharType.arithmeticOp, CharType.syntaxChar };
             break;
         case CharType.brace:
             if(c.val == ')')
                 currentStringTokenType = TokenType.closedBrace;
             if (c.val == '(')
                 currentStringTokenType = TokenType.openBrace;
             charsThatAppendToCurrentString = new HashSet<CharType>() {};
             syntaxIllegalCharTypes = new HashSet<CharType>() { };
             break;
         case CharType.letter:
             currentStringTokenType = TokenType.charString;
             charsThatAppendToCurrentString = new HashSet<CharType>() { CharType.letter, CharType.number };
             syntaxIllegalCharTypes = new HashSet<CharType>() { };
             break;
         case CharType.number:
             //We're not dealing with a word - we're dealing with a number or +/-
             if (currentStringTokenType != TokenType.charString) {
                 currentStringTokenType = TokenType.number;
                 charsThatAppendToCurrentString = new HashSet<CharType>() { CharType.number, CharType.letter };
                 syntaxIllegalCharTypes = new HashSet<CharType>() { };
             } else {
                 currentStringTokenType = TokenType.charString;
                 charsThatAppendToCurrentString = new HashSet<CharType>() { CharType.letter, CharType.number };
                 syntaxIllegalCharTypes = new HashSet<CharType>() { };
             }
             break;
         case CharType.plusOrMinusSign:
             currentStringTokenType = TokenType.arithmeticOp;
             charsThatAppendToCurrentString = new HashSet<CharType>() { CharType.number };
             syntaxIllegalCharTypes = new HashSet<CharType>() { CharType.arithmeticOp, CharType.syntaxChar };
             break;
         case CharType.syntaxChar:
             currentStringTokenType = TokenType.syntaxChar;
             charsThatAppendToCurrentString = new HashSet<CharType>() { };
             syntaxIllegalCharTypes = new HashSet<CharType>() { CharType.arithmeticOp, CharType.syntaxChar };
             break;
         case CharType.whitespace:
             tokenString = string.Empty;
             break;
     }
     return tokenToReturn;
 }
示例#4
0
 public void Add(Token token)
 {
     testForOperationInference(token);
     tokens.Add(token);
     allTokenStrings += token.TokenType.ToString() + ": " + token.TokenString + " \n";
 }
		public void Add(Token token) {
			allTokenStrings += token.TokenType.ToString() + ": " + token.TokenString + " \n";
			tokens.Add(token);
		}