!747 !multi-line strings m { static L rules = litlist( [[ on "hello", say "hello you too" ]] ); p { readLocally("rules"); answerQuestionsOnPortAndConsole(stringfunc { fallback(s, answer(s)) }); } static class Matches { S[] m; } static S fallback(S s, S answer) { if (answer != null) return answer; return "?"; } static synchronized S answer(S s) { new Matches matches; if (match("on * say *", s)) { rules.add(0, s); // add rule on top to override others saveLocally("rules"); return "ok"; } /*if (match("what are your *?", s, matches)) { O value = getOpt(main.class, matches.m[0].toLowerCase()); if (value != null) return structure(value); }*/ if (match("what are your rules?", s, matches)) return structure(rules); for (S rule : rules) { if (match("on * say *", rule, matches)) { S in = matches.m[0], out = matches.m[1]; if (match(unquote(in), s)) return unquote(out); } } return null; } static boolean match(S pat, S s) { return match(pat, s, null); } static boolean match(S pat, S s, Matches matches) { L tokpat = parse(pat), toks = parse(s); S[] m = match2(tokpat, toks); //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m)); if (m == null) return false; else { if (matches != null) matches.m = m; return true; } } static L parse(S s) { return dropPunctuation(javaTokPlusPeriod(s)); } }