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

public class main {
static class MultiMap<A,B> {
  Map<A, List<B>> data = new HashMap<A, List<B>>();

  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 putAll(A key, Collection<B> values) {
    for (B value : values)
      put(key, value);
  }

  public List<B> get(A key) {
    List<B> list = data.get(key);
    return list == null ? Collections.<B> emptyList() : list;
  }

  public Set<A> keySet() {
    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);
  }
}

// If the pope is in the house and the house is in the world, then is the pope in the world?


public static void main(String[] args) throws Exception {
  makeBot("Is In Bot.");
}

static String answer(String s) {
  MultiMap<String, String> isIn = new MultiMap<String, String>();
  
  Matches m = new Matches();
  if (
    flexmatch("if the * is in the *, and the * is in the *, then is the * in the *?", s, m) ||
    flexmatch("if the * is in the *, and the * is in the *, is the * in the *?", s, m)) {
    String a = m.unq(0), b = m.unq(1), c = m.unq(2), d = m.unq(3), e = m.unq(4), f = m.unq(5);
    isIn.put(a, b);
    isIn.put(c, d);
    return makeAnswer(isIn, e, f);
  }
  
  if (
    flexmatch("if the * is in the *, and the * is in the *, and the * is in the *, then is the * in the *?", s, m) ||
    flexmatch("if the * is in the *, and the * is in the *, and the * is in the *, is the * in the *?", s, m)) {
    String a = m.unq(0), b = m.unq(1), c = m.unq(2), d = m.unq(3), e = m.unq(4), f = m.unq(5), g = m.unq(6), h = m.unq(7);
    isIn.put(a, b);
    isIn.put(c, d);
    isIn.put(e, f);
    return makeAnswer(isIn, g, h);
  }
  return null;
}

static String makeAnswer(MultiMap<String, String> isIn, String e, String f) {
  boolean result = hasTransitive(isIn, e, f);
  if (result)
    return format("Yes, the * is in the *.", e, f);
  else
    return format("No, the * is not in the *.", e, f);
}

static boolean flexmatch(String pat, String s, Matches m) {
  pat = simplify1(pat); s = simplify1(s);
  if (match(pat, s, m)) return true;
  pat = simplify2(pat); s = simplify2(s);
  return match(pat, s, m);
}
 
// simplifications that seem safe
static String simplify1(String s) {
  s = dropWords(s, "the", "an", "my", "your", "his", "her", "our", "their");
  return replaceWords(s, "are", "is", "on", "in", "am", "is");
}

// simplifications that might fail (because a is a popular noun for this bot, like in "if a is in b and...2)
static String simplify2(String s) {
  return dropWords(s, "a");
}


static String replaceWords(String s, String... replacements) {
  Map<String, String> map = litmap(toLowerCase(replacements));
  List<String> tok = nlTok(s);
  for (int i = 1; i < l(tok); i += 2) {
    String x = map.get(tok.get(i).toLowerCase());
    if (x != null)
      tok.set(i, x);
  }
  return join(tok);
}

static String dropWords(String s, String... words) {
  Set<String> set = asSet(toLowerCase(words));
  List<String> tok = nlTok(s);
  for (int i = 1; i < l(tok); )
    if (set.contains(tok.get(i).toLowerCase()))
      for (int __twice = 0; __twice < 2; __twice++) {  tok.remove(i); }
    else
      i += 2;
  return join(tok);
}

static boolean hasTransitive(MultiMap<String, String> map, String e, String f) {
  return makeHull(map, e).contains(f);
}


  static String format(String pat, Object... args) {
    return format3(pat, args);
  }


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 int makeBot(String greeting) {
  return makeAndroid3(greeting).port;
}

static void makeBot(Android3 a) {
  makeAndroid3(a);
}

static Set<String> asSet(String[] array) {
  return new TreeSet<String>(asList(array));
}

static Set<String> asSet(List<String> l) {
  return new TreeSet<String>(l);
}

static String[] toLowerCase(String[] strings) {
  String[] x = new String[l(strings)];
  for (int i = 0; i < l(strings); i++)
    x[i] = strings[i].toLowerCase();
  return x;
}

static List<String> makeHull(MultiMap<String, String> map, String e) {
  List<String> l = new ArrayList<String>();
  makeHull_impl(map, e, l);
  return l;
}

static void makeHull_impl(MultiMap<String, String> map, String e, List<String> dest) {
  if (dest.contains(e))
    return;
  dest.add(e);
  for (String f : map.get(e))
    makeHull_impl(map, f, dest);
}


static Map litmap(Object... x) {
  TreeMap map = new TreeMap();
  for (int i = 0; i < x.length-1; i += 2)
    map.put(x[i], x[i+1]);
  return map;
}

static List<String> nlTok(String s) {
  return javaTokPlusPeriod(s);
}

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

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

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




  static abstract class DialogIO {
    abstract boolean isStillConnected();
    abstract String readLineNoBlock();
    abstract boolean waitForLine();
    abstract void sendLine(String line);
    abstract boolean isLocalConnection();
    abstract Socket getSocket();
    abstract void close();
    
    int getPort() { return getSocket().getPort(); }
    
    boolean helloRead;
    
    String readLine() {
      waitForLine();
      helloRead = true;
      return readLineNoBlock();
    }
    
    String ask(String s, Object... args) {
      if (!helloRead) readLine();
      if (args.length != 0) s = format3(s, args);
      sendLine(s);
      return readLine();
    }
    
    String askLoudly(String s, Object... args) {
      if (!helloRead) readLine();
      if (args.length != 0) s = format3(s, args);
      print("> " + s);
      sendLine(s);
      String answer = readLine();
      print("< " + answer);
      return answer;
    }
  }
  
  static abstract class DialogHandler {
    abstract void run(DialogIO io);
  } // Dialog classes


static class Android3 {
  String greeting;
  boolean publicOverride; // optionally set this in client
  int startPort = 5000; // optionally set this in client
  Responder responder;
  boolean console = true;
  boolean daemon = false;
  boolean incomingSilent = false;
  boolean useMultiPort = true;
  
  // set by system
  int port;
  long vport;
  DialogHandler handler;
  ServerSocket server;
  
  Android3(String greeting) {
  this.greeting = greeting;}
  Android3() {}
  
  synchronized void dispose() {
    if (server == null) return;
    try {
      server.close();
    } catch (IOException e) {
      print("[internal] " + e);
    }
    server = null;
  }
}

static abstract class Responder {
  abstract String answer(String s, List<String> history);
}

static Android3 makeAndroid3(final String greeting) {
  return makeAndroid3(new Android3(greeting));
}

static Android3 makeAndroid3(final String greeting, Responder responder) {
  Android3 android = new Android3(greeting);
  android.responder = responder;
  return makeAndroid3(android);
}

static Android3 makeAndroid3(final Android3 a) {
  if (a.responder == null)
    a.responder = new Responder() {
      String answer(String s, List<String> history) {
        return callStaticAnswerMethod(s, history);
      }
    };
    
  if (a.useMultiPort) {
    a.vport = addToMultiPort(a.greeting, a.responder);
    if (a.vport == 1)
      makeAndroid3_handleConsole(a);
    return a;
  }
    
  print(a.greeting);
  
  a.handler = makeAndroid3_makeDialogHandler(a);
  a.port = a.daemon
    ? startDialogServerOnPortAboveDaemon(a.startPort, a.handler)
    : startDialogServerOnPortAbove(a.startPort, a.handler);
  a.server = startDialogServer_serverSocket;

  if (a.console && makeAndroid3_consoleInUse()) a.console = false;
  
  if (a.console)
    makeAndroid3_handleConsole(a);

  record(a);
  return a;
}

static void makeAndroid3_handleConsole(final Android3 a) {
  // Console handling stuff
  print("You may also type on this console.");
  Thread _t_0 = new Thread() {
public void run() {
try {

    List<String> history = new ArrayList<String>();
    String line;
    while ((line = readLine()) != null) {
      if ("bye".equals(line)) {
        print("> bye stranger");
        history = new ArrayList<String>();
      } else {
        history.add(line);
        history.add(makeAndroid3_getAnswer(line, history, a)); // prints answer on console too
      }
    }
  } catch (Exception _e) {
  throw _e instanceof RuntimeException ? (RuntimeException) _e : new RuntimeException(_e); } }
};
_t_0.start();
}

static DialogHandler makeAndroid3_makeDialogHandler(final Android3 a) {
  return new DialogHandler() {
public void run(final DialogIO io) {

    if (!a.publicOverride && !(publicCommOn() || io.isLocalConnection())) {
      io.sendLine("Sorry, not allowed");
      return;
    }
    
    String dialogID = randomID(8);
    
    io.sendLine(a.greeting + " / Your ID: " + dialogID);
    
    List<String> history = new ArrayList<String>();
    
    while (io.isStillConnected()) {
      if (io.waitForLine()) {
        final String line = io.readLineNoBlock();
        String s = dialogID + " at " + now() + ": " + quote(line);
        if (!a.incomingSilent)
          print(s);
        if ("bye".equals(line)) {
          io.sendLine("bye stranger");
          return;
        }
        Matches m = new Matches();
        history.add(line);
        String answer;
        if (match3("this is a continuation of talk *", s, m)
          || match3("hello bot! this is a continuation of talk *", s, m)) {
          dialogID = unquote(m.m[0]);
          answer = "ok";
        } else
          answer = makeAndroid3_getAnswer(line, history, a);
        history.add(answer);
        io.sendLine(answer);
        //appendToLog(logFile, s);
      }
    }
  
}};
}

static String makeAndroid3_getAnswer(String line, List<String> history, Android3 a) {
  String answer;
  try {
    answer = makeAndroid3_fallback(line, history, a.responder.answer(line, history));
  } catch (Throwable e) {
    e = getInnerException(e);
    e.printStackTrace();
    answer = e.toString();
  }
  if (!a.incomingSilent)
    print("> " + shorten(answer, 500));
  return answer;
}

static String makeAndroid3_fallback(String s, List<String> history, String answer) {
  // Now we only do the safe thing instead of VM inspection - give out our process ID
  if (answer == null && match3("what is your pid", s))
    return getPID();
    
  if (answer == null && match3("what is your program id", s)) // should be fairly safe, right?
    return getProgramID();
    
  if (match3("get injection id", s))
    return getInjectionID();
    
  if (answer == null) answer = "?";
  if (answer.indexOf('\n') >= 0 || answer.indexOf('\r') >= 0)
    answer = quote(answer);
  return answer;
}

static boolean makeAndroid3_consoleInUse() {
  for (Object o : record_list)
    if (o instanceof Android3 && ((Android3) o).console)
      return true;
  return false;
}


  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) ? s : quote(s);
    }
    if (arg instanceof Integer || arg instanceof Long) return String.valueOf(arg);
    return quote(structure(arg));
  }
  



    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
  }
 // class Matches
  
  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;
    List<String> tokpat = parse3(pat), toks = parse3(s);
    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;
    }
  }

  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);
  }


static boolean isIdentifier(String s) {
  return isJavaIdentifier(s);
}

static String callStaticAnswerMethod(List<Class> bots, String s) {
  for (Class c : bots) try {
    String answer = callStaticAnswerMethod(c, s);
    if (answer != null) return answer;
  } catch (Throwable e) {
    print("Error calling " + getProgramID(c));
    e.printStackTrace();
  }
  return null;
}

static String callStaticAnswerMethod(Class c, String s) {
  String answer = (String) callOpt(c, "answer", s, litlist(s));
  if (answer == null)
    answer = (String) callOpt(c, "answer", s);
  return answer;
}

static String callStaticAnswerMethod(String s, List<String> history) {
  String answer = (String) callOpt(getMainClass(), "answer", s, history);
  if (answer == null)
    answer = (String) callOpt(getMainClass(), "answer", s);
  return answer;
}

static String getInjectionID() {
  return (String) call(getJavaX(), "getInjectionID", getMainClass());
}

static Object addToMultiPort_responder;

static long addToMultiPort(final String botName) {
  return addToMultiPort(botName, new Object() {
    public String answer(String s, List<String> history) {
      String answer = (String) ( callOpt(getMainClass(), "answer", s, history));
      if (answer != null) return answer;
      answer = (String) callOpt(getMainClass(), "answer", s);
      if (answer != null) return answer;
      if (match3("get injection id", s))
        return getInjectionID();
      return null;
    }
  });
}

static long addToMultiPort(final String botName, final Object responder) {
  print(botName);
  addToMultiPort_responder = responder;
  startMultiPort();
  List ports = getMultiPorts();
  if (ports.isEmpty())
    fail("No multiports!");
  if (ports.size() > 1)
    print("Multiple multi-ports. Using last one.");
  Object port = last(ports);
  Object responder2 = new Object() {
    public String answer(String s, List<String> history) {
      if (match3("get injection id", s))
        return getInjectionID();
      if (match3("your name", s))
        return botName;
      return (String) call(responder, "answer", s, history);
    }
  };
  record(responder2);
  return (Long) call(port, "addResponder", botName, responder2);
}


  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 Throwable getInnerException(Throwable e) {
  while (e.getCause() != null)
    e = e.getCause();
  return e;
}

static boolean publicCommOn() {
  return "1".equals(loadTextFile(new File(userHome(), ".javax/public-communication")));
}

static List<Object> record_list = synchroList();

static void record(Object o) {
  record_list.add(o);
}

// try to get our current process ID
static String getPID() {
  String name = ManagementFactory.getRuntimeMXBean().getName();
  return name.replaceAll("@.*", "");
}


static String programID;

static String getProgramID() {
  return programID;
}

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

  static AtomicInteger dialogServer_clients = new AtomicInteger();
  static boolean dialogServer_printConnects;
  
  static Set<String> dialogServer_knownClients = synchroTreeSet();
  
  static int startDialogServerOnPortAbove(int port, DialogHandler handler) {
    while (!startDialogServerIfPortAvailable(port, handler))
      ++port;
    return port;
  }
  
  static int startDialogServerOnPortAboveDaemon(int port, DialogHandler handler) {
    while (!startDialogServerIfPortAvailable(port, handler, true))
      ++port;
    return port;
  }
  
  static void startDialogServer(int port, DialogHandler handler) {
    if (!startDialogServerIfPortAvailable(port, handler))
      fail("Can't start dialog server on port " + port);
  }
  
  static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler) {
    return startDialogServerIfPortAvailable(port, handler, false);
  }
  
  static ServerSocket startDialogServer_serverSocket;
    
  static boolean startDialogServerIfPortAvailable(int port, final DialogHandler handler, boolean daemon) {
    ServerSocket serverSocket = null;
    try {
      serverSocket = new ServerSocket(port);
    } catch (IOException e) {
      // probably the port number is used - let's assume there already is a chat server.
      return false;
    }
    final ServerSocket _serverSocket = serverSocket;
    startDialogServer_serverSocket = serverSocket;

    Thread thread = new Thread("Socket accept port " + port) { public void run() {
     try {
      while (true) {
        try {
          final Socket s = _serverSocket.accept();
          
          String client = s.getInetAddress().toString();
          if (!dialogServer_knownClients.contains(client)) {
            print("connect from " + client + " - clients: " + dialogServer_clients.incrementAndGet());
            dialogServer_knownClients.add(client);
          }
          
          String threadName = "Handling client " + s.getInetAddress();

          Thread t2 = new Thread(threadName) {
           public void run() {
            try {
              final Writer w = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
              final BufferedReader in = new BufferedReader(
                new InputStreamReader(s.getInputStream(), "UTF-8"));
              DialogIO io = new DialogIO() {
                String line;
                boolean buff;
                
                Socket getSocket() { return s; }
                
                void close() {
                  try {
                    s.close();
                  } catch (IOException e) {
                    // whatever
                  }
                }
                
                // local means localhost - todo: test
                boolean isLocalConnection() {
                  return s.getInetAddress().isLoopbackAddress();
                }
                
                boolean isStillConnected() {
                  return !(buff || s.isClosed());
                }
                
                String readLineNoBlock() {
                  String l = line;
                  line = null;
                  return l;
                }
                
                boolean waitForLine() { try {
 
                  if (line != null) return true;
                  //print("Readline");
                  line = in.readLine();
                  //print("Readline done: " + line);
                  if (line == null) buff = true;
                  return line != null;
                
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
                
                void sendLine(String line) { try {
 
                  w.write(line + "\n");
                  w.flush();
                
} catch (Throwable __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}
              };
              
              try {
                handler.run(io);
              } finally {
                s.close();
              }
            } catch (IOException e) {
              print("[internal] " + e);
            } finally {
              //print("client disconnect - " + dialogServer_clients.decrementAndGet() + " remaining");
            }
           }
          }; // Thread t2
          t2.setDaemon(true); // ?
          t2.start();
        } catch (SocketTimeoutException e) {
        }
      }   
     } catch (IOException e) {
       print("[internal] " + e);
     }
    }};
    if (daemon) thread.setDaemon(true);
    thread.start();
   
    print("Dialog server on port " + port + " started."); 
    return true;
  }

  static List<String> parse3(String s) {
    return dropPunctuation(javaTokPlusPeriod(s));
  }


static BufferedReader readLine_reader;

static String readLine() {
  return (String) call(getJavaX(), "readLine");
}

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<Integer>();
  for (int i : a) l.add(i);
  return l;
}

static <A> ArrayList<A> asList(Set<A> s) {
  return new ArrayList<A>(s);
}

static String randomID(int length) {
  return makeRandomID(length);
}

// 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 ("*".equals(p))
      result.add(t);
    else if (!p.equalsIgnoreCase(t))
      return null;
  }
  return result.toArray(new String[result.size()]);
}


static long now_virtualTime;
static long now() {
  return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}


// This is made for NL parsing.
// It's javaTok extended with "..." token, "$n" and "#n"

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 == '\'' || c == '"') {
      char opener = c;
      ++j;
      while (j < l) {
        if (s.charAt(j) == opener) {
          ++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)) || 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.isDigit(s.charAt(j)));
    else
      ++j;

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


static StringBuffer print_log;

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

static void print(Object o) {
  String s = String.valueOf(o) + "\n";
  synchronized(StringBuffer.class) {
    if (print_log == null) print_log = new StringBuffer();
  }
  print_log.append(s);
  System.out.print(s);
}

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

  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.length()-1);
      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 structure(Object o) {
  return structure(o, 0);
}

// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;
  
static String structure(Object o, int stringSizeLimit) {
  if (o == null) return "null";
  String name = o.getClass().getName();
  
  StringBuilder buf = new StringBuilder();
  
  if (o instanceof Collection) { // TODO: store the type (e.g. HashSet/TreeSet)
    for (Object x : (Collection) o) {
      if (buf.length() != 0) buf.append(", ");
      buf.append(structure(x, stringSizeLimit));
    }
    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));
      buf.append("=");
      buf.append(structure(((Map.Entry) e).getValue(), stringSizeLimit));
    }
    return "{" + 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));
    }
    return "array{" + buf + "}";
  }

  if (o instanceof String)
    return quote(stringSizeLimit != 0 ? shorten((String) o, stringSizeLimit) : (String) o);
    
  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\\$", "");
  
  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));
      }
      ++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;
      Object value;
      try {
        field.setAccessible(true);
        value = field.get(o);
      } catch (Exception e) {
        value = "?";
      }
      
      fieldName = field.getName();
      
      // put special cases here...
  
      if (value != null) {
        if (buf.length() != 0) buf.append(", ");
        buf.append(fieldName + "=" + structure(value, stringSizeLimit));
      }
      ++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;
}

static String shorten(String s, int max) {
  return s.length() <= max ? s : s.substring(0, Math.min(s.length(), max)) + "...";
}

static Class run(String progID) {
  Class main = hotwire(progID);
  callMain(main);
  return main;
}

static String or(String a, String b) {
  return a != null ? a : b;
}

// start multi-port if none exists in current VM.
static void startMultiPort() {
  // TODO: might start multiple if run concurrently...
  if (getMultiPorts().isEmpty())
    callMain(hotwire("#1001672"));
}

static List<Object> getMultiPorts() {
  return (List) call(getJavaX(), "getMultiPorts");
}

  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 <A> Set<A> synchroTreeSet() {
  return Collections.synchronizedSet(new TreeSet<A>());
}


static Class getMainClass() { try {
 
  return Class.forName("main");

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

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 Object getOpt(Object o, String field) {
  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);
    
  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) {
  for (Field f : c.getDeclaredFields())
    if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
      return f;
  return null;
}

static Field getOpt_findField(Class<?> c, String field) {
  for (Field f : c.getDeclaredFields())
    if (f.getName().equals(field))
      return f;
  return null;
}

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

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


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

static <A> A last(List<A> l) {
  return l.isEmpty() ? null : l.get(l.size()-1);
}

static Class __javax;

static Class getJavaX() {
  return __javax;
}

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)) && !t.equals("*")) {
      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)));
}

  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 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 RuntimeException fail() {
    throw new RuntimeException("fail");
  }
  
  static RuntimeException fail(Object msg) {
    throw new RuntimeException(String.valueOf(msg));
  }

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 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 Object get(Object o, String field) {
  if (o instanceof Class) return get((Class) o, 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) {
  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 Field get_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());
}

// 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;
  return type.isInstance(arg);
}

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

static void callMain(Object c, String... args) {
  callOpt(c, "main", new Object[] {args});
}

  // compile JavaX source, load classes & return main class
  // src can be a snippet ID or actual source code
  // TODO: record injection?
  
  static Class<?> hotwire(String src) {
    try {
      Class j = getJavaX();
      
      synchronized(j) { // hopefully this goes well...
        List<File> libraries = new ArrayList<File>();
        File srcDir = (File) call(j, "transpileMain", src, libraries);
        if (srcDir == null)
          fail("transpileMain returned null (src=" + quote(src) + ")");
        
        Object androidContext = get(j, "androidContext");
        if (androidContext != null)
          return (Class) call(j, "loadx2android", srcDir, src);
          
        File classesDir = (File) call(j, "TempDirMaker_make");
        String javacOutput = (String) call(j, "compileJava", srcDir, libraries, classesDir);
        System.out.println(javacOutput);
        
        URL[] urls = new URL[libraries.size()+1];
        urls[0] = classesDir.toURI().toURL();
        for (int i = 0; i < libraries.size(); i++)
          urls[i+1] = libraries.get(i).toURI().toURL();
  
        // make class loader
        URLClassLoader classLoader = new URLClassLoader(urls);
    
        // load & return main class
        Class<?> theClass = classLoader.loadClass("main");
        
        call(j, "setVars", theClass, isSnippetID(src) ? src: null);
        
        if (isSnippetID(src))
          callOpt(j, "addInstance", src, theClass);
  
        return theClass;
      }
    } catch (Exception e) {
      throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
    }
  }

  public static boolean isSnippetID(String s) {
    try {
      parseSnippetID(s);
      return true;
    } catch (RuntimeException e) {
      return false;
    }
  }

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 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 snippetID;
}

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

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