private Signal ScanSignalExpression() { /* This method is kind of a postfix machine, parsing infix expressions to postfix expressions (having regard to precedence) using the operator stack and evaluates the postfix term to an expression tree using the expression stack. */ SignalStack expressionStack = new SignalStack(4, 4096); EntityStack operatorStack = new EntityStack(4, 4096); expressionStack.Push(ScanOperand()); Entity lastEntity, topEntity; MathNet.Symbolics.Backend.Containers.EntityTable entityTable = context.Library.Entities; Library library = context.Library; while(true) { LexerToken ahead = tokenizer.LookaheadFistToken; if(!(ahead.IsType(TokenTypes.SymbolIdentifier) || ahead.IsType(TokenTypes.TextIdentifier))) break; if(entityTable.ContainsSymbol(ahead.Text,InfixNotation.LeftAssociativeInnerOperator)) lastEntity = library.LookupEntity(ahead.Text, InfixNotation.LeftAssociativeInnerOperator, 2, 1, 0); else if(entityTable.ContainsSymbol(ahead.Text,InfixNotation.RightAssociativeInnerOperator)) lastEntity = library.LookupEntity(ahead.Text, InfixNotation.RightAssociativeInnerOperator, 2, 1, 0); else break; tokenizer.Consume(); while(operatorStack.Count > 0 && IsPrecedenceLeftFirst(operatorStack.Peek(), lastEntity)) { topEntity = operatorStack.Pop(); Signal swap = expressionStack.Pop(); Signal s = context.Builder.Function(topEntity, expressionStack.Pop(), swap); expressionStack.Push(s); } operatorStack.Push(lastEntity); expressionStack.Push(ScanOperand()); } while(operatorStack.Count > 0) { topEntity = operatorStack.Pop(); Signal swap = expressionStack.Pop(); Signal s = context.Builder.Function(topEntity, expressionStack.Pop(), swap); expressionStack.Push(s); } Signal ret = expressionStack.Pop(); system.AddSignalTree(ret, false, false); return ret; }
private Signal ScanSignalExpression() { /* * This method is kind of a postfix machine, parsing * infix expressions to postfix expressions (having * regard to precedence) using the operator stack and * evaluates the postfix term to an expression tree * using the expression stack. */ SignalStack expressionStack = new SignalStack(4, 4096); EntityStack operatorStack = new EntityStack(4, 4096); expressionStack.Push(ScanOperand()); Entity lastEntity, topEntity; MathNet.Symbolics.Backend.Containers.EntityTable entityTable = context.Library.Entities; Library library = context.Library; while (true) { LexerToken ahead = tokenizer.LookaheadFistToken; if (!(ahead.IsType(TokenTypes.SymbolIdentifier) || ahead.IsType(TokenTypes.TextIdentifier))) { break; } if (entityTable.ContainsSymbol(ahead.Text, InfixNotation.LeftAssociativeInnerOperator)) { lastEntity = library.LookupEntity(ahead.Text, InfixNotation.LeftAssociativeInnerOperator, 2, 1, 0); } else if (entityTable.ContainsSymbol(ahead.Text, InfixNotation.RightAssociativeInnerOperator)) { lastEntity = library.LookupEntity(ahead.Text, InfixNotation.RightAssociativeInnerOperator, 2, 1, 0); } else { break; } tokenizer.Consume(); while (operatorStack.Count > 0 && IsPrecedenceLeftFirst(operatorStack.Peek(), lastEntity)) { topEntity = operatorStack.Pop(); Signal swap = expressionStack.Pop(); Signal s = context.Builder.Function(topEntity, expressionStack.Pop(), swap); expressionStack.Push(s); } operatorStack.Push(lastEntity); expressionStack.Push(ScanOperand()); } while (operatorStack.Count > 0) { topEntity = operatorStack.Pop(); Signal swap = expressionStack.Pop(); Signal s = context.Builder.Function(topEntity, expressionStack.Pop(), swap); expressionStack.Push(s); } Signal ret = expressionStack.Pop(); system.AddSignalTree(ret, false, false); return(ret); }