//-------------------------------------------------------- // add or replace a definition public void Set(MMC.Calc.CTerm_Base Term) { if (Term != null) { string Name = Term.Name; _Operations[Name] = Term; if (Name.Length > _LengthMax) { _LengthMax = Name.Length; } } }
//------------------------------------------------------------ // find the longest match starting at pos in Term public MMC.Calc.CTerm_Base Get(string Term, int pos) { int len = Term.Length - pos; MMC.Calc.CTerm_Base rc = null; for (int i = (len > _LengthMax) ? _LengthMax : len; i > 0; i--) { if (_Operations.TryGetValue(Term.Substring(pos, i), out rc)) { break; } } return(rc); }
//------------------------------------------------------------ // combine two terms, e.g. "2sqrt2" = "2*sqrt2" public MMC.Calc.CTerm_Base Combine(MMC.Calc.CTerm_Base A, MMC.Calc.CTerm_Base B) { return(new MMC.Calc.Functions.CTerm_Mul("*", A, B)); }
//------------------------------------------------------------ // analyse the string and return the operation public MMC.Calc.CTerm_Base FindOp(ref string Term) { //-------------------------------------------------------- // make sure we have something to do if (Term != null) { int length = Term.Length; int idx = 0; while (idx < length) { char c = Term[idx]; if (c != ' ' && c != '\t' && c != '\n' && c != '\r') { break; } idx++; } Term = Term.Substring(idx); } if (string.IsNullOrEmpty(Term)) { return(null); } //-------------------------------------------------------- // find the match in the various operation maps MMC.Calc.CTerm_Base Op = _Operations.Get(Term, 0); if (Op != null) { Term = Term.Remove(0, Op.Name.Length); return(Op.Clone()); } Op = _Support.Get(Term, 0); if (Op != null) { Term = Term.Remove(0, Op.Name.Length); return(Op.Clone()); } Op = _Constants.Get(Term, 0); if (Op != null) { Term = Term.Remove(0, Op.Name.Length); return(Op); // Do not clone a constant } Op = _Variables.Get(Term, 0); if (Op != null) { Term = Term.Remove(0, Op.Name.Length); return(Op); // Do not clone a variable } //-------------------------------------------------------- // is it a number MMC.Numbers.CNumber N = NewNumber(0.0); if (N.FromString(ref Term)) { return(new MMC.Calc.Functions.CTerm_Number(N)); } //-------------------------------------------------------- // check for an assignment for (int pos = 1; pos < Term.Length; pos++) { Op = _Support.Get(Term, pos); if (Op != null && Op.Type == CTerm_Base.TTermType.Assignment) { MMC.Calc.Functions.CTerm_Variable Var = new MMC.Calc.Functions.CTerm_Variable(Term.Substring(0, pos)); Term = Term.Remove(0, pos); return(Var); } } //-------------------------------------------------------- // nothing found so something's wrong somewhere throw new CTermException("Unknown Function or Variable!"); }
//------------------------------------------------------------ // add a new or replace a function/variable public void SetVariable(MMC.Calc.CTerm_Base Term) { _Variables.Set(Term); }