private Pile <Reach> splitBraces(Reach formula) { while (formula.at(1, utl.dmyBool("al(occur, token) is planned"), "(").len > 0) { Reach part = braces.upon(formula); Reach lastPart = formula; while (part.startsWith("(")) { lastPart = part; part = braces.upon(part.from(2)); } if (lastPart.startsWith("(")) { terms.Add(braces.on(lastPart).Trim()); } else { terms.Add(lastPart.Trim()); } formula = formula.RPW("«" + terms.Len + "»").at(lastPart); } if (formula.startsWith("(")) { terms.Add(braces.on(formula).Trim()); } else { terms.Add(formula.Trim()); } return(terms); }
private Reach extract(Reach source, bool includeTokens) { Reach ret = source.upto(0) + source.from(source.len + 1); // FromLeftToRight strategy Reach ret = source.from(source.len + 1) for (long i = 1; i <= def.Length; i++) { long k = 1; while ((k <= rpt[i - 1]) || ((rpt[i - 1] == 0) && (source.len > 0))) { Reach leadIn = source.at(1, utl.dmyBool("al(occur, token) is planned"), def[i - 1].Name); if ((leadIn.len == 0) && (def[i - 1].Name.Length > 0)) { return(ret); } if (includeTokens) { if (ret.len == 0) { ret = leadIn; } else { ret = ret + leadIn; } } Reach res = source.after(leadIn); Reach leadOut = null; int occur = 0; // first we try the first closing bracelet, only in case that this will be a non-matching bracelet then wee will try the second, third, fourth etc. closing closing bracelet do { occur++; for (int j = (def[i - 1]).Len; j > 0; j--) { Reach token = res.at(occur, utl.dmyBool("al(occur, token) is planned"), (def[i - 1])[j]); if (token.len > 0) { leadOut = token; } } } while (nested[i - 1] && (res.upto(leadOut).at(-occur, utl.dmyBool("al(occur, token) is planned"), leadIn.text).len > 0)); if (leadOut != null) { res = res.before(leadOut); } if (leadOut == null) { return((ret.len == 0) ? res : ret + res); } if (ret.len == 0) { ret = includeTokens ? source.after(leadIn).upto(leadOut) : source.after(leadIn).before(leadOut); } else { ret = includeTokens ? ret + source.after(leadIn).upto(leadOut) : ret + source.after(leadIn).before(leadOut); } source = source.after(leadOut); k++; } } return(ret); }
private static void selfTest() { selfTested = true; Reach r1 = new Reach("ab"); Reach r2 = new Reach("..X--XX___XX,,XX"); Reach r3 = new Reach("aa bb cc "); ass(r3.firstWord(" ").equals("")); ass(r3.firstWord(" a").equals("aa ")); ass(r3.lastWord(" ").equals(" ")); ass(r2.upto(1, false, "X,", "X_", "X-").equals("..X-")); ass(r2.upto(2, false, "X,", "X_", "X-").equals("..X--XX_")); ass(r2.upto(3, false, "X,", "X_", "X-").equals("..X--XX___XX,")); ass(r2.upto(-1, false, "X,", "X_", "X-").equals("..X--XX___XX,")); ass(r2.upto(-2, false, "X,", "X_", "X-").equals("..X--XX_")); ass(r2.upto(-3, false, "X,", "X_", "X-").equals("..X-")); ass(r2.upto(1, false, ",XX", "_XX", "X-", "..").equals("..")); ass(r2.upto(2, false, ",XX", "_XX", "X-", "..").equals("..X-")); ass(r2.upto(3, false, ",XX", "_XX", "X-", "..").equals("..X--XX___XX")); ass(r2.upto(-1, false, ",XX", "_XX", "X-", "..").equals("..X--XX___XX,,XX")); ass(r2.upto(-2, false, ",XX", "_XX", "X-", "..").equals("..X--XX___XX")); ass(r2.upto(-3, false, ",XX", "_XX", "X-", "..").equals("..X-")); ass(r2.from(1, false, "X,", "X_", "X-").equals("X--XX___XX,,XX")); ass(r2.from(2, false, "X,", "X_", "X-").equals("X___XX,,XX")); ass(r2.from(3, false, "X,", "X_", "X-").equals("X,,XX")); ass(r2.from(-1, false, "X,", "X_", "X-").equals("X,,XX")); ass(r2.from(-2, false, "X,", "X_", "X-").equals("X___XX,,XX")); ass(r2.from(-3, false, "X,", "X_", "X-").equals("X--XX___XX,,XX")); ass(r2.from(1, false, ",XX", "_XX", "X-", "..").equals("..X--XX___XX,,XX")); ass(r2.from(2, false, ",XX", "_XX", "X-", "..").equals("X--XX___XX,,XX")); ass(r2.from(3, false, ",XX", "_XX", "X-", "..").equals("_XX,,XX")); ass(r2.from(-1, false, ",XX", "_XX", "X-", "..").equals(",XX")); ass(r2.from(-2, false, ",XX", "_XX", "X-", "..").equals("_XX,,XX")); ass(r2.from(-3, false, ",XX", "_XX", "X-", "..").equals("X--XX___XX,,XX")); ass(r1.upto(1).equals("a")); ass(r1.before(1).equals("")); ass(r1.from(1).equals("ab")); ass(r1.after(1).equals("b")); ass(r1.upto(-1).equals("ab")); ass(r1.before(-1).equals("a")); ass(r1.from(-1).equals("b")); ass(r1.after(-1).equals("")); ass(r2.upto(2, true, "XX").equals("..X--XX___XX")); ass(r2.upto(2, true, "X").equals("..X--X")); ass(r2.upto(2, true, "XX", "X").equals("..X--X")); ass(r2.upto(2, true, "X", "XX").equals("..X--X")); ass(r2.upto(-2, true, "XX").equals("..X--XX___XX")); ass(r2.upto(-2, true, "X").equals("..X--XX___XX,,X")); ass(r2.upto(-2, true, "XX", "X").equals("..X--XX___XX,,X")); ass(r2.upto(-2, true, "X", "XX").equals("..X--XX___XX,,X")); ass(r2.from(2, true, "XX").equals("XX,,XX")); ass(r2.from(2, true, "X").equals("XX___XX,,XX")); ass(r2.from(2, true, "XX", "X").equals("XX___XX,,XX")); ass(r2.from(2, true, "X", "XX").equals("XX___XX,,XX")); ass(r2.from(-2, true, "XX").equals("XX,,XX")); ass(r2.from(-2, true, "X").equals("XX")); ass(r2.from(-2, true, "XX", "X").equals("XX")); ass(r2.from(-2, true, "X", "XX").equals("XX")); ass(r2.before(2, true, "XX").equals("..X--XX___")); ass(r2.before(2, true, "X").equals("..X--")); ass(r2.before(2, true, "XX", "X").equals("..X--")); ass(r2.before(2, true, "X", "XX").equals("..X--")); ass(r2.before(-2, true, "XX").equals("..X--XX___")); ass(r2.before(-2, true, "X").equals("..X--XX___XX,,")); ass(r2.before(-2, true, "XX", "X").equals("..X--XX___XX,,")); ass(r2.before(-2, true, "X", "XX").equals("..X--XX___XX,,")); ass(r2.after(2, true, "XX").equals(",,XX")); ass(r2.after(2, true, "X").equals("X___XX,,XX")); ass(r2.after(2, true, "XX", "X").equals("X___XX,,XX")); ass(r2.after(2, true, "X", "XX").equals("X___XX,,XX")); ass(r2.after(-2, true, "XX").equals(",,XX")); ass(r2.after(-2, true, "X").equals("X")); ass(r2.after(-2, true, "XX", "X").equals("X")); ass(r2.after(-2, true, "X", "XX").equals("X")); ass(r2.at(2, true, "XX").equals("XX")); ass(r2.at(2, true, "X").equals("X")); ass(r2.at(2, true, "XX", "X").equals("X")); ass(r2.at(2, true, "X", "XX").equals("X")); ass(r2.at(-2, true, "XX").equals("XX")); ass(r2.at(-2, true, "X").equals("X")); ass(r2.at(-2, true, "XX", "X").equals("X")); ass(r2.at(-2, true, "X", "XX").equals("X")); }
public Operator at(Reach other) { if (Part == null) { Part = Base; } return(new Operator(Base, Part.at(other))); }
private double eval(Reach term, KeyPile <string, Reach> values) { term = term.Trim(); if (term.startsWith("+")) { return(eval(term.from(2), values)); } if (term.startsWith("-")) { return(-eval(term.from(2), values)); } if (term.startsWith("!")) { return(not(eval(term.from(2), values))); } if (term.startsWith("«")) { return(eval(Int32.Parse(term.from(2).before(1, utl.dmyBool("al(occur, token) is planned"), "»").text), values)); } if (term.startsWith(true, numCharacters)) { return(parseDouble(term)); } if (term.at(1, utl.dmyBool("al(occur, token) is planned"), "«").len == 0) { return(parseDouble(values[term.text])); } Reach function = term.XTR().before(1, utl.dmyBool("al(occur, token) is planned"), "«"); double ret = eval(Int32.Parse(term.from(2).before(1, utl.dmyBool("al(occur, token) is planned"), "»").text), values); if (function.equals("abs")) { return(Math.Abs(ret)); } if (function.equals("sign")) { return(Math.Sign(ret)); } if (function.equals("round")) { return(Math.Round(ret)); } if (function.equals("ceiling")) { return(Math.Ceiling(ret)); } if (function.equals("floor")) { return(Math.Floor(ret)); } if (function.equals("sqrt")) { return(Math.Sqrt(ret)); } if (function.equals("exp")) { return(Math.Exp(ret)); } if (function.equals("log")) { return(Math.Log(ret)); } if (function.equals("sin")) { return(Math.Sin(ret)); } if (function.equals("asin")) { return(Math.Asin(ret)); } if (function.equals("cos")) { return(Math.Cos(ret)); } if (function.equals("acos")) { return(Math.Acos(ret)); } if (function.equals("tan")) { return(Math.Tan(ret)); } if (function.equals("atan")) { return(Math.Atan(ret)); } if (function.equals("sinh")) { return(Math.Sinh(ret)); } if (function.equals("cosh")) { return(Math.Cosh(ret)); } if (function.equals("tanh")) { return(Math.Tanh(ret)); } return(0); }
private string format(Reach term, KeyPile <string, Reach> values) { term = term.Trim(); if (term.startsWith("+")) { return(format(term.from(2), values)); } if (term.startsWith("-")) { return("-" + format(term.from(2), values)); } if (term.startsWith("!")) { return("!" + format(term.from(2), values)); } if (term.startsWith("«")) { return(format(Int32.Parse(term.from(2).before(1, utl.dmyBool("al(occur, token) is planned"), "»").text), values)); } if (term.startsWith(true, numCharacters)) { return("" + parseDouble(term)); } if (term.at(1, utl.dmyBool("al(occur, token) is planned"), "«").len == 0) { try { return("" + parseDouble(values[term.text])); } catch (Exception ex) { return(term.text.Trim()); } } Reach function = term.XTR().before(1, utl.dmyBool("al(occur, token) is planned"), "«"); string ret = format(Int32.Parse(term.from(2).before(1, utl.dmyBool("al(occur, token) is planned"), "»").text), values); if (function.equals("abs")) { return("abs(" + ret + ")"); } if (function.equals("sign")) { return("sign(" + ret + ")"); } if (function.equals("round")) { return("round(" + ret + ")"); } if (function.equals("ceiling")) { return("ceiling(" + ret + ")"); } if (function.equals("floor")) { return("floor(" + ret + ")"); } if (function.equals("sqrt")) { return("sqrt(" + ret + ")"); } if (function.equals("exp")) { return("exp(" + ret + ")"); } if (function.equals("log")) { return("log(" + ret + ")"); } if (function.equals("sin")) { return("sin(" + ret + ")"); } if (function.equals("asin")) { return("asin(" + ret + ")"); } if (function.equals("cos")) { return("cos(" + ret + ")"); } if (function.equals("acos")) { return("acos(" + ret + ")"); } if (function.equals("tan")) { return("tan(" + ret + ")"); } if (function.equals("atan")) { return("atan(" + ret + ")"); } if (function.equals("sinh")) { return("sinh(" + ret + ")"); } if (function.equals("cosh")) { return("cosh(" + ret + ")"); } if (function.equals("tanh")) { return("tanh(" + ret + ")"); } return("0"); }