//************************************************************ // static helpers to generate a Term //************************************************************ // try to extract the next term from the string // put the term on the stack protected static CTerm_Base GetNextTerm(ref string Term, Stack <CTerm_Base> Stack, MMC.Calc.CEnvironment Env, TPriority Prio) { CTerm_Base Result = null; int Stack_Count = Stack.Count; while (!String.IsNullOrEmpty(Term) || Result != null) { // if we didn't find a term yet if (Result == null) { // find next operation Result = Env.FindOp(ref Term); // if we didn't find anything ... if (Result == null) { break; } } // This term ends if prio is less or equal, i.e. 2-3+2 = (2-3)+2 if (Result.Priotity <= Prio) { break; } // Let the Operation get their operands from stack or string CTerm_Base Next = Result.Compile(ref Term, Stack, Env); // and try to reduce the term as much as possible TTermType Type = Result.Type; Result = Result.Replace(Env); // check if we have to combine this Term with the TopOfStack // but only if the second term is not a number // e.g. "2sqrt2" = "2*sqrt2" if ((Type != TTermType.Number) && (Type != TTermType.Seperator) && (Stack.Count == Stack_Count + 1)) { Result = Env.Combine(Stack.Pop(), Result); Result = Result.Replace(Env); } // put the Operation onto the stack Stack.Push(Result); // and proceed with the next term (or null if none) Result = Next; } return(Result); }
//------------------------------------------------------------ // construct this object with a function or variable name // e.g. "+" or "sin" or "pi" public CTerm_Base(string Name, TPriority Priority, TTermType TermType) { _Name = Name; _Priority = Priority; _TermType = TermType; }