public Insert ( |
||
Node | ||
Left | ||
Right | ||
Résultat | void |
public static Value CreateValueFromExpression( TokenList List ) { if ( List.First == null ) return new Array(); //Bracket pass var startNode = new TokenListNode(); int parCount = 0; for ( TokenListNode node = List.First ; node != null ; node = node.Next ) { if ( node.Value is ParenthesisOpen ) { if ( parCount == 0 ) { startNode = node; } parCount++; } else if ( node.Value is ParenthesisClosed ) { parCount--; if ( parCount == 0 ) { var group = new TokenList(); for ( TokenListNode innerNode = startNode.Next ; innerNode != node ; innerNode = innerNode.Next ) { group.Add( innerNode.Value ); } Value val = CreateValueFromExpression( group ); List.Insert( new TokenListNode( val ), startNode.Previous, node.Next ); } } } //Operator pass for ( int i = 1 ; i <= Operators.LowestPrecedance ; ++i ) { for ( TokenListNode node = List.First ; node != null ; node = node.Next ) { if ( node.Value is Operator ) { var oper = (Operator)node.Value; OperatorType type = oper.Type; if ( oper.Precedance == i ) { var temp = new TokenList(); TokenListNode left = new TokenListNode(), right = new TokenListNode(); if ( oper is DotOperator ) type = OperatorType.InfixBinary; switch ( type ) //Using the operators type, determines which tokens to make a value from { case OperatorType.InfixBinary: //A + B temp.Add( node.Previous.Value ); temp.Add( node.Value ); temp.Add( node.Next.Value ); left = node.Previous.Previous; right = node.Next.Next; break; case OperatorType.PrefixBinary: //+ A B temp.Add( node.Next.Value ); temp.Add( node.Value ); temp.Add( node.Next.Next.Value ); left = node.Previous; right = node.Next.Next.Next; break; case OperatorType.PrefixUnary: //+ A temp.Add( node.Value ); temp.Add( node.Next.Value ); left = node.Previous; right = node.Next.Next; break; case OperatorType.SufixBinary: //A B + temp.Add( node.Previous.Previous.Value ); temp.Add( node.Value ); temp.Add( node.Previous.Value ); left = node.Previous.Previous.Previous; right = node.Next; break; case OperatorType.SufixUnary: //A + temp.Add( node.Value ); temp.Add( node.Previous.Value ); left = node.Previous.Previous; right = node.Next; break; } Value val = MakeValue( temp, ( type == OperatorType.PrefixUnary || type == OperatorType.SufixUnary ) ); List.Insert( new TokenListNode( val ), left, right ); } } } } if ( List.First.Next != null ) { return new ExpressionSequence( List ); } return (Value)List.First.Value; }
public static Value CreateValueFromExpression(TokenList List) { if (List.First == null) { return(new Array()); } //Bracket pass var startNode = new TokenListNode(); int parCount = 0; for (TokenListNode node = List.First; node != null; node = node.Next) { if (node.Value is ParenthesisOpen) { if (parCount == 0) { startNode = node; } parCount++; } else if (node.Value is ParenthesisClosed) { parCount--; if (parCount == 0) { var group = new TokenList(); for (TokenListNode innerNode = startNode.Next; innerNode != node; innerNode = innerNode.Next) { group.Add(innerNode.Value); } Value val = CreateValueFromExpression(group); List.Insert(new TokenListNode(val), startNode.Previous, node.Next); } } } //Operator pass for (int i = 1; i <= Operators.LowestPrecedance; ++i) { for (TokenListNode node = List.First; node != null; node = node.Next) { if (node.Value is Operator) { var oper = (Operator)node.Value; OperatorType type = oper.Type; if (oper.Precedance == i) { var temp = new TokenList(); TokenListNode left = new TokenListNode(), right = new TokenListNode(); if (oper is DotOperator) { type = OperatorType.InfixBinary; } switch (type) //Using the operators type, determines which tokens to make a value from { case OperatorType.InfixBinary: //A + B temp.Add(node.Previous.Value); temp.Add(node.Value); temp.Add(node.Next.Value); left = node.Previous.Previous; right = node.Next.Next; break; case OperatorType.PrefixBinary: //+ A B temp.Add(node.Next.Value); temp.Add(node.Value); temp.Add(node.Next.Next.Value); left = node.Previous; right = node.Next.Next.Next; break; case OperatorType.PrefixUnary: //+ A temp.Add(node.Value); temp.Add(node.Next.Value); left = node.Previous; right = node.Next.Next; break; case OperatorType.SufixBinary: //A B + temp.Add(node.Previous.Previous.Value); temp.Add(node.Value); temp.Add(node.Previous.Value); left = node.Previous.Previous.Previous; right = node.Next; break; case OperatorType.SufixUnary: //A + temp.Add(node.Value); temp.Add(node.Previous.Value); left = node.Previous.Previous; right = node.Next; break; } Value val = MakeValue(temp, (type == OperatorType.PrefixUnary || type == OperatorType.SufixUnary)); List.Insert(new TokenListNode(val), left, right); } } } } if (List.First.Next != null) { return(new ExpressionSequence(List)); } return((Value)List.First.Value); }