import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
public class main {

static PersistentMap<String, String> theories; // name -> text
static MultiMap<String, String> signMap = new MultiMap<String, String>();

static Map<String, String> icCache = new TreeMap<String, String>(); // lower case name -> text

static class Sign {
  String user, theory, as;
  long since;
}

static List<Sign> signList = new ArrayList<Sign>(); // v2 of signMap

static boolean printTimings = false;

public static void main(String[] args) throws Exception {
  theories = new PersistentMap("theories");
  for (String name : getTheoryNames()) icCache.put(name.toLowerCase(), theories.get(name));
  load("signMap");
  load("signList");
}

static synchronized List<String> getTheoryNames() {
  return new ArrayList(theories.keySet());
}

static synchronized boolean hasTheory(String name) {
  return icCache.containsKey(name.toLowerCase());
}

static synchronized String getTheoryOpt(String name) {
  return icCache.get(name.toLowerCase());
}

static synchronized String getTheory(String name) {
  String text = getTheoryOpt(name);
  if (text == null) fail("Theory * not found", name);
  return text;
}

static synchronized List<String> getTheoriesSignedByAs(String user, String as) {
  List<String> l = new ArrayList<String>();
  for (Sign s : signList)
    if (eq(s.user, user) && eq(s.as, as))
      l.add(s.theory);
  return l;
}

static synchronized Sign findSigning(String user, String as, String theory) {
  for (Sign s : signList)
    if (eq(s.user, user) && eq(s.as, as) && eq(s.theory, theory))
      return s;
  return null;
}

static synchronized void theoriesPut(String name, String text) {
  theories.put(name, text);
  icCache.put(name.toLowerCase(), text);
}

static synchronized void theoriesRemove(String name) {
  theories.remove(name);
  icCache.remove(name.toLowerCase());
}

static String signTheory(String user, String theory, String as) {
  if (empty(user)) return "go to slack please";
  if (!hasTheory(theory)) return "Theory not found";
  if (getTheoriesSignedByAs(user, as).contains(theory))
    return "You have already signed this theory as " + as + "!";
  Sign sign = new Sign();
  sign.user = user;
  sign.theory = theory;
  sign.as = as;
  sign.since = now();
  signList.add(sign);
  save("signList");
  return format("OK, signed * as *!", theory, as);
}

static String unsignTheory(String user, String theory, String as) {
  if (empty(user)) return "go to slack please";
  if (!hasTheory(theory)) return "Theory not found";
  Sign sign = findSigning(user, as, theory);
  if (sign == null)
    return "You have not signed this theory as " + as + ".";
  signList.remove(sign);
  save("signList");
  return format("OK, unsigned * as *.", theory, as);
}

synchronized static String answer(String s) {
Matches m = new Matches();
 
  //if (!attn()) ret null;
  
  if (match("count theories", s, m))
    return lstr(theories);
    
  if (match("list theories", s, m))
    return structure(keys(theories));
    
  if (match("show theory *", s, m)) try { 
    return showTheories(litlist(m.unq(0)));
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  if (matchStart("show theories", s, m)) try { 
    return showTheories(codeTokensOnly(javaTok(m.rest())));
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  if (match("modify theory * *", s, m)) try { 
    String name = m.unq(0), text = m.unq(1);
    return saveTheory(name, text, true);
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  if (match("add theory * *", s, m)) try { 
    String name = m.unq(0), text = m.unq(1);
    return saveTheory(name, text, false);
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  String line = firstLine(s).trim();
  if (startsWithIgnoreCase(line, "theory ") && l(toLines(s)) > 1) try { 
    line = dropPrefixIgnoreCase("theory ", line);
    line = dropSuffix(":", line).trim();
    String name = unquote(line), text = dropFirstLine(s);
    return saveTheory(name, text, false);
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  if (match("inc theory * *", s, m) || match("increment theory * *", s, m)) try { 
    String name = m.unq(0), text = m.unq(1);
    return incrementTheory(name, text);
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  if (match("sign theory *", s, m)) try { 
    String user = getUserName();
    return signTheory(user, m.unq(0), "default");
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  if (match("unsign theory *", s, m)) try { 
    String user = getUserName();
    return unsignTheory(user, m.unq(0), "default");
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  if (match("sign theory * as *", s, m)) try { 
    String user = getUserName();
    return signTheory(user, m.unq(0), m.unq(1));
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  if (match("unsign theory * as *", s, m)) try { 
    String user = getUserName();
    return unsignTheory(user, m.unq(0), m.unq(1));
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  if (match("my signed theories", s, m)) try { 
    String user = getUserName();
    if (empty(user)) return "go to slack please";
    return structure(getTheoriesSignedByAs(user, "default"));
  } catch (Throwable __e) { return exceptionToUser(__e); }

  if (match("my theories signed as *", s, m)) try { 
    String user = getUserName();
    if (empty(user)) return "go to slack please";
    return structure(getTheoriesSignedByAs(user, m.unq(0)));
  } catch (Throwable __e) { return exceptionToUser(__e); }

  if (match("theories signed by *", s, m)) try { 
    String user = m.unq(0);
    return structure(getTheoriesSignedByAs(user, "default"));
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  if (match("theories page", s) || match("theories url", s))
    return getBotURL();

  if (!master()) return null;
  
  if (match("delete theory *", s, m)) try { 
    String name = m.unq(0);
    if (hasTheory(name)) {
      String text = getTheoryOpt(name);
      logMap("deleted", "name", name, "text", text, "time", now(), "signers", signMap.get(name));
      logMap("deleted", "name", name, "signers", signMap.get(name));
      theoriesRemove(name);
      
      signMap.remove(name);
      save("signMap");
      
      return format("Theory * backed up & deleted", name);
    } else
      return format("Theory * not found", name);
  } catch (Throwable __e) { return exceptionToUser(__e); }
  
  if (match("rename theory * to *", s, m)) try { 
    String name = m.unq(0), newName = m.unq(1);
    if (hasTheory(newName))
      return format("A theory named * already exists", newName);
    if (hasTheory(name)) {
      String text = getTheoryOpt(name);
      theoriesPut(newName, text);
      theoriesRemove(name);

      // update signMap
      List<String> signers = signMap.get(name);
      signMap.addAll(newName, signers);
      signMap.remove(name);
      save("signMap");
      
      return format("Theory * renamed to *", name, newName);
    } else
      return format("Theory * not found", name);
  } catch (Throwable __e) { return exceptionToUser(__e); }

return null;
}

static String showTheories(List<String> theoryNames) {
  StringBuilder buf = new StringBuilder();
  for (String name: theoryNames) {
    if (!hasTheory(name))
      buf.append(format("Theory * not found\n", name));
    else {
      buf.append(quote(name) + "\n" + slackSnippet(getTheoryOpt(name)) + "\n\n");
    }
  }
  return str(buf).trim();
}

static String html(String uri, Map<String, String> params) {
  if (eq(uri, "/signList"))
    return htmlencode(structure(signList));
  
  if (eq(uri, "/theories"))
    return htmlencode(structure(theories));
    
  if (eq(uri, "/theoriesInc")) {
    int n = parseInt(or(params.get("n"), "0"));
    String id = params.get("id");
    
    if (nempty(id) && neq(id, theories.id()))
      n = 0;
      
    int m = (int) theories.file.length();
    String text = unnull(loadTextFileStartingFrom(theories.file, n));
    //text = substr(text, n);
    return theories.id() + "\n" + m + "\n" + htmlencode(text);
  }

  boolean showMulti = false;
  List data = new ArrayList();
  MultiMap<String, Sign> signs = getSigningsByTheory();
  for (String name : getTheoryNames()) {
    String text = unnull(getTheoryOpt(name));
    boolean multi = false;
    Exception error = null;
    List<Sign> signings = signs.get(name);
    List<String> bla = new ArrayList<String>();
    for (Sign sign : signings)
      bla.add(sign.user + " as " + sign.as);
    Map map = litmap(
      "Name", htmlencode(name),
      "Theory", pre(htmlencode(minitrim(rtrim(text)))),
      "Signed by", htmlencode(join(", ", bla)));
    if (showMulti) {
      map.put("Multiple rules?", multi);
      try {
        multi = nlIsMultipleStatements(text);
      } catch (Exception e) {
        error = e;
      }
    }
    if (error != null)
      map.put("Parse Error", str(error));
    data.add(map);
  }
  return h3("Theories!") + htmlTable(data, false);
}

// preserves formatting of first line
static String minitrim(String s) {
  List<String> l = toLines(s);
  while (!empty(l) && empty(trim(first(l))))
    l.remove(0);
  return autoUnindent(fromLines(l));
}

static String saveTheory(String name, String text, boolean autoModify) {
  boolean has = hasTheory(name);
  String oldText = getTheoryOpt(name);
  String origName = name;
  int counter = 0;
  while (has && !autoModify) {
    name = origName + "." + (char) (((int) 'a') + (++counter));
    has = hasTheory(name);
  }
  logMap(has ? "modified" : "added", "name", name, "text", text, "time", now(), "oldText", oldText);
  theoriesPut(name, text);
  return format((has ? "OK, modified theory *" : "OK, added theory *"), name);
}

static synchronized long getTheoryCounter(String name) {
  String text = getTheoryOpt(name + ".count");
  return isEmpty(text) ? 0 : parseLong(text);
}

// returns inc'ed value
static synchronized long incTheoryCounter(String name) {
  long c = getTheoryCounter(name);
  ++c;
  theoriesPut(name + ".count", str(c));
  return c;
}

// returned list might include null elements
static List<String> getIncrements(String name) {
  List<String> l = new ArrayList<String>();
  long n = getTheoryCounter(name);
  for (long i = 1; i <= n; i++)
    l.add(getTheoryOpt(name + "." + i));
  return l;
}

static synchronized String incrementTheory(String name, String newText) {
  long n = incTheoryCounter(name);
  return saveTheory(name + "." + n, newText, false);
}

static synchronized MultiMap<String, Sign> getSigningsByTheory() {
  MultiMap<String, Sign> map = new MultiMap<String, Sign>();
  for (Sign s : signList)
    map.put(s.theory, s);
  return map;
}


static boolean empty(Collection c) {
  return isEmpty(c);
}

static boolean empty(String s) {
  return isEmpty(s);
}

static boolean empty(Map map) {
  return map == null || map.isEmpty();
}
  static String quote(String s) {
    if (s == null) return "null";
    return "\"" + s.replace("\\", "\\\\").replace("\"", "\\\"").replace("\r", "\\r").replace("\n", "\\n") + "\"";
  }
  
  static String quote(long l) {
    return quote("" + l);
  }

static String trim(String s) { return s == null ? null : s.trim(); }
static String trim(StringBuilder buf) { return buf.toString().trim(); }
static String trim(StringBuffer buf) { return buf.toString().trim(); }
static String htmlTable(Object data) {
  return htmlTable(data, true);
}

static String htmlTable(Object data, boolean useHtmlEncode) {
  return htmlTable(data, useHtmlEncode, false);
}

static String htmlTable(Object data, boolean useHtmlEncode, boolean useBr) {
  // prepare table
  
  List<List<String>> rows = new ArrayList<List<String>>();
  List<String> cols = new ArrayList<String>();
  
  if (data instanceof List) {
    for (Object x : (List) data) try { 
      rows.add(dataToTable_makeRow(x, cols));
    } catch (Throwable __e) { printStackTrace(__e); }
  } else if (data instanceof Map) {
    Map map = (Map) ( data);
    for (Object key : map.keySet()) {
      Object value = map.get(key);
      rows.add(litlist(structureOrText(key), structureOrText(value)));
    }
  } else
    print("Unknown data type: " + data);
    
  // get table width
  int w = 0;
  for (List<String> row : rows)
    w = max(w, l(row));
    
  // construct HTML for table
  
  StringBuilder buf = new StringBuilder();
  buf.append("<table border>\n");
  
  // title
  buf.append("<tr>\n");
  for (String cell : padList(cols, w, "?"))
    buf.append("  <th>" + htmlTable_encodeCell(cell, useHtmlEncode, useBr) + "</th>\n");
  buf.append("</tr>\n");
  
  // data
  for (List<String> row : rows) {
    buf.append("<tr>\n");
    for (String cell : padList(row, w, "?"))
      buf.append("  <td>" + htmlTable_encodeCell(cell, useHtmlEncode, useBr) + "</td>\n");
    buf.append("</tr>\n");
  }
  buf.append("</table>\n");
  return buf.toString();
}

static String htmlTable_encodeCell(String cell, boolean useHtmlEncode, boolean useBr) {
  if (useHtmlEncode) cell = htmlEncode(cell);
  if (useBr) cell = nlToBr(cell);
  return cell;
}
  static boolean matchStart(String pat, String s) {
    return matchStart(pat, s, null);
  }
  
  // matches are as you expect, plus an extra item for the rest string
  static boolean matchStart(String pat, String s, Matches matches) {
    if (s == null) return false;
    List<String> tokpat = parse3(pat), toks = parse3(s);
    if (toks.size() < tokpat.size()) return false;
    String[] m = match2(tokpat, toks.subList(0, tokpat.size()));
    //print(structure(tokpat) + " on " + structure(toks) + " => " + structure(m));
    if (m == null)
      return false;
    else {
      if (matches != null) {
        matches.m = new String[m.length+1];
        arraycopy(m, matches.m);
        matches.m[m.length] = join(toks.subList(tokpat.size(), toks.size())); // for Matches.rest()
      }
      return true;
    }
  }
static String getUserName() {
  return (String) callOpt(getMainBot(), "getUserName");
}
static boolean neq(Object a, Object b) {
  return !eq(a, b);
}
  public static String join(String glue, Iterable<String> strings) {
    StringBuilder buf = new StringBuilder();
    Iterator<String> i = strings.iterator();
    if (i.hasNext()) {
      buf.append(i.next());
      while (i.hasNext())
        buf.append(glue).append(i.next());
    }
    return buf.toString();
  }
  
  public static String join(String glue, String[] strings) {
    return join(glue, Arrays.asList(strings));
  }
  
  public static String join(Iterable<String> strings) {
    return join("", strings);
  }
  
  public static String join(String[] strings) {
    return join("", strings);
  }

  public static String loadTextFileStartingFrom(File fileName, int startAt) { try {
 
    if (!fileName.exists())
      return "";

    FileInputStream fileInputStream = new FileInputStream(fileName);
    fileInputStream.skip(startAt);
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
    return loadTextFile(inputStreamReader);
  
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static boolean nlIsMultipleStatements(String text) {
  List<String> tok = codeTokens(snlTok(text));
  //ret eq(first(tok), "[") && eq(last(tok), "]");
  
  if (!(eq(first(tok), "[") && eq(last(tok), "]")))
    return false;
    
  List<String> warnings1 = new ArrayList<String>();
  List<String> warnings2 = new ArrayList<String>();
  Lisp parse1 = nlParse(tok, true, warnings1);
  Lisp parse2 = nlParse(subList(tok, 1, l(tok)-1), true, warnings2);
  //ret !eq(parse1, parse2);
  return l(warnings2) > l(warnings1);
  
  //ret isJuxta(tree) && snlSplitOps(tree) == null;
}
static String pre(String s) {
  return tag("pre", s);
}
  static String format(String pat, Object... args) {
    return format3(pat, args);
  }

  static List<String> codeTokensOnly(List<String> tok) {
    List<String> l = new ArrayList<String>();
    for (int i = 1; i < tok.size(); i += 2)
      l.add(tok.get(i));
    return l;
  }
static boolean eq(Object a, Object b) {
  if (a == null) return b == null;
  if (a.equals(b)) return true;
  if (a instanceof BigInteger) {
    if (b instanceof Integer) return a.equals(BigInteger.valueOf((Integer) b));
    if (b instanceof Long) return a.equals(BigInteger.valueOf((Long) b));
  }
  return false;
}
static Map litmap(Object... x) {
  TreeMap map = new TreeMap();
  for (int i = 0; i < x.length-1; i += 2)
    if (x[i+1] != null)
      map.put(x[i], x[i+1]);
  return map;
}
static String firstLine(String text) {
  int i = text.indexOf('\n');
  return i >= 0 ? text.substring(0, i) : text;
}
static String str(Object o) {
  return String.valueOf(o);
}
static String autoUnindent(String s) {
  int n = getIndent(s);
  if (n == 0) return s;
  List<String> l = toLines(s);
  for (int i = 0; i < l(l); i++)
    l.set(i, substring(l.get(i), n));
  return fromLines(l);
}
  static RuntimeException fail() {
    throw new RuntimeException("fail");
  }
  
  static RuntimeException fail(Object msg) {
    throw new RuntimeException(String.valueOf(msg));
  }
  
  static RuntimeException fail(String msg) {
    throw new RuntimeException(unnull(msg));
  }
    
  static RuntimeException fail(String msg, Object... args) {
    throw new RuntimeException(format(msg, args));
  }
static String getBotURL(String id) {
  return mainURL() + parseSnippetID(id);
}

static String getBotURL() {
  return getBotURL(programID());
}
static <A, B> Set<A> keys(Map<A, B> map) {
  return map.keySet();
}

static Set keys(Object map) {
  return keys((Map) map);
}
static void save(String varName) {
  saveLocally(varName);
}

static void save(String progID, String varName) {
  saveLocally(progID, varName);
}
// replacement for class JavaTok
// maybe incomplete, might want to add floating point numbers
// todo also: extended multi-line strings

static List<String> javaTok(String s) {
  List<String> tok = new ArrayList<String>();
  int l = s.length();
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c; String cc;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      cc = s.substring(j, Math.min(j+2, l));
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (cc.equals("/*")) {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (cc.equals("//")) {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    tok.add(s.substring(i, j));
    i = j;
    if (i >= l) break;
    c = s.charAt(i); // cc is not needed in rest of loop body
    cc = s.substring(i, Math.min(i+2, l));

    // scan for non-whitespace
    if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't"
    else if (Character.isDigit(c)) {
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
      if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
    } else if (cc.equals("[[")) {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else if (cc.equals("[=") && i+2 < l && s.charAt(i+2) == '[') {
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
      j = Math.min(j+3, l);
    } else
      ++j;

    tok.add(s.substring(i, j));
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  return tok;
}

static List<String> javaTok(List<String> tok) {
  return javaTok(join(tok));
}

  static String unnull(String s) {
    return s == null ? "" : s;
  }
  
  static List unnull(List l) {
    return l == null ? emptyList() : l;
  }
static String h3(String s) {
  return tag("h3", s);
}
  public static String fromLines(List<String> lines) {
    StringBuilder buf = new StringBuilder();
    if (lines != null)
      for (String line : lines)
        buf.append(line).append('\n');
    return buf.toString();
  }
static void load(String varName) {
  readLocally(varName);
}

static void load(String progID, String varName) {
  readLocally(progID, varName);
}
static <A> ArrayList<A> litlist(A... a) {
  return new ArrayList<A>(Arrays.asList(a));
}
static long now_virtualTime;
static long now() {
  return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}

static List<String> toLines(File f) {
  return toLines(loadTextFile(f));
}

  public static List<String> toLines(String s) {
    List<String> lines = new ArrayList<String>();
    if (s == null) return lines;
    int start = 0;
    while (true) {
      int i = toLines_nextLineBreak(s, start);
      if (i < 0) {
        if (s.length() > start) lines.add(s.substring(start));
        break;
      }

      lines.add(s.substring(start, i));
      if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
        i += 2;
      else
        ++i;

      start = i;
    }
    return lines;
  }

  private static int toLines_nextLineBreak(String s, int start) {
    for (int i = start; i < s.length(); i++) {
      char c = s.charAt(i);
      if (c == '\r' || c == '\n')
        return i;
    }
    return -1;
  }
  public static String unquote(String s) {
    if (s.startsWith("[")) {
      int i = 1;
      while (i < s.length() && s.charAt(i) == '=') ++i;
      if (i < s.length() && s.charAt(i) == '[') {
        String m = s.substring(1, i);
        if (s.endsWith("]" + m + "]"))
          return s.substring(i+1, s.length()-i-1);
      }
    }
    
    if (s.startsWith("\"") /*&& s.endsWith("\"")*/ && s.length() > 1) {
      String st = s.substring(1, s.endsWith("\"") ? s.length()-1 : s.length());
      StringBuilder sb = new StringBuilder(st.length());
  
      for (int i = 0; i < st.length(); i++) {
        char ch = st.charAt(i);
        if (ch == '\\') {
          char nextChar = (i == st.length() - 1) ? '\\' : st
                  .charAt(i + 1);
          // Octal escape?
          if (nextChar >= '0' && nextChar <= '7') {
              String code = "" + nextChar;
              i++;
              if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
                      && st.charAt(i + 1) <= '7') {
                  code += st.charAt(i + 1);
                  i++;
                  if ((i < st.length() - 1) && st.charAt(i + 1) >= '0'
                          && st.charAt(i + 1) <= '7') {
                      code += st.charAt(i + 1);
                      i++;
                  }
              }
              sb.append((char) Integer.parseInt(code, 8));
              continue;
          }
          switch (nextChar) {
          case '\\':
              ch = '\\';
              break;
          case 'b':
              ch = '\b';
              break;
          case 'f':
              ch = '\f';
              break;
          case 'n':
              ch = '\n';
              break;
          case 'r':
              ch = '\r';
              break;
          case 't':
              ch = '\t';
              break;
          case '\"':
              ch = '\"';
              break;
          case '\'':
              ch = '\'';
              break;
          // Hex Unicode: u????
          case 'u':
              if (i >= st.length() - 5) {
                  ch = 'u';
                  break;
              }
              int code = Integer.parseInt(
                      "" + st.charAt(i + 2) + st.charAt(i + 3)
                              + st.charAt(i + 4) + st.charAt(i + 5), 16);
              sb.append(Character.toChars(code));
              i += 5;
              continue;
          default:
            ch = nextChar; // added by Stefan
          }
          i++;
        }
        sb.append(ch);
      }
      return sb.toString();      
    } else
      return s; // return original
  }
static String dropPrefixIgnoreCase(String prefix, String s) {
  return startsWithIgnoreCase(s, prefix) ? s.substring(l(prefix)) : s;
}
static <A> A or(A a, A b) {
  return a != null ? a : b;
}
static String dropFirstLine(String text) {
  int i = text.indexOf('\n');
  return i >= 0 ? text.substring(i+1) : "";
}
static String dropSuffix(String suffix, String s) {
  return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : s;
}
static boolean match(String pat, String s) {
  return match3(pat, s);
}

static boolean match(String pat, String s, Matches matches) {
  return match3(pat, s, matches);
}

static boolean nempty(Collection c) {
  return !isEmpty(c);
}

static boolean nempty(String s) {
  return !isEmpty(s);
}
public static String rtrim(String s) {
  int i = s.length();
  while (i > 0 && " \t\r\n".indexOf(s.charAt(i-1)) >= 0)
    --i;
  return i < s.length() ? s.substring(0, i) : s;
}
static boolean isEmpty(Collection c) {
  return c == null || c.isEmpty();
}

static boolean isEmpty(String s) {
  return s == null || s.length() == 0;
}
static String slackSnippet(Object contents) {
  if (contents instanceof List) contents = join("\n", (List) contents);
  String s = str(contents);
  //ret "```" + (empty(s) ? "\n" : s) + "```";
  return "```\n" + s + "```";
}
static int l(Object[] array) {
  return array == null ? 0 : array.length;
}

static int l(byte[] array) {
  return array == null ? 0 : array.length;
}

static int l(int[] array) {
  return array == null ? 0 : array.length;
}

static int l(char[] array) {
  return array == null ? 0 : array.length;
}

static int l(Collection c) {
  return c == null ? 0 : c.size();
}

static int l(Map m) {
  return m == null ? 0 : m.size();
}

static int l(String s) {
  return s == null ? 0 : s.length();
} 


// "seen" is now default
static String structure(Object o) {
  return structure(o, 0, new IdentityHashMap());
}

static String structure_seen(Object o) {
  return structure(o, 0, new IdentityHashMap());
}

// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;
  
static String structure(Object o, int stringSizeLimit, IdentityHashMap<Object, Integer> seen) {
  if (o == null) return "null";
  
  // these are never back-referenced (for readability)
  
  if (o instanceof String)
    return quote(stringSizeLimit != 0 ? shorten((String) o, stringSizeLimit) : (String) o);
    
  if (o instanceof BigInteger)
    return "bigint(" + o + ")";
  
  if (o instanceof Double)
    return "d(" + quote(str(o)) + ")";
    
  if (o instanceof Long)
    return o + "L";
  
  if (seen != null) {
    Integer ref = seen.get(o);
    if (ref != null)
      return "r" + ref;
      
    seen.put(o, seen.size()+1);
  }

  String name = o.getClass().getName();

  StringBuilder buf = new StringBuilder();
  
  if (o instanceof HashSet)
    return "hashset" + structure(new ArrayList((Set) o), stringSizeLimit, seen);

  if (o instanceof TreeSet)
    return "treeset" + structure(new ArrayList((Set) o), stringSizeLimit, seen);
  
  if (o instanceof Collection) {
    for (Object x : (Collection) o) {
      if (buf.length() != 0) buf.append(", ");
      buf.append(structure(x, stringSizeLimit, seen));
    }
    return "[" + buf + "]";
  }
  
  if (o instanceof Map) {
    for (Object e : ((Map) o).entrySet()) {
      if (buf.length() != 0) buf.append(", ");
      buf.append(structure(((Map.Entry) e).getKey(), stringSizeLimit, seen));
      buf.append("=");
      buf.append(structure(((Map.Entry) e).getValue(), stringSizeLimit, seen));
    }
    return (o instanceof HashMap ? "hashmap" : "") + "{" + buf + "}";
  }
  
  if (o.getClass().isArray()) {
    int n = Array.getLength(o);
    for (int i = 0; i < n; i++) {
      if (buf.length() != 0) buf.append(", ");
      buf.append(structure(Array.get(o, i), stringSizeLimit, seen));
    }
    return "array{" + buf + "}";
  }

  if (o instanceof Class)
    return "class(" + quote(((Class) o).getName()) + ")";
    
  if (o instanceof Throwable)
    return "exception(" + quote(((Throwable) o).getMessage()) + ")";
    
  // Need more cases? This should cover all library classes...
  if (name.startsWith("java.") || name.startsWith("javax."))
    return String.valueOf(o);
    
  String shortName = o.getClass().getName().replaceAll("^main\\$", "");
  
  if (shortName.equals("Lisp")) {
    buf.append("l(" + structure(getOpt(o, "head"), stringSizeLimit, seen));
    List args = (List) ( getOpt(o, "args"));
    if (nempty(args))
      for (int i = 0; i < l(args); i++) {
        buf.append(", ");
        Object arg = args.get(i);
        
        // sweet shortening
        if (arg != null && eq(arg.getClass().getName(), "main$Lisp") && isTrue(call(arg, "isEmpty")))
          arg = get(arg, "head");
          
        buf.append(structure(arg, stringSizeLimit, seen));
      }
    buf.append(")");
    return str(buf);
  }
    
  int numFields = 0;
  String fieldName = "";
  if (shortName.equals("DynamicObject")) {
    shortName = (String) get(o, "className");
    Map<String, Object> fieldValues = (Map) get(o, "fieldValues");
    
    for (String _fieldName : fieldValues.keySet()) {
      fieldName = _fieldName;
      Object value = fieldValues.get(fieldName);
      if (value != null) {
        if (buf.length() != 0) buf.append(", ");
        buf.append(fieldName + "=" + structure(value, stringSizeLimit, seen));
      }
      ++numFields;
   }
  } else {
    // regular class
    // TODO: go to superclasses too
    Field[] fields = o.getClass().getDeclaredFields();
    for (Field field : fields) {
      if ((field.getModifiers() & Modifier.STATIC) != 0)
        continue;
      fieldName = field.getName();
      
      // skip outer object reference
      if (fieldName.indexOf("$") >= 0) continue;

      Object value;
      try {
        field.setAccessible(true);
        value = field.get(o);
      } catch (Exception e) {
        value = "?";
      }
      
      // put special cases here...
  
      if (value != null) {
        if (buf.length() != 0) buf.append(", ");
        buf.append(fieldName + "=" + structure(value, stringSizeLimit, seen));
      }
      ++numFields;
    }
  }
  
  String b = buf.toString();
  
  if (numFields == 1 && structure_allowShortening)
    b = b.replaceAll("^" + fieldName + "=", ""); // drop field name if only one
  String s = shortName;
  if (buf.length() != 0)
    s += "(" + b + ")";
  return s;
}
  // automagically encode a whole map (keys+values)
  static Map htmlencode(Map o) {
    HashMap bla = new HashMap();
    for (Object key : keys(o)) {
      Object value = o.get(key);
      bla.put(htmlencode(key), htmlencode(value));
    }
    return bla;
  }

  static String htmlencode(Object o) {
    return htmlencode(string(o));
  }
  
  static String htmlencode(String s) {
    if (s == null) return "";
    StringBuilder out = new StringBuilder(Math.max(16, s.length()));
    for (int i = 0; i < s.length(); i++) {
        char c = s.charAt(i);
        if (c > 127 || c == '"' || c == '<' || c == '>' || c == '&') {
            out.append("&#");
            out.append((int) c);
            out.append(';');
        } else {
            out.append(c);
        }
    }
    return out.toString();
  }
static boolean master() {
  return webAuthed() || litlist("stefanreich", "bgrgndz", "okhan", "mrshutco").contains(getUserName());
}
static void logMap(File logFile, Map map) {
  logStructure(logFile, map);
}

// quick version - log to file in program directory
static void logMap(String fileName, Map map) {
  logStructure(fileName, map);
}

static void logMap(String fileName, Object... data) {
  logMap(fileName, litmap(data));
}
static int parseInt(String s) {
  return Integer.parseInt(s);
}
static long parseLong(String s) {
  return Long.parseLong(s);
}
static String lstr(Map map) {
  return str(l(map));
}

static String lstr(List l) {
  return str(l(l));
}

static String lstr(String s) {
  return str(l(s));
}
static boolean startsWithIgnoreCase(String a, String b) {
  return a != null && a.regionMatches(true, 0, b, 0, b.length());
}
static Object first(Object list) {
  return ((List) list).isEmpty() ? null : ((List) list).get(0);
}

static <A> A first(List<A> list) {
  return list.isEmpty() ? null : list.get(0);
}

// match2 matches multiple "*" (matches a single token) wildcards and zero or one "..." wildcards (matches multiple tokens)

static String[] match2(List<String> pat, List<String> tok) {
  // standard case (no ...)
  int i = pat.indexOf("...");
  if (i < 0) return match2_match(pat, tok);
  
  pat = new ArrayList<String>(pat); // We're modifying it, so copy first
  pat.set(i, "*");
  while (pat.size() < tok.size()) {
    pat.add(i, "*");
    pat.add(i+1, ""); // doesn't matter
  }
  
  return match2_match(pat, tok);
}

static String[] match2_match(List<String> pat, List<String> tok) {
  List<String> result = new ArrayList<String>();
  if (pat.size() != tok.size()) {
    /*if (debug)
      print("Size mismatch: " + structure(pat) + " vs " + structure(tok));*/
    return null;
  }
  for (int i = 1; i < pat.size(); i += 2) {
    String p = pat.get(i), t = tok.get(i);
    /*if (debug)
      print("Checking " + p + " against " + t);*/
    if (eq(p, "*"))
      result.add(t);
    else if (!equalsIgnoreCase(unquote(p), unquote(t))) // bold change - match quoted and unquoted now
      return null;
  }
  return result.toArray(new String[result.size()]);
}

  static String format3(String pat, Object... args) {
    if (args.length == 0) return pat;
    
    List<String> tok = javaTokPlusPeriod(pat);
    int argidx = 0;
    for (int i = 1; i < tok.size(); i += 2)
      if (tok.get(i).equals("*"))
        tok.set(i, format3_formatArg(argidx < args.length ? args[argidx++] : "null"));
    return join(tok);
  }
  
  static String format3_formatArg(Object arg) {
    if (arg == null) return "null";
    if (arg instanceof String) {
      String s = (String) arg;
      return isIdentifier(s) || isNonNegativeInteger(s) ? s : quote(s);
    }
    if (arg instanceof Integer || arg instanceof Long) return String.valueOf(arg);
    return quote(structure(arg));
  }
  

  // class Matches is added by #752
 
  static boolean match3(String pat, String s) {
    return match3(pat, s, null);
  }
  
  static boolean match3(String pat, String s, Matches matches) {
    if (s == null) return false;
    return match3(pat, parse3(s), matches);
  }
    
  static boolean match3(String pat, List<String> toks, Matches matches) {
    List<String> tokpat = parse3(pat);
    return match3(tokpat,toks,matches);
  }

  static boolean match3(List<String> tokpat, List<String> toks, Matches matches) {

    String[] 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 String shorten(String s, int max) {
  if (s == null) return "";
  return s.length() <= max ? s : s.substring(0, Math.min(s.length(), max)) + "...";
}
  static void dataToTable_dynSet(List<String> l, int i, String s) {
    while (i >= l.size()) l.add("");
    l.set(i, s);
  }
  
  static List<String> dataToTable_makeRow(Object x, List<String> cols) {
    if (instanceOf(x, "DynamicObject"))
      x = get_raw(x, "fieldValues");

    if (x instanceof Map) {
      Map m = (Map) ( x);
      List<String> row = new ArrayList<String>();
      for (Object _field : m.keySet()) {
        String field = (String) ( _field);
        Object value = m.get(field);
        int col = cols.indexOf(field);
        if (col < 0) {
          cols.add(field);
          col = cols.size()-1;
        }
        dataToTable_dynSet(row, col, structureOrText(value));
      }
      return row;
    }
    
    return litlist(structureOrText(x));
  }
static String tag(String tag, String contents, Object... params) {
  return htag(tag, contents, params);
}

static String tag(String tag, StringBuilder contents, Object... params) {
  return htag(tag, contents, params);
}

static String tag(String tag, StringBuffer contents, Object... params) {
  return htag(tag, contents, params);
}
static int getIndent(String s) {
  int n = Integer.MAX_VALUE;
  for (String l : toLines(s)) {
    int i = 0;
    while (i < l(l) && l.charAt(i) == ' ')
      ++i;
    n = min(n, i);
  }
  return n;
}

static <A> A last(List<A> l) {
  return l.isEmpty() ? null : l.get(l.size()-1);
}
static void saveLocally(String variableName) {
  saveLocally(programID(), variableName);
}

static synchronized void saveLocally(String progID, String variableName) {
  File textFile = new File(programDir(progID), variableName + ".text");
  File structureFile = new File(programDir(progID), variableName + ".structure");
  Object x = get(main.class, variableName);
  
  if (x == null) {
    textFile.delete();
    structureFile.delete();
  } else if (x instanceof String) {
    structureFile.delete();
    saveTextFile(textFile, (String) x);
  } else {
    textFile.delete();
    saveTextFile(structureFile, structure(x));
  }
}
static <A> List<A> padList(List<A> l, int w, A a) {
  if (l(l) >= w) return l;
  List<A> x = cloneList(l);
  while (l(x) < w) x.add(a);
  return x;
}
static void logStructure(File logFile, Object o) {
  logQuoted(logFile, structure(o));
}

// quick version - log to file in program directory
static void logStructure(String fileName, Object o) {
  logStructure(getProgramFile(fileName), o);
}
static String string(Object o) {
  return String.valueOf(o);
}
  static Object callOpt(Object o, String method, Object... args) {
    try {
      if (o == null) return null;
      if (o instanceof Class) {
        Method m = callOpt_findStaticMethod((Class) o, method, args, false);
        if (m == null) return null;
        m.setAccessible(true);
        return m.invoke(null, args);
      } else {
        Method m = callOpt_findMethod(o, method, args, false);
        if (m == null) return null;
        m.setAccessible(true);
        return m.invoke(o, args);
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  static Method callOpt_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
    Class _c = c;
    while (c != null) {
      for (Method m : c.getDeclaredMethods()) {
        if (debug)
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
        if (!m.getName().equals(method)) {
          if (debug) System.out.println("Method name mismatch: " + method);
          continue;
        }

        if ((m.getModifiers() & Modifier.STATIC) == 0 || !callOpt_checkArgs(m, args, debug))
          continue;

        return m;
      }
      c = c.getSuperclass();
    }
    return null;
  }

  static Method callOpt_findMethod(Object o, String method, Object[] args, boolean debug) {
    Class c = o.getClass();
    while (c != null) {
      for (Method m : c.getDeclaredMethods()) {
        if (debug)
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
        if (m.getName().equals(method) && callOpt_checkArgs(m, args, debug))
          return m;
      }
      c = c.getSuperclass();
    }
    return null;
  }

  private static boolean callOpt_checkArgs(Method m, Object[] args, boolean debug) {
    Class<?>[] types = m.getParameterTypes();
    if (types.length != args.length) {
      if (debug)
        System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
      return false;
    }
    for (int i = 0; i < types.length; i++)
      if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
        if (debug)
          System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
        return false;
      }
    return true;
  }


static String nlToBr(String s) {
  return s.replace("\n", "<br>\n");
}
  static List<String> parse3(String s) {
    return dropPunctuation(javaTokPlusPeriod(s));
  }
static Object mainBot;

static Object getMainBot() {
  return mainBot;
}
static String substring(String s, int x) {
  return safeSubstring(s, x);
}

static String substring(String s, int x, int y) {
  return safeSubstring(s, x, y);
}
public static long parseSnippetID(String snippetID) {
  long id = Long.parseLong(shortenSnippetID(snippetID));
  if (id == 0) fail("0 is not a snippet ID");
  return id;
}
static List emptyList() {
  return Collections.emptyList();
}
static void arraycopy(Object[] a, Object[] b) {
  int n = min(a.length, b.length);
  for (int i = 0; i < n; i++)
    b[i] = a[i];
}
// get purpose 1: access a list/array (safer version of x.get(y))

static <A> A get(List<A> l, int idx) {
  return idx >= 0 && idx < l(l) ? l.get(idx) : null;
}

static <A> A get(A[] l, int idx) {
  return idx >= 0 && idx < l(l) ? l[idx] : null;
}

// get purpose 2: access a field by reflection or a map

static Object get(Object o, String field) {
  if (o instanceof Class) return get((Class) o, field);
  
  if (o instanceof Map)
    return ((Map) o).get(field);
    
  if (o.getClass().getName().equals("main$DynamicObject"))
    return call(get_raw(o, "fieldValues"), "get", field);
    
  return get_raw(o, field);
}

static Object get_raw(Object o, String field) {
  try {
    Field f = get_findField(o.getClass(), field);
    f.setAccessible(true);
    return f.get(o);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Object get(Class c, String field) {
  try {
    Field f = get_findStaticField(c, field);
    f.setAccessible(true);
    return f.get(null);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Field get_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}

static Field get_findField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static AtomicInteger nlParse_count = new AtomicInteger(); // how often did we parse something?

static Lisp nlParse(String s) {
  return nlParse(s, true);
}

static Lisp nlParse(String s, boolean unbracket) {
  return nlParse(codeTokens(snlTok(s)), unbracket, null);
}

static Lisp nlParse(List<String> tok) {
  return nlParse(tok, true, null);
}

static Lisp nlParse(List<String> tok, boolean unbracket, List<String> warnings) {
  nlParse_count.incrementAndGet();
  
  class Entry {
    int i;
    Lisp tree;
    String wrapper;
    
    Entry(int i) {
  this.i = i;
      tree = lisp("[]");
    }
    
    Entry(int i, String bracket) {
  this.i = i;
      tree = lisp("[]");
      wrapper = bracket;
    }
    
    Lisp wrapped() {
      Lisp t = nlUnbracket(tree);
      return wrapper == null ? t : lisp(wrapper, t);
    }
  }

  List<Entry> stack = new ArrayList<Entry>();
  stack.add(new Entry(0));
  
  for (int i = 0; i < l(tok); i++) {
    String t = tok.get(i);
    if (eq(t, "[") || eq(t, "(")) {
      stack.add(new Entry(i, eq(t, "(") ? "()" : null));
    } else if (eq(t, "]") || eq(t, ")")) {
      if (l(stack) == 1)
        warn("too many closing brackets", warnings);
      else {
        Entry e = popLast(stack);
        /*if (!bracketsMatch(tok.get(e.i), t))
          warn("non-matching brackets");*/
        last(stack).tree.add(e.wrapped());
      }
    } else
      last(stack).tree.add(t);
  }
  
  while (l(stack) > 1) {
    warn("too many opening brackets", warnings);
    Entry e = popLast(stack);
    last(stack).tree.add(e.wrapped());
  }
  
  Lisp result = last(stack).wrapped();
  return unbracket ? nlUnbracket(result) : result;
}

static void readLocally(String varNames) {
  readLocally(programID(), varNames);
}

// read a string variable from standard storage
// does not overwrite variable contents if there is no file
static synchronized void readLocally(String progID, String varNames) {
  for (String variableName : codeTokensOnly(javaTok(varNames))) {
    File textFile = new File(programDir(progID), variableName + ".text");
    File structureFile = new File(programDir(progID), variableName + ".structure");
    
    String value = loadTextFile(textFile);
    if (value != null)
      set(main.class, variableName, value);
    else {
      value = loadTextFile(structureFile);
      if (value != null)
      readLocally_set(main.class, variableName, unstructure(value));
    }
  }
}

static void readLocally_set(Class c, String varName, Object value) {
  Object oldValue = get(c, varName);
  if (oldValue instanceof List && !(oldValue instanceof ArrayList) && value != null) {
    // Assume it's a synchroList.
    value = synchroList((List) value);
  }
  set(c, varName, value);
}

static String programID() {
  return getProgramID();
}
static boolean webAuthed() {
  return eq(Boolean.TRUE, callOpt(getBot("#1002590"), "currentHttpRequestAuthorized"));
}
// This is made for SNL parsing.
// It does NOT recognize multiline strings as these conflict
// with syntax like [[a] [b]].

static List<String> snlTok(String s) {
  List<String> tok = new ArrayList<String>();
  int l = s.length();
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c; String cc;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      cc = s.substring(j, Math.min(j+2, l));
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (cc.equals("/*")) {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (cc.equals("//")) {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    tok.add(s.substring(i, j));
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    cc = s.substring(i, Math.min(i+2, l));

    // scan for non-whitespace
    if (c == '\u201C' || c == '\u201D') c = '"'; // normalize quotes
    if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        char _c = s.charAt(j);
        if (_c == '\u201C' || _c == '\u201D') _c = '"'; // normalize quotes
        if (_c == opener) {
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
      if (j-1 >= i+1) {
        tok.add(opener + s.substring(i+1, j-1) + opener);
        i = j;
        continue;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); // for things like "this one's"
    else if (Character.isDigit(c))
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    /*else if (cc.equals("[[")) {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    }*/ else if (s.substring(j, Math.min(j+3, l)).equals("..."))
      j += 3;
    else if (c == '$' || c == '#')
      do ++j; while (j < l && Character.isLetterOrDigit(s.charAt(j)));
    else
      ++j;

    tok.add(s.substring(i, j));
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  return tok;
}

static <A> List<A> subList(List<A> l, int startIndex) {
  return subList(l, startIndex, l(l));
}

static <A> List<A> subList(List<A> l, int startIndex, int endIndex) {
  startIndex = max(0, min(l(l), startIndex));
  endIndex = max(0, min(l(l), endIndex));
  if (startIndex > endIndex) return litlist();
  return l.subList(startIndex, endIndex);
}
  public static String loadTextFile(String fileName) {
    try {
      return loadTextFile(fileName, null);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  
  public static String loadTextFile(String fileName, String defaultContents) throws IOException {
    if (!new File(fileName).exists())
      return defaultContents;

    FileInputStream fileInputStream = new FileInputStream(fileName);
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
    return loadTextFile(inputStreamReader);
  }
  
  public static String loadTextFile(File fileName) {
    try {
      return loadTextFile(fileName, null);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public static String loadTextFile(File fileName, String defaultContents) throws IOException {
    try {
      return loadTextFile(fileName.getPath(), defaultContents);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  
  public static String loadTextFile(Reader reader) throws IOException {
    StringBuilder builder = new StringBuilder();
    try {
      char[] buffer = new char[1024];
      int n;
      while (-1 != (n = reader.read(buffer)))
        builder.append(buffer, 0, n);
        
    } finally {
      reader.close();
    }
    return builder.toString();
  }
static List<String> codeTokens(List<String> tok) {
  return codeTokensOnly(tok);
}
static int max(int a, int b) {
  return Math.max(a, b);
}

static long max(int a, long b) {
  return Math.max((long) a, b);
}

static long max(long a, long b) {
  return Math.max(a, b);
}

static double max(int a, double b) {
  return Math.max((double) a, b);
}

static int max(Collection<Integer> c) {
  int x = Integer.MIN_VALUE;
  for (int i : c) x = max(x, i);
  return x;
}

static double max(double[] c) {
  if (c.length == 0) return Double.MIN_VALUE;
  double x = c[0];
  for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
  return x;
}

  // varargs assignment fixer for a single string array argument
  static Object call(Object o, String method, String[] arg) {
    return call(o, method, new Object[] {arg});
  }
  
  static Object call(Object o, String method, Object... args) {
    try {
      if (o instanceof Class) {
        Method m = call_findStaticMethod((Class) o, method, args, false);
        m.setAccessible(true);
        return m.invoke(null, args);
      } else {
        Method m = call_findMethod(o, method, args, false);
        m.setAccessible(true);
        return m.invoke(o, args);
      }
    } catch (Exception e) {
      throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
    }
  }

  static Method call_findStaticMethod(Class c, String method, Object[] args, boolean debug) {
    Class _c = c;
    while (c != null) {
      for (Method m : c.getDeclaredMethods()) {
        if (debug)
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
        if (!m.getName().equals(method)) {
          if (debug) System.out.println("Method name mismatch: " + method);
          continue;
        }

        if ((m.getModifiers() & Modifier.STATIC) == 0 || !call_checkArgs(m, args, debug))
          continue;

        return m;
      }
      c = c.getSuperclass();
    }
    throw new RuntimeException("Method '" + method + "' (static) with " + args.length + " parameter(s) not found in " + _c.getName());
  }

  static Method call_findMethod(Object o, String method, Object[] args, boolean debug) {
    Class c = o.getClass();
    while (c != null) {
      for (Method m : c.getDeclaredMethods()) {
        if (debug)
          System.out.println("Checking method " + m.getName() + " with " + m.getParameterTypes().length + " parameters");;
        if (m.getName().equals(method) && call_checkArgs(m, args, debug))
          return m;
      }
      c = c.getSuperclass();
    }
    throw new RuntimeException("Method '" + method + "' (non-static) with " + args.length + " parameter(s) not found in " + o.getClass().getName());
  }

  private static boolean call_checkArgs(Method m, Object[] args, boolean debug) {
    Class<?>[] types = m.getParameterTypes();
    if (types.length != args.length) {
      if (debug)
        System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
      return false;
    }
    for (int i = 0; i < types.length; i++)
      if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
        if (debug)
          System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
        return false;
      }
    return true;
  }


static boolean isTrue(Object o) {
  return booleanValue(o);
}
static String structureOrText(Object o) {
  return o instanceof String ? (String) o : structure(o);
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile StringBuffer print_log = local_log; // might be redirected, e.g. to main bot

// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;

static void print() {
  print("");
}

static void print(Object o) {
  String s = String.valueOf(o) + "\n";
  StringBuffer loc = local_log;
  StringBuffer buf = print_log;
  int loc_max = print_log_max;
  if (buf != loc && buf != null) {
    print_append(buf, s, print_log_max);
    loc_max = local_log_max;
  }
  if (loc != null) 
    print_append(loc, s, loc_max);
  System.out.print(s);
}

static void print(long l) {
  print(String.valueOf(l));
}

static void print_append(StringBuffer buf, String s, int max) {
  synchronized(buf) {
    buf.append(s);
    max /= 2;
    if (buf.length() > max) try {
      int newLength = max/2;
      int ofs = buf.length()-newLength;
      String newString = buf.substring(ofs);
      buf.setLength(0);
      buf.append("[...] ").append(newString);
    } catch (Exception e) {
      buf.setLength(0);
    }
  }
}
static String htmlEncode(String s) {
  return htmlencode(s);
}
static Object getOpt(Object o, String field) {
  if (o instanceof String) o = getBot ((String) o);
  if (o == null) return null;
  if (o instanceof Class) return getOpt((Class) o, field);
  
  if (o.getClass().getName().equals("main$DynamicObject"))
    return call(getOpt_raw(o, "fieldValues"), "get", field);
  
  if (o instanceof Map) return ((Map) o).get(field);
  
  return getOpt_raw(o, field);
}

static Object getOpt_raw(Object o, String field) {
  try {
    Field f = getOpt_findField(o.getClass(), field);
    if (f == null) return null;
    f.setAccessible(true);
    return f.get(o);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Object getOpt(Class c, String field) {
  try {
    Field f = getOpt_findStaticField(c, field);
    if (f == null) return null;
    f.setAccessible(true);
    return f.get(null);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Field getOpt_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}

static Field getOpt_findField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  return null;
}
// best guess for our main URL (with trailing /)
// should improve (return actual server name!)
static String mainURL() {
  Object bot = getBot("#1002596"); // System URL Bot
  if (bot != null) try { 
    String url = (String) call(bot, "getBaseURL");
    if (nempty(url))
      return addSlash(url);
  } catch (Throwable __e) { printStackTrace(__e); }
  int port = webServerPort();
  return "http://" + myBestIP() + (port == 80 ? "" : ":" + port) + "/";
}


static String htag(String tag, String contents, Object... params) {
  StringBuilder buf = new StringBuilder();
  buf.append("<" + tag);
  for (int i = 0; i < l(params); i += 2) {
    Object val = get(params, i+1);
    if (val != null) {
      String s = str(val);
      if (!empty(s))
        buf.append(" " + get(params, i) + "=" + htmlQuote(s));
    }
  }
  buf.append(">" + contents + "</" + tag + ">");
  return str(buf);
}

// the usual convenience shortcut for StringBuilder instead of string...
static String htag(String tag, StringBuilder contents, Object... params) {
  return htag(tag, str(contents), params);
}

// same with StringBuffer
static String htag(String tag, StringBuffer contents, Object... params) {
  return htag(tag, str(contents), params);
}
// hopefully covers all cases :)
static String safeSubstring(String s, int x, int y) {
  if (s == null) return null;
  if (x < 0) x = 0;
  if (x > s.length()) return "";
  if (y < x) y = x;
  if (y > s.length()) y = s.length();
  return s.substring(x, y);
}

static String safeSubstring(String s, int x) {
  return safeSubstring(s, x, l(s));
}

static Object getBot(String botID) {
  return call(getMainBot(), "getBot", botID);
}

static boolean equalsIgnoreCase(String a, String b) {
  return a == null ? b == null : a.equalsIgnoreCase(b);
}
static String shortenSnippetID(String snippetID) {
  if (snippetID.startsWith("#"))
    snippetID = snippetID.substring(1);
  String httpBlaBla = "http://tinybrain.de/";
  if (snippetID.startsWith(httpBlaBla))
    snippetID = snippetID.substring(httpBlaBla.length());
  return "" + parseLong(snippetID);
}
static <A> List<A> cloneList(List<A> l) {
  //O mutex = getOpt(l, "mutex");
  /*if (mutex != null)
    synchronized(mutex) {
      ret new ArrayList<A>(l);
    }
  else
    ret new ArrayList<A>(l);*/
  // assume mutex is equal to collection, which will be true unless you explicitly pass a mutex to synchronizedList() which no one ever does.
  synchronized(l) {
    return new ArrayList<A>(l);
  }
}
static Lisp nlUnbracket(Lisp tree) {
  while (tree.is("[]", 1))
    tree = tree.get(0);
  return tree;
}
static String programID;

static String getProgramID() {
  return programID;
}

// TODO: ask JavaX instead
static String getProgramID(Class c) {
  return or((String) getOpt(c, "programID"), "?");
}

static String getProgramID(Object o) {
  return getProgramID(getMainClass(o));
}
static class DynamicObject {
  String className;
  Map<String, Object> fieldValues = new TreeMap<String, Object>();
}

static Object unstructure(String text) {
  return unstructure(text, false);
}

// actually it's now almost the same as jsonDecode :)
static Object unstructure(String text, final boolean allDynamic) {
  final List<String> tok = javaTok(text);
  
  class X {
    int i = 1;

    Object parse() {
      String t = tok.get(i);
      if (t.startsWith("\"")) {
        String s = unquote(tok.get(i));
        i += 2;
        return s;
      }
      if (t.equals("hashset"))
        return parseHashSet();
      if (t.equals("treeset"))
        return parseTreeSet();
      if (t.equals("hashmap"))
        return parseHashMap();
      if (t.equals("{"))
        return parseMap();
      if (t.equals("["))
        return parseList();
      if (t.equals("array"))
        return parseArray();
      if (t.equals("class"))
        return parseClass();
      if (t.equals("bigint"))
        return parseBigInt();
      if (t.equals("d"))
        return parseDouble();
      if (t.equals("l"))
        return parseLisp();
      if (t.equals("null")) {
        i += 2; return null;
      }
      if (t.equals("false")) {
        i += 2; return false;
      }
      if (t.equals("true")) {
        i += 2; return true;
      }
      if (t.equals("-")) {
        t = tok.get(i+2);
        i += 4;
        t = dropSuffix("L", t);
        long l = -Long.parseLong(t);
        return l == (int) l ? (int) l : l;
      }
      if (isInteger(t) || isLongConstant(t)) {
        i += 2;
        t = dropSuffix("L", t);
        long l = Long.parseLong(t);
        return l == (int) l ? (int) l : l;
      }
      if (isJavaIdentifier(t)) {
        Class c = allDynamic ? null : findClass(t);
        DynamicObject dO = null;
        Object o = null;
        if (c != null)
          o = nuObject(c);
        else {
          dO = new DynamicObject();
          dO.className = t;
        }
        i += 2;
        if (i < tok.size() && tok.get(i).equals("(")) {
          consume("(");
          while (!tok.get(i).equals(")")) {
            // It's like parsing a map.
            //Object key = parse();
            //if (tok.get(i).equals(")"))
            //  key = onlyField();
            String key = unquote(tok.get(i));
            i += 2;
            consume("=");
            Object value = parse();
            if (o != null)
              setOpt(o, key, value);
            else
              dO.fieldValues.put(key, value);
            if (tok.get(i).equals(",")) i += 2;
          }
          consume(")");
        }
        return o != null ? o : dO;
      }
      throw new RuntimeException("Unknown token " + (i+1) + ": " + t);
    }
    
    Object parseSet(Set set) {
      set.addAll((List) parseList());
      return set;
    }
    
    Object parseLisp() {
      consume("l");
      consume("(");
      List list = new ArrayList();
      while (!tok.get(i).equals(")")) {
        list.add(parse());
        if (tok.get(i).equals(",")) i += 2;
      }
      consume(")");
      return newObject("main$Lisp", (String) list.get(0), subList(list, 1));
    }
    
    Object parseList() {
      consume("[");
      List list = new ArrayList();
      while (!tok.get(i).equals("]")) {
        list.add(parse());
        if (tok.get(i).equals(",")) i += 2;
      }
      consume("]");
      return list;
    }
    
    Object parseArray() {
      consume("array");
      consume("{");
      List list = new ArrayList();
      while (!tok.get(i).equals("}")) {
        list.add(parse());
        if (tok.get(i).equals(",")) i += 2;
      }
      consume("}");
      return list.toArray();
    }
    
    Object parseClass() {
      consume("class");
      consume("(");
      String name = tok.get(i);
      i += 2;
      consume(")");
      Class c = allDynamic ? null : findClass(name);
      if (c != null) return c;
      DynamicObject dO = new DynamicObject();
      dO.className = "java.lang.Class";
      dO.fieldValues.put("name", name);
      return dO;
    }
    
    Object parseBigInt() {
      consume("bigint");
      consume("(");
      String val = tok.get(i);
      i += 2;
      if (eq(val, "-")) {
        val = "-" + tok.get(i);
        i += 2;
      }
      consume(")");
      return new BigInteger(val);
    }
    
    Object parseDouble() {
      consume("d");
      consume("(");
      String val = unquote(tok.get(i));
      i += 2;
      consume(")");
      return Double.parseDouble(val);
    }
    
    Object parseHashMap() {
      consume("hashmap");
      return parseMap(new HashMap());
    }
    
    Object parseHashSet() {
      consume("hashset");
      return parseSet(new HashSet());
    }
    
    Object parseTreeSet() {
      consume("treeset");
      return parseSet(new TreeSet());
    }
    
    Object parseMap() {
      return parseMap(new TreeMap());
    }
    
    Object parseMap(Map map) {
      consume("{");
      while (!tok.get(i).equals("}")) {
        Object key = unstructure(tok.get(i));
        i += 2;
        consume("=");
        Object value = parse();
        map.put(key, value);
        if (tok.get(i).equals(",")) i += 2;
      }
      consume("}");
      return map;
    }
    
    void consume(String s) {
      if (!tok.get(i).equals(s)) {
        String prevToken = i-2 >= 0 ? tok.get(i-2) : "";
        String nextTokens = join(tok.subList(i, Math.min(i+4, tok.size())));
        fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")");
      }
      i += 2;
    }
  }
  
  return new X().parse();
}
static int min(int a, int b) {
  return Math.min(a, b);
}

static double min(double[] c) {
  double x = Double.MAX_VALUE;
  for (double d : c) x = Math.min(x, d);
  return x;
}

static int webServerPort() {
  Object port = getOpt(getMainBot(), "webServerPort");
  return port == null ? 0 : (int) port;
}
  static void set(Object o, String field, Object value) {
    if (o instanceof Class) set((Class) o, field, value);
    else try {
      Field f = set_findField(o.getClass(), field);
      smartSet(f, o, value);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  static void set(Class c, String field, Object value) {
    try {
      Field f = set_findStaticField(c, field);
      smartSet(f, null, value);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  static Field set_findField(Class<?> c, String field) {
    for (Field f : c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
  }
  
  static Field set_findStaticField(Class<?> c, String field) {
    for (Field f : c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
        return f;
    throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
  }
static boolean isIdentifier(String s) {
  return isJavaIdentifier(s);
}
static boolean instanceOf(Object o, String className) {
  if (o == null) return false;
  String c = o.getClass().getName();
  return eq(c, className) || eq(c, "main$" + className);
}
static File programDir() {
  return programDir(getProgramID());
}

static File programDir(String snippetID) {
  return new File(javaxDataDir(), formatSnippetID(snippetID));
}

// extended over Class.isInstance() to handle primitive types
static boolean isInstanceX(Class type, Object arg) {
  if (type == boolean.class) return arg instanceof Boolean;
  if (type == int.class) return arg instanceof Integer;
  if (type == long.class) return arg instanceof Long;
  if (type == float.class) return arg instanceof Float;
  if (type == short.class) return arg instanceof Short;
  if (type == char.class) return arg instanceof Character;
  if (type == byte.class) return arg instanceof Byte;
  if (type == double.class) return arg instanceof Double;
  return type.isInstance(arg);
}
static boolean isNonNegativeInteger(String s) {
  return s != null && Pattern.matches("\\d+", s);
}
// make a lisp form
static Lisp lisp(String head, Object... args) {
  Lisp l = new Lisp(head);
  for (Object o : args)
    l.add(o);
  return l;
}

static Lisp lisp(String head, List args) {
  return new Lisp(head, args);
}

static boolean warn_on = false;

static void warn(String s) {
  if (warn_on)
    print("Warn: " + s);
}

static void warn(String s, List<String> warnings) {
  warn(s);
  if (warnings != null)
    warnings.add(s);
}
  /** writes safely (to temp file, then rename) */
  public static void saveTextFile(String fileName, String contents) throws IOException {
    File file = new File(fileName);
    File parentFile = file.getParentFile();
    if (parentFile != null)
      parentFile.mkdirs();
    String tempFileName = fileName + "_temp";
    if (contents != null) {
      FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
      PrintWriter printWriter = new PrintWriter(outputStreamWriter);
      printWriter.print(contents);
      printWriter.close();
    }
    
    if (file.exists() && !file.delete())
      throw new IOException("Can't delete " + fileName);

    if (contents != null)
      if (!new File(tempFileName).renameTo(file))
        throw new IOException("Can't rename " + tempFileName + " to " + fileName);
  }
  
  public static void saveTextFile(File fileName, String contents) {
    try {
      saveTextFile(fileName.getPath(), contents);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

// This is made for NL parsing.
// It's javaTok extended with "..." token, "$n" and "#n" and
// special quotes (which are converted to normal ones).

static List<String> javaTokPlusPeriod(String s) {
  List<String> tok = new ArrayList<String>();
  int l = s.length();
  
  int i = 0;
  while (i < l) {
    int j = i;
    char c; String cc;
    
    // scan for whitespace
    while (j < l) {
      c = s.charAt(j);
      cc = s.substring(j, Math.min(j+2, l));
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        ++j;
      else if (cc.equals("/*")) {
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
        j = Math.min(j+2, l);
      } else if (cc.equals("//")) {
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
      } else
        break;
    }
    
    tok.add(s.substring(i, j));
    i = j;
    if (i >= l) break;
    c = s.charAt(i);
    cc = s.substring(i, Math.min(i+2, l));

    // scan for non-whitespace
    if (c == '\u201C' || c == '\u201D') c = '"'; // normalize quotes
    if (c == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        char _c = s.charAt(j);
        if (_c == '\u201C' || _c == '\u201D') _c = '"'; // normalize quotes
        if (_c == opener) {
          ++j;
          break;
        } else if (s.charAt(j) == '\\' && j+1 < l)
          j += 2;
        else
          ++j;
      }
      if (j-1 >= i+1) {
        tok.add(opener + s.substring(i+1, j-1) + opener);
        i = j;
        continue;
      }
    } else if (Character.isJavaIdentifierStart(c))
      do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); // for things like "this one's"
    else if (Character.isDigit(c))
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    else if (cc.equals("[[")) {
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
      j = Math.min(j+2, l);
    } else if (cc.equals("[=") && i+2 < l && s.charAt(i+2) == '[') {
      do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]"));
      j = Math.min(j+3, l);
    } else if (s.substring(j, Math.min(j+3, l)).equals("..."))
      j += 3;
    else if (c == '$' || c == '#')
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
    else
      ++j;

    tok.add(s.substring(i, j));
    i = j;
  }
  
  if ((tok.size() % 2) == 0) tok.add("");
  return tok;
}

static boolean booleanValue(Object o) {
  return eq(true, o);
}
static String myBestIP() {
  return first(getMyIPs());
}
static void logQuoted(String logFile, String line) {
  logQuoted(getProgramFile(logFile), line);
}

static void logQuoted(File logFile, String line) {
  appendToFile(logFile, quote(line) + "\n");
}
static <A> List<A> synchroList() {
  return Collections.synchronizedList(new ArrayList<A>());
}

static <A> List<A> synchroList(List<A> l) {
  return Collections.synchronizedList(l);
}

static File getProgramFile(String progID, String fileName) {
  return new File(getProgramDir(progID), fileName);
}

static File getProgramFile(String fileName) {
  return getProgramFile(getProgramID(), fileName);
}

static List<String> dropPunctuation_keep = litlist("*", "<", ">");

static List<String> dropPunctuation(List<String> tok) {
  tok = new ArrayList<String>(tok);
  for (int i = 1; i < tok.size(); i += 2) {
    String t = tok.get(i);
    if (t.length() == 1 && !Character.isLetter(t.charAt(0)) && !Character.isDigit(t.charAt(0)) && !dropPunctuation_keep.contains(t)) {
      tok.set(i-1, tok.get(i-1) + tok.get(i+1));
      tok.remove(i);
      tok.remove(i);
      i -= 2;
    }
  }
  return tok;
}

static String dropPunctuation(String s) {
  return join(dropPunctuation(nlTok(s)));
}
static <A> A popLast(List<A> l) {
  return liftLast(l);
}
static String addSlash(String s) {
  return s.endsWith("/") ? s : s + "/";
}

// currently finds only inner classes of class "main"
// returns null on not found
// this is the simple version that is not case-tolerant
static Class findClass(String name) {
  try {
    return Class.forName("main$" + name);
  } catch (ClassNotFoundException e) {
    return null;
  }
}
static boolean isJavaIdentifier(String s) {
  if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0)))
    return false;
  for (int i = 1; i < s.length(); i++)
    if (!Character.isJavaIdentifierPart(s.charAt(i)))
      return false;
  return true;
}
static <A> A liftLast(List<A> l) {
  if (l.isEmpty()) return null;
  int i = l(l)-1;
  A a = l.get(i);
  l.remove(i);
  return a;
}
static String htmlQuote(String s) {
  return "\"" + htmlencode(s) + "\"";
}
static Object newObject(Class c, Object... args) {
  return nuObject(c, args);
}

static Object newObject(String className, Object... args) {
  return nuObject(className, args);
}

static boolean isLongConstant(String s) {
  if (!s.endsWith("L")) return false;
  s = s.substring(0, l(s)-1);
  return isInteger(s);
}
static Class getMainClass() { try {
 
  return Class.forName("main");

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static Class getMainClass(Object o) { try {
 
  return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main");

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static File getProgramDir() {
  return programDir();
}

static File getProgramDir(String snippetID) {
  return programDir(snippetID);
}
static boolean isInteger(String s) {
  return s != null && Pattern.matches("\\-?\\d+", s);
}
 // Let's just generally synchronize this to be safe.
 static synchronized void appendToFile(String path, String s) { try {
 
    new File(path).getParentFile().mkdirs();
    //print("[Logging to " + path + "]");
    Writer writer = new BufferedWriter(new OutputStreamWriter(
      new FileOutputStream(path, true), "UTF-8"));
    writer.write(s);
    writer.close();
  
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
  
  static void appendToFile(File path, String s) {
    appendToFile(path.getPath(), s);
  }

static String formatSnippetID(String id) {
  return "#" + parseSnippetID(id);
}

static String formatSnippetID(long id) {
  return "#" + id;
}
static List<String> getMyIPs() { try {
 
  TreeSet<String> ips = new TreeSet<String>();
  Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
  while (interfaces.hasMoreElements()) {
    NetworkInterface iface = interfaces.nextElement();
    // filters out 127.0.0.1 and inactive interfaces
    if (iface.isLoopback() || !iface.isUp())
        continue;

    Enumeration<InetAddress> addresses = iface.getInetAddresses();
    while(addresses.hasMoreElements()) {
      InetAddress addr = addresses.nextElement();
      String ip = addr.getHostAddress();
      if (ip.startsWith("127.")) continue;
      ips.add(ip);
    }
  }
  return new ArrayList<String>(ips);

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
static Object nuObject(String className, Object... args) { try {
 
  return nuObject(Class.forName(className), args);

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static Object nuObject(Class c, Object... args) { try {
 
  Constructor m = nuObject_findConstructor(c, args);
  m.setAccessible(true);
  return m.newInstance(args);

} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}

static Constructor nuObject_findConstructor(Class c, Object... args) {
  for (Constructor m : c.getDeclaredConstructors()) {
    if (!nuObject_checkArgs(m.getParameterTypes(), args, false))
      continue;
    return m;
  }
  throw new RuntimeException("Constructor with " + args.length + " matching parameter(s) not found in " + c.getName());
}

 static boolean nuObject_checkArgs(Class[] types, Object[] args, boolean debug) {
    if (types.length != args.length) {
      if (debug)
        System.out.println("Bad parameter length: " + args.length + " vs " + types.length);
      return false;
    }
    for (int i = 0; i < types.length; i++)
      if (!(args[i] == null || isInstanceX(types[i], args[i]))) {
        if (debug)
          System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]);
        return false;
      }
    return true;
  }
static File javaxDataDir_dir; // can be set to work on different base dir

static File javaxDataDir() {
  return javaxDataDir_dir != null ? javaxDataDir_dir : new File(userHome(), "JavaX-Data");
}
static double parseDouble(String s) {
  return Double.parseDouble(s);
}
static void smartSet(Field f, Object o, Object value) throws Exception {
  f.setAccessible(true);
  
  // take care of common case (long to int)
  if (f.getType() == int.class && value instanceof Long)
    value = ((Long) value).intValue();
    
  f.set(o, value);
}
static List<String> nlTok(String s) {
  return javaTokPlusPeriod(s);
}
  static void setOpt(Object o, String field, Object value) {
    if (o instanceof Class) setOpt((Class) o, field, value);
    else try {
      Field f = setOpt_findField(o.getClass(), field);
      if (f != null)
        smartSet(f, o, value);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  static void setOpt(Class c, String field, Object value) {
    try {
      Field f = setOpt_findStaticField(c, field);
      if (f != null)
        smartSet(f, null, value);
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
  
  static Field setOpt_findField(Class<?> c, String field) {
    for (Field f : c.getDeclaredFields())
      if (f.getName().equals(field))
        return f;
    return null;
  }
  
  static Field setOpt_findStaticField(Class<?> c, String field) {
    for (Field f : c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
        return f;
    return null;
  }

static String _userHome;
static String userHome() {
  if (_userHome == null) {
    if (isAndroid())
      _userHome = "/storage/sdcard0/";
    else
      _userHome = System.getProperty("user.home");
    //System.out.println("userHome: " + _userHome);
  }
  return _userHome;
}


static boolean isAndroid() { return System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0; }

// a Lisp-like form
static class Lisp implements Iterable<Lisp> {
  String head;
  List<Lisp> args = new ArrayList<Lisp>();
  
  Object more; // additional info, user-defined
  
  Lisp() {}
  Lisp(String head) {
  this.head = head;}
  Lisp(String head, Lisp... args) {
  this.head = head;
    this.args.addAll(asList(args));
  }
  Lisp(String head, List args) {
  this.head = head;
    for (Object arg : args) add(arg);
  }
  
  // INEFFICIENT
  public String toString() {
    if (args.isEmpty())
      return quoteIfNotIdentifier(head);
    List<String> bla = new ArrayList<String>();
    for (Lisp a : args)
      bla.add(a.toString());
    String inner = join(", ", bla);
    if (head.equals(""))
      return "{" + inner + "}"; // list
    else
      return quoteIfNotIdentifier(head) + "(" + inner + ")";
  }

  String raw() {
    if (!isEmpty ()) fail("not raw: " + this);
    return head;
  }
  
  Lisp add(Lisp l) {
    args.add(l);
    return this;
  }
  
  Lisp add(String s) {
    args.add(new Lisp(s));
    return this;
  }
  
  Lisp add(Object o) {
    if (o instanceof Lisp) add((Lisp) o);
    else if (o instanceof String) add((String) o);
    else fail("Bad argument type: " + structure(o));
    return this;
  }
  
  int size() {
    return args.size();
  }
  
  boolean isEmpty() {
    return args.isEmpty();
  }
  
  boolean isLeaf() {
    return args.isEmpty();
  }
  
  Lisp get(int i) {
    return args.get(i);
  }
  
  String getString(int i) {
    return get(i).head;
  }
  
  String s(int i) {
    return getString(i);
  }
  
  boolean isA(String head) {
    return eq(head, this.head);
  }

  boolean is(String head, int size) {
    return isA(head) && size() == size;
  }
  
  boolean is(String head) {
    return isA(head);
  }
  
  boolean is(String... heads) {
    return asList(heads).contains(head);
  }
  
  // check head for one of these (ignore case)
  boolean isic(String... heads) {
    return containsIgnoreCase(heads, head);
  }
  
  public Iterator<Lisp> iterator() {
    return args.iterator();
  }
  
  Lisp subList(int fromIndex, int toIndex) {
    Lisp l = new Lisp(head);
    l.args.addAll(args.subList(fromIndex, toIndex)); // better to copy here I guess - safe
    return l;
  }

  public boolean equals(Object o) {
    if (o == null || o.getClass() != Lisp.class) return false;
    Lisp l = (Lisp) ( o);
    return eq (head, l.head) && eq(args, l.args);
  }
  
  Lisp addAll(List args) {
    for (Object arg : args) add(arg);
    return this;
  }
  
  String unquoted() {
    return unquote(raw());
  }
  
  String unq() {
    return unquoted();
  }
}


  static class Matches {
    String[] m;
    String get(int i) { return m[i]; }
    String unq(int i) { return unquote(m[i]); }
    String fsi(int i) { return formatSnippetID(unq(i)); }
    boolean bool(int i) { return "true".equals(unq(i)); }
    String rest() { return m[m.length-1]; } // for matchStart
    int psi(int i) { return Integer.parseInt(unq(i)); }
  }


static class MultiMap<A,B> {
  Map<A, List<B>> data = new HashMap<A, List<B>>();
  
  MultiMap() {}
  MultiMap(MultiMap<A, B> map) { putAll(map); }

  public void put(A key, B value) {
    List<B> list = data.get(key);
    if (list == null)
      data.put(key, list = new ArrayList<B>());
    list.add(value);
  }

  public void addAll(A key, Collection<B> values) {
    putAll(key, values);
  }
  
  public void addAllIfNotThere(A key, Collection<B> values) {
    for (B value : values)
      setPut(key, value);
  }
  
  void setPut(A key, B value) {
    if (!containsPair(key, value))
      put(key, value);
  }
  
  boolean containsPair(A key, B value) {
    return get(key).contains(value);
  }
  
  public void putAll(A key, Collection<B> values) {
    for (B value : values)
      put(key, value);
  }

  void removeAll(A key, Collection<B> values) {
    for (B value : values)
      remove(key, value);
  }
  
  public List<B> get(A key) {
    List<B> list = data.get(key);
    return list == null ? Collections.<B> emptyList() : list;
  }

  // returns actual mutable live list
  // creates the list if not there
  public List<B> getActual(A key) {
    List<B> list = data.get(key);
    if (list == null)
      data.put(key, list = litlist());
    return list;
  }
 
  void clean(A key) {
    List<B> list = data.get(key);
    if (list != null && list.isEmpty())
      data.remove(key);
  }

  public Set<A> keySet() {
    return data.keySet();
  }

  public Set<A> keys() {
    return data.keySet();
  }

  public void remove(A key) {
    data.remove(key);
  }

  public void remove(A key, B value) {
    List<B> list = data.get(key);
    if (list != null) {
      list.remove(value);
      if (list.isEmpty())
        data.remove(key);
    }
  }

  public void clear() {
    data.clear();
  }

  public boolean containsKey(A key) {
    return data.containsKey(key);
  }

  public B getFirst(A key) {
    List<B> list = get(key);
    return list.isEmpty() ? null : list.get(0);
  }
  
  public void putAll(MultiMap<A, B> map) {
    for (A key : map.keySet())
      putAll(key, map.get(key));
  }
  
  // note: expensive operation
  int size() {
    int n = 0;
    for (List l : data.values())
      n += l(l);
    return n;
  }
  
  // expensive operation
  List<A> reverseGet(B b) {
    List<A> l = new ArrayList<A>();
    for (A key : data.keySet())
      if (data.get(key).contains(b))
        l.add(key);
    return l;
  }
}

// a persistent map using a clever combination of persisting and logging
// (well, only logging as of now)
// Note: don't put in static initializer (programID not set yet)
static class PersistentMap<A, B> extends AbstractMap<A, B> {
  Map<A, B> m = new TreeMap<A, B>();
  File file;
  String id; // unique ID of this map
  
  PersistentMap(String fileName) {
    this(getProgramFile(fileName));
  }
  
  PersistentMap(String progID, String fileName) {
    this(getProgramFile(progID, fileName));
  }
  
  PersistentMap(File file) {
  this.file = file;
    for (String s : scanLog(file)) try { 
      List l = (List) ( unstructure(s));
      Object cmd = l.get(0);
      if (eq(cmd, "add"))
        m.put((A) l.get(1), (B) l.get(2));
      else if (eq(cmd, "remove"))
        m.remove((A) l.get(1));
      else if (eq(cmd, "id"))
        id = (String) l.get(1);
      else
        print("Unknown command in log: " + l.get(0));
    } catch (Throwable __e) { printStackTrace(__e); }
    
    if (id == null) {
      id = makeRandomID(12);
      logQuoted(file, structure(litlist("id", id)));
    }
  }
  
  // just delegates
  
  public int size() {
    return m.size();
  }
  
  public B get(Object a) {
    return m.get(a);
  }
  
  // TODO: calling remove() in the iterator will have unpersisted
  // effects.
  public Set<Map.Entry<A,B>> entrySet() {
    return m.entrySet();
  }
  
  // delegates with logging

  public B put(A a, B b) {
    B c = m.get(a);
    if (neq(b, c)) {
      m.put(a, b);
      logQuoted(file, structure(litlist("add", a, b)));
    }
    return c;
  }
  
  public B remove(Object a) {
    B result = m.remove(a);
    logQuoted(file, structure(litlist("remove", a)));
    return result;
  }
  
  // unique id of this map
  public String id() {
    return id;
  }
}

static <A> ArrayList<A> asList(A[] a) {
  return new ArrayList<A>(Arrays.asList(a));
}

static ArrayList<Integer> asList(int[] a) {
  ArrayList<Integer> l = new ArrayList();
  for (int i : a) l.add(i);
  return l;
}

static <A> ArrayList<A> asList(Set<A> s) {
  return s == null ? new ArrayList() : new ArrayList(s);
}
static void printStackTrace(Throwable e) {
  // we go to system.out now - system.err is nonsense
  print(getStackTrace(e));
}

static void printStackTrace() {
  printStackTrace(new Throwable());
}
static String quoteIfNotIdentifier(String s) {
  if (s == null) return null;
  return isJavaIdentifier(s) ? s : quote(s);
}
static List<String> scanLog(String progID, String fileName) {
  return scanLog(getProgramFile(progID, fileName));
}

static List<String> scanLog(String fileName) {
  return scanLog(getProgramFile(fileName));
}

static List<String> scanLog(File file) {
  List<String> l = new ArrayList<String>();
  for (String s : toLines(file))
    if (isQuoted(s))
      l.add(unquote(s));
  return l;
}
static boolean equals(Object a, Object b) {
  return a == null ? b == null : a.equals(b);
}
// also logs full exception to console
static String exceptionToUser(Throwable e) {
  return throwableToUser(e);
}
static String makeRandomID(int length) {
  Random random = new Random();
  char[] id = new char[length];
  for (int i = 0; i < id.length; i++)
    id[i] = (char) ((int) 'a' + random.nextInt(26));
  return new String(id);
}
static String getString(Map map, Object key) {
  return map == null ? null : (String) map.get(key);
}

static String getString(List l, int idx) {
  return (String) get(l, idx);
}

static String getString(Object o, Object key) {
  if (o instanceof Map) return getString((Map) o, key);
  if (key instanceof String)
    return (String) get(o, (String) key);
  throw fail("Not a string key: " + getClassName(key));
}
static boolean containsIgnoreCase(List<String> l, String s) {
  for (String x : l)
    if (eqic(x, s))
      return true;
  return false;
}

static boolean containsIgnoreCase(String[] l, String s) {
  for (String x : l)
    if (eqic(x, s))
      return true;
  return false;
}

static String getStackTrace(Throwable throwable) {
  StringWriter writer = new StringWriter();
  throwable.printStackTrace(new PrintWriter(writer));
  return writer.toString();
}
// supports the usual quotings (', ", variable length double brackets)
static boolean isQuoted(String s) {
  if (s.startsWith("'") || s.startsWith("\"")) return true;
  if (!s.startsWith("[")) return false;
  int i = 1;
  while (i < s.length() && s.charAt(i) == '=') ++i;
  return i < s.length() && s.charAt(i) == '[';
  //return Pattern.compile("^\\[=*\\[").matcher(s).find();
}
static String getClassName(Object o) {
  return o == null ? "null" : o.getClass().getName();
}
static boolean eqic(String a, String b) {
  if ((a == null) != (b == null)) return false;
  if (a == null) return true;
  return a.equalsIgnoreCase(b);
}
// also logs full exception to console
static String throwableToUser(Throwable e) {
  return formatExceptionForUser(e);
}

// also logs full exception to console
static String formatExceptionForUser(Throwable e) {
  printStackTrace(e);
  String msg = unnull(e.getMessage());
  if (msg.indexOf("Error") < 0 && msg.indexOf("Exception") < 0)
    return dropPrefix("java.lang.", str(e));
  else
    return msg;
}

static String dropPrefix(String prefix, String s) {
  return s.startsWith(prefix) ? s.substring(l(prefix)) : s;
}
}