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 {
// ignores case
static class StartsWith extends Prolog.BaseNative {
StartsWith() { super("$x starts with $y"); }
boolean yo() {
String x = unq("x"), y = unq("y");
return swic(x, y);
}
}
// ignores case
static class EndsWith extends Prolog.BaseNative {
EndsWith() { super("$x ends with $y"); }
boolean yo() {
String x = unq("x"), y = unq("y");
return endsWithIgnoreCase(x, y);
}
}
// Prolog Native Stuff
static String trueStuff = "\n [\"oops\" starts with \"oo\"]\n";
static String falseStuff = "\n [\"poopoo\" starts with \"oopoop\"]\n";
public static void main(String[] args) throws Exception {
Prolog p = new Prolog();
p.addNatives(new StartsWith(), new EndsWith());
prolog_checkTrueStuff(p, trueStuff);
prolog_checkFalseStuff(p, falseStuff);
print("ok!");
}
static void prolog_checkTrueStuff(Prolog p, String data) {
for (Lisp s : p.parseStatements(data)) {
print("Checking TRUE: " + nlUnparse(s));
assertTrue(p.canSolve(s));
}
}
static void prolog_checkFalseStuff(Prolog p, String data) {
for (Lisp s : p.parseStatements(data)) {
print("Checking FALSE: " + nlUnparse(s));
assertFalse(p.canSolve(s));
}
}
static boolean endsWithIgnoreCase(String a, String b) {
return a != null && l(a) >= l(b) && a.regionMatches(true, l(a)-l(b), b, 0, l(b));
}
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 int print_maxLineLength = 0; // 0 = unset
static boolean print_silent; // total mute if set
static void print() {
print("");
}
// slightly overblown signature to return original object...
static A print(A o) {
if (print_silent) return o;
String s = String.valueOf(o) + "\n";
// TODO if (print_maxLineLength != 0)
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);
return o;
}
static void print(long l) {
print(String.valueOf(l));
}
static void print(char c) {
print(String.valueOf(c));
}
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 void assertTrue(Object o) {
assertEquals(true, o);
}
static boolean assertTrue(String msg, boolean b) {
if (!b)
throw fail(msg);
return b;
}
static boolean assertTrue(boolean b) {
if (!b)
throw fail("oops");
return b;
}
static void assertFalse(Object o) {
assertEquals(false, o);
}
static boolean assertFalse(boolean b) {
if (b)
throw fail("oops");
return b;
}
static boolean swic(String a, String b) {
return startsWithIgnoreCase(a, b);
}
static String nlUnparse(Lisp tree) {
return nlUnparse(tree, true);
}
static String nlUnparse(Lisp tree, boolean dropOuterBrackets) {
StringBuilder buf = new StringBuilder();
nlUnparse(tree, buf, dropOuterBrackets);
return str(buf);
}
static void nlUnparse(Lisp tree, StringBuilder buf, boolean dropOuterBrackets) {
if (tree == null) return;
// special classes - Var etc.
if (tree.getClass() != Lisp.class) {
buf.append(tree);
return;
}
if (tree.isA("[]")) {
if (!dropOuterBrackets) buf.append("[");
} else if (tree.isA("()"))
buf.append("(");
else {
if (tree.isLeaf())
buf.append(tree.head);
else
buf.append("?? " + tree);
return;
}
for (int i = 0; i < tree.size(); i++) {
if (i != 0) buf.append(" ");
nlUnparse(tree.get(i), buf, false);
}
if(tree.isA("[]")) {
if (!dropOuterBrackets) buf.append("]");
} else
buf.append(")");
}
static String str(Object o) {
return String.valueOf(o);
}
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));
}
// disabled for now to shorten some programs
/*static RuntimeException fail(S msg, O... args) {
throw new RuntimeException(format(msg, args));
}*/
static A assertEquals(Object x, A y) {
return assertEquals(null, x, y);
}
static A assertEquals(String msg, Object x, A y) {
if (!(x == null ? y == null : x.equals(y)))
throw fail((msg != null ? msg + ": " : "") + structure(x) + " != " + structure(y));
return y;
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.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(CharSequence s) { return s == null ? 0 : s.length(); }
static int l(Object o) {
return l((List) o); // incomplete
}
static boolean startsWithIgnoreCase(String a, String b) {
return a != null && a.regionMatches(true, 0, b, 0, b.length());
}
static String unnull(String s) {
return s == null ? "" : s;
}
static List unnull(List l) {
return l == null ? emptyList() : l;
}
static Iterable unnull(Iterable i) {
return i == null ? emptyList() : i;
}
static Object[] unnull(Object[] a) {
return a == null ? new Object[0] : a;
}
static BitSet unnull(BitSet b) {
return b == null ? new BitSet() : b;
}
static String structure(Object o) {
HashSet refd = new HashSet();
return structure_2(structure_1(o, new structure_Data(refd)), refd);
}
// leave to false, unless unstructure() breaks
static boolean structure_allowShortening = false;
static int structure_shareStringsLongerThan = 20;
static class structure_Data {
int stringSizeLimit;
IdentityHashMap