コード例 #1
0
        /// <summary>
        /// Take each token string from the list of postfixedtokens and build a parse tree
        /// with each node evaluated when possible.
        /// </summary>
        internal void AppendFunction(IToken token)
        {
            int           numberOfChildLeafs = 0;
            FunctionToken functToken         = null;

            switch (token.Type)
            {
            case TokenType.function:
                functToken         = (FunctionToken)token;
                numberOfChildLeafs = functToken.numberOfChildren;
                break;

            case TokenType.infixOperator:
                numberOfChildLeafs = 2;
                break;

            case TokenType.suffixOperator:
                numberOfChildLeafs = 1;
                break;
            }
            //TODO: In the parser you must keep count of the amount of children and pass that number here
            //also have restrictions in certain cases
            string   tokenString = token.TokenString;
            TreeNode child       = new TreeNode();

            child.type = nodeType.operation;
            child.name = tokenString;
            TreeNode[] childLeafNodes = new TreeNode[numberOfChildLeafs];
            for (int i = 0; i < numberOfChildLeafs; i++)
            {
                if (children.Count() == 0)
                {
                    throw new Exception("Post fixed token notation error.");
                }
                childLeafNodes[i] = children[0];
                children.RemoveAt(0);
                child.children.Insert(0, childLeafNodes[i]);
            }
            if (childLeafNodes.All(i => i.numericalEvaluation))
            {
                child.numericalEvaluation = true;
                List <Complex> paramaters = childLeafNodes.Select(i => i.val).ToList();
                if (token.Type == TokenType.suffixOperator || token.Type == TokenType.infixOperator)
                {
                    child.val = postFixedOperatorEvaluator(paramaters, tokenString);
                }
                else if (token.Type == TokenType.function)
                {
                    //TODO: You should be evaluating parse trees not Complex values
                    child.val = functToken.Function.Compute(paramaters);
                }
                else
                {
                    throw new Exception("Not operator or function can't evaluate");
                }
            }
            flattenTieredAddOrMult(child);
            children.Insert(0, child);
        }
コード例 #2
0
ファイル: Tokenizer.cs プロジェクト: Amichai/PhysicsPad
            public IToken PublishCurrentTokenString()
            {
                IToken tokenToReturn = null;

                switch (currentStringTokenType)
                {
                case TokenType.number:
                    tokenToReturn = new NumberToken(tokenString);
                    break;

                case TokenType.infixOperator:
                    tokenToReturn = new OperatorToken(tokenString, TokenType.infixOperator);
                    break;

                case TokenType.suffixOperator:
                    tokenToReturn = new OperatorToken(tokenString, TokenType.suffixOperator);
                    break;

                case TokenType.charString:
                    tokenString = tokenString.ToUpper();
                    if (FunctionToken.Library.Contains(tokenString))
                    {
                        tokenToReturn = new FunctionToken(tokenString);
                    }
                    else if (VariableToken.VariableLibrary.Contains(tokenString))
                    {
                        tokenToReturn = new VariableToken(tokenString);
                    }
                    else if (UnitToken.Units.Contains(tokenString))
                    {
                        tokenToReturn = new UnitToken(tokenString);
                    }
                    else if (KeywordToken.Keywords.Contains(tokenString))
                    {
                        tokenToReturn = new KeywordToken(tokenString);
                    }
                    break;

                case TokenType.openBrace:
                    tokenToReturn = new SyntaxToken(tokenString, TokenType.openBrace);
                    break;

                case TokenType.closedBrace:
                    tokenToReturn = new SyntaxToken(tokenString, TokenType.closedBrace);
                    break;

                case TokenType.argSeperator:
                    tokenToReturn = new SyntaxToken(tokenString, TokenType.argSeperator);
                    break;
                }
                return(tokenToReturn);
            }
コード例 #3
0
ファイル: Tokenizer.cs プロジェクト: Amichai/PhysicsPad
 public IToken PublishCurrentTokenString()
 {
     IToken tokenToReturn = null ;
     switch (currentStringTokenType) {
     case TokenType.number:
         tokenToReturn = new NumberToken(tokenString);
         break;
     case TokenType.infixOperator:
         tokenToReturn = new OperatorToken(tokenString, TokenType.infixOperator);
         break;
     case TokenType.suffixOperator:
         tokenToReturn = new OperatorToken(tokenString, TokenType.suffixOperator);
         break;
     case TokenType.charString:
         tokenString = tokenString.ToUpper();
         if (FunctionToken.Library.Contains(tokenString)) {
             tokenToReturn = new FunctionToken(tokenString);
         } else if (VariableToken.VariableLibrary.Contains(tokenString)) {
             tokenToReturn = new VariableToken(tokenString);
         } else if(UnitToken.Units.Contains(tokenString)){
             tokenToReturn = new UnitToken(tokenString);
         } else if (KeywordToken.Keywords.Contains(tokenString)) {
             tokenToReturn = new KeywordToken(tokenString);
         }
         break;
         case TokenType.openBrace:
         tokenToReturn = new SyntaxToken(tokenString, TokenType.openBrace);
         break;
         case TokenType.closedBrace:
         tokenToReturn = new SyntaxToken(tokenString, TokenType.closedBrace);
         break;
         case TokenType.argSeperator:
         tokenToReturn = new SyntaxToken(tokenString, TokenType.argSeperator);
         break;
     }
     return tokenToReturn;
 }
コード例 #4
0
        public PostfixedTokens(List <IToken> inputTokens)
        {
            foreach (IToken token in inputTokens)
            {
                switch (token.Type)
                {
                case TokenType.keyword:
                    InAList.Add(token);
                    break;

                case TokenType.number:
                    InAList.Add(token);
                    break;

                case TokenType.variable:
                    InAList.Add(token);
                    break;

                case TokenType.function:
                    operatorStack.Push(token);
                    numberOfFunctionParameters.Add(0);
                    break;

                case TokenType.argSeperator:
                    if (operatorStack.Count() == 0)
                    {
                        ErrorLog.Add(new ErrorMessage(token.TokenString + " operator syntax error."));
                    }
                    else
                    {
                        if (numberOfFunctionParameters.Count > 0)
                        {
                            numberOfFunctionParameters[numberOfFunctionParameters.Count - 1] = numberOfFunctionParameters.Last() + 1;
                        }
                        while (operatorStack.First().Type != TokenType.openBrace)
                        {
                            InAList.Add(operatorStack.Pop());
                        }
                    }
                    break;

                case TokenType.infixOperator:
                    handleOperator(token);
                    break;

                case TokenType.suffixOperator:
                    InAList.Add(token);
                    break;

                case TokenType.openBrace:
                    operatorStack.Push(token);
                    break;

                case TokenType.closedBrace:
                    if (numberOfFunctionParameters.Count() > 0)
                    {
                        numberOfFunctionParameters[numberOfFunctionParameters.Count - 1] = numberOfFunctionParameters.Last() + 1;
                    }
                    while (operatorStack.First().Type != TokenType.openBrace)
                    {
                        if (operatorStack.Count() == 0)
                        {
                            ErrorLog.Add(new ErrorMessage("mismatched parenthesis error"));
                        }
                        InAList.Add(operatorStack.Pop());
                    }
                    operatorStack.Pop();                             //Pop the left parenthesis off the stack
                    if (operatorStack.Count > 0 && operatorStack.First().Type == TokenType.function)
                    {
                        FunctionToken tokenToAdd = (FunctionToken)operatorStack.Pop();
                        tokenToAdd.numberOfChildren = numberOfFunctionParameters.Last();
                        numberOfFunctionParameters.RemoveAt(numberOfFunctionParameters.Count() - 1);
                        InAList.Add(tokenToAdd);
                    }
                    break;

                default:
                    throw new Exception("Unknown token type");
                }
            }
            while (operatorStack.Count() > 0)
            {
                InAList.Add(operatorStack.Pop());
            }
            //TODO: Handle mismatched bracket exception
        }