コード例 #1
0
ファイル: ExpressionParser.cs プロジェクト: scbrady/lynicon
        public Tuple <OperatorDefinition, int> GetTopOperator(IEnumerable <Token> tokens, List <OperatorDefinition> ops)
        {
            int pos          = 0;
            int winningPos   = -1;
            int bracketDepth = 0;
            OperatorDefinition winningOpDef = null;
            OperatorDefinition opDef        = null;

            foreach (Token t in tokens)
            {
                opDef = ops.FirstOrDefault(op => op.Op == t.ToString().ToLower());
                if (OpenBrackets.Contains(t.ToString()))
                {
                    bracketDepth++;
                }
                else if (CloseBrackets.Contains(t.ToString()))
                {
                    bracketDepth--;
                }
                if (bracketDepth < 0)
                {
                    break;
                }
                if (opDef != null && (winningOpDef == null || opDef.Precedence >= winningOpDef.Precedence))
                {
                    winningOpDef = opDef;
                    winningPos   = pos;
                }
                pos++;
            }

            return(new Tuple <OperatorDefinition, int>(winningOpDef, winningPos));
        }
コード例 #2
0
        // builds node - constants, unary operators and binary operators
        private List <ISyntaxNode> BuildListSyntaxNode(string function)
        {
            List <ISyntaxNode> listNode = new List <ISyntaxNode>();

            for (int i = 0; i < function.Length; i++)
            {
                if (function[i] == 'p')
                {
                    ISyntaxNode pi = new Var(Math.PI);
                    listNode.Add(pi);
                    i++;
                }

                if (function[i] == 'e')
                {
                    ISyntaxNode e = new Var(Math.E);
                    listNode.Add(e);
                }

                if (function[i] == 'x')
                {
                    ISyntaxNode x = new X();
                    listNode.Add(x);
                }
                if (Char.IsNumber(function[i]))
                {
                    ISyntaxNode var = ReadVar(function, i, out i);
                    listNode.Add(var);
                }
                if (function[i] == '-')
                {
                    if (i == 0 || function[i - 1] == '(' || function[i + 1] == '(')
                    {
                        ISyntaxNode op = new Neg();
                        listNode.Add(op);
                        continue;
                    }
                }
                if (MyChar.IsBinaryOperation(function[i]))
                {
                    ISyntaxNode op = ReadOperation(function, i, out i);
                    listNode.Add(op);
                }
                if (function[i] == '(')
                {
                    ISyntaxNode op = new OpenBrackets();
                    listNode.Add(op);
                }
                if (function[i] == ')')
                {
                    ISyntaxNode op = new CloseBrackets();
                    listNode.Add(op);
                }
                if (function[i] == 's' || function[i] == 'c' || function[i] == 't' ||
                    function[i] == 'l' || function[i] == 'a')
                {
                    ISyntaxNode func = ReadFunction(function, i, out i);
                    listNode.Add(func);
                }
            }
            return(listNode);
        }