Monday, June 14, 2010

One thing I like to do is play Rock Band. However, I often find that I don't remember what songs I just played in a playlist, and sometimes I want to check and see if I got a new high score. My first thought was to sniff the packets sent to the online leaderboards and pull the songs and the scores out of that data. Unfortunately, the packets are encrypted, so I gave up on that idea. But I had a new idea. I could point my computer's camera at the screen and record a video.

Making a video of the gameplay would eat up disk space and it would be a pain to go through a long video to see all the songs. But, going back to my first idea of sniffing packets, I could have some scheme of taking pictures and saving the ones around the time when packets were sent to the leaderboards. After finding out how to take pictures using the quicktime API and trying it out, I wrote some code that took a picture every 5 seconds, and interleaved the pictures with tcpdump output. I found that 32 byte packets were being sent every few seconds to Xbox Live, and 80 byte packets were being sent every once in a while, but larger packets were always sent after finishing a song. So then I wrote it to take a few pictures when seeing the larger packets. It worked really well. It takes some extraneous pictures due to other packets. Fortunately, I don't have lots of Xbox friends that would cause lots of notification packets.

Now I can play a dozen or so songs, and then go back and see how I did, instead of worrying about not remembering what I just played after playing only 3 or 4 songs.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Date;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import quicktime.QTSession;
import quicktime.io.QTFile;
import quicktime.qd.Pict;
import quicktime.qd.QDConstants;
import quicktime.qd.QDGraphics;
import quicktime.qd.QDRect;
import quicktime.std.StdQTConstants4;
import quicktime.std.image.GraphicsExporter;
import quicktime.std.sg.SequenceGrabber;

public class Scorer {
private static ArrayBlockingQueue<Long> queue = new ArrayBlockingQueue<Long>(20);
private static boolean running = true;

private static void capturePhoto(String filename) throws Exception {
try {
QTSession.open();
QDRect qdRect = new QDRect(640, 480);
QDGraphics qdGraphics = new QDGraphics(qdRect);
SequenceGrabber sequenceGrabber = new SequenceGrabber();
sequenceGrabber.setGWorld(qdGraphics, null);
GraphicsExporter graphicsExporter = new GraphicsExporter(StdQTConstants4.kQTFileTypePNG);
graphicsExporter.setInputPicture(Pict.fromSequenceGrabber(sequenceGrabber, qdRect, 0, 0));
graphicsExporter.setOutputFile(new QTFile(filename));
graphicsExporter.doExport();
} finally {
QTSession.close();
}
}

private static void recorder(String dir) {
try {
PrintStream out = new PrintStream(new FileOutputStream(dir + "index.html"));
out.println("<html><body>");
int i = 0;
long t = 0L;
while (running) {
queue.take();
if (System.currentTimeMillis() - t < 30000L)
continue;
out.println("<br>Start:" + new Date() + "<br>");
for (int j = 0; j < 3; j++) {
capturePhoto(dir + i + ".png");
out.println("<img src=\"" + i + ".png\">");
i++;
TimeUnit.SECONDS.sleep(1);
}
out.println("<br>End:" + new Date() + "<br>");
queue.clear();
t = System.currentTimeMillis();
}
out.println("</body></html>");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}

private static void sniffer() {
try {
Process process = new ProcessBuilder("/usr/bin/sudo", "/usr/sbin/tcpdump", "-n", "-l", "port", "3074").start();
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
while (running) {
String s = in.readLine();
if (s == null)
break;
if (s.endsWith("length 32") || s.endsWith("length 80"))
continue;
queue.offer(System.currentTimeMillis());
}
process.destroy();
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) throws Exception {
new File("/tmp/score").mkdir();
new Thread() { public void run() { recorder("/tmp/score/"); } }.start();
new Thread() { public void run() { sniffer(); } }.start();
System.in.read();
running = false;
}
}

Monday, June 7, 2010

I played around a little with Clojure's Java interoperability at work. Since I made making a new module in the main product that I work on a matter of putting some classes and some xml in a jar file and putting that jar file and any jars it depends on in a directory, it shouldn't be a problem.

The Clojure code was all straightforward, using gen-class. How to actually get the class files generated was all in the documentation, but having the correct classpath was a big snag for me, especially the need to have ./classes in the classpath, which kept causing mysterious NoClassDefFoundErrors. After getting that figured out, I stuck it into ant:

<target name="compile">
<mkdir dir="${build.dir}"/>
<java classname="clojure.main">
<arg value="-e"/>
<arg value="(set! *compile-path* &#34;${build.dir}&#34;) (compile 'name.of.test.ModuleClass)"/>
<classpath>
<fileset dir="${lib.dir}">
<include name="*.jar"/>
</fileset>
<fileset dir="${clojure.home}">
<include name="*.jar"/>
</fileset>
<pathelement location="${src.dir}"/>
<pathelement location="${build.dir}"/>
</classpath>
</java>
</target>

Then, after getting the jar files built, I dropped the jar file and clojure.jar into the directory and got a giant stack trace, leading to

Caused by: java.io.FileNotFoundException: Could not locate clojure/core__init.class or clojure/core.clj on classpath:
at clojure.lang.RT.load(RT.java:402)
at clojure.lang.RT.load(RT.java:371)
at clojure.lang.RT.doInit(RT.java:406)
at clojure.lang.RT.(RT.java:292)
... 45 more

I hadn't the classloader set properly for the initial loading of the modules. For all the other modules, there wasn't much done in the class initializers, so this problem didn't come up. I had the classloader set for all other operations, though, so it was merely a matter of doing the same thing at initialization, and it worked.

I also noted that I handled the classloader by hand-creating a proxy class and implementing each method to wrap the classloader switch,

final ClassLoader handlerClassLoader = handler.getClass().getClassLoader();
return new Handler() {
public Object getObject(Object parameter) {
ClassLoader cl = Thread.currentThread().getContentClassLoader();
Thread.currentThread().setContextClassLoader(handlerClassLoader);
try {
return handler.getObject(parameter);
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
}
};

which I could improve by using java.lang.reflect.Proxy, which I could use to wrap all the calls with one method. The code was about 100 lines shorter, and would no longer have to be changed if the Handler interface changed. I'll check in these changes, but not the test module in Clojure.

Monday, May 31, 2010

After playing with Clojure for a little while, one feature that I really like about it is doc and doc-strings, which work just like doc-strings in Emacs Lisp. I can use find-doc and ns-publics to find or explore the available symbols. I'm sure that an emacs mode or an IDE could make the interface as nice as the Emacs Lisp interface, but the functionality is there.

In Java, I often use javap to see what the method signatures of a class were, and have sometimes wished that the Javadoc were also available in the same way. The Java compiler could have put the Javadoc in attributes in the class files, which would then be available to tools like javap.

When using hugs or ghci for Haskell, I like using :browse and :info. The name and type almost always is enough to convey what a function does. It would be nice to have a short description of relevant information not captured by the name and type, but that's somewhat rare.

In any case, if I really needed to, documentation for the standard libraries for all three of these languages are available online. For Clojure and Haskell, the source code is available. For Java, the class files can be disassembled, though the occasional native method would remain opaque.

I find Clojure compelling in a way that I do not find Python compelling. They are both dynamically typed, which is a minus to me for both. I'm neither drawn to nor repelled by the syntax of either. However, I like feel of the immutable data structures of Clojure better than the data structures of Python. I'm also going to play around with integrating Clojure code with Java code. And while I haven't explored multimethods or any concurrency features, I can see learning new concepts, perhaps even learning when volatile would be useful when writing in Java, when looking into them.

Monday, May 24, 2010

In the product I work on at work, there are modules that accept items of various content types. Somebody made a dumb extension that made text content a special case, so now I'm getting silly NullPointerException bugs assigned to me because null is being passed in for the content. It would have been much cleaner to just use "text/plain" as the content type without any new special case code.

Monday, May 17, 2010

It seemed inappropriate that I hadn't implemented Parenthesis Hell in a Lisp, so I decided to learn a little about Clojure, a new Lisp dialect. I didn't use any of the concurrency features of Clojure, which is one of its main selling points. However, a Parenthesis Hell interpreter doesn't need any mutable state.

(defn ph-eval* [expr scope input]
(loop [scope* scope]
(cond
(empty? expr) input
(contains? scope* (first expr))
((scope* (first expr)) (rest expr) scope input scope*)
:else (recur (scope* 'outer)))))

(defn ph-valid? [val]
(cond
(not (seq? val)) false
(empty? val) true
(not (seq? (first val))) false
(empty? (first val)) (recur (rest val))
:else (recur (cons (first (first val)) (cons (rest (first val)) (rest val))))))

(defn ph-fn [body]
(fn [expr scope input def-scope]
(ph-eval* body def-scope expr)))

(defn ph-quote [expr scope input def-scope]
expr)

(defn ph-let [expr scope input def-scope]
(if (empty? expr)
()
(loop [bindings (first expr) scope* {'outer scope}]
(cond
(empty? bindings) (ph-eval* (rest expr) scope* input)
(empty? (first bindings)) (recur (rest bindings) scope*)
:else (recur (rest bindings)
(assoc scope*
(first (first bindings))
(ph-fn (rest (first bindings)))))))))

(defn ph-car [expr scope input def-scope]
(let [expr* (ph-eval* expr scope input)]
(if (empty? expr*) () (first expr*))))

(defn ph-cdr [expr scope input def-scope]
(let [expr* (ph-eval* expr scope input)]
(if (empty? expr*) () (rest expr*))))

(defn ph-cons [expr scope input def-scope]
(if (empty? expr)
()
(let [head (ph-eval* (first expr) scope input)
tail (ph-eval* (rest expr) scope input)]
(cons head tail))))

(defn ph-if [expr scope input def-scope]
(cond
(or (empty? expr) (empty? (rest expr))) ()
(not (empty? (ph-eval* (first expr) scope input)))
(ph-eval* (first (rest expr)) scope input)
:else
(ph-eval* (rest (rest expr)) scope input)))

(defn ph-eval [expr scope input def-scope]
(let [expr* (ph-eval* expr scope input)]
(if (not (ph-valid? expr))
(throw (IllegalArgumentException. "Not valid Parenthesis Hell")))
(ph-eval* expr* scope input)))

(defn ph-eval
"Evaluate a Parenthesis Hell expression with optional input."
([expr] (ph-eval expr ()))
([expr input]
(if (or (not (ph-valid? expr))
(not (seq? input)))
(throw (IllegalArgumentException. "Not valid Parenthesis Hell")))
(ph-eval* expr
{'() ph-quote
'(()) ph-let
'((())) ph-car
'(()()) ph-cdr
'((())()) ph-cons
'(()()()) ph-if
'(((()))) ph-eval}
input)))

Monday, May 10, 2010

Here is are a couple of programs in Parenthesis Hell that print themselves. I've inserted a bunch of line breaks, but they should be on one line.

The first one uses the optional concat in the initial scope. The second does not, and I haven't successfully tested it, since my interpreter runs out of memory when running it.

The following uses concat:

((())((((()()))()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()(
)(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()(
)()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(
()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()
()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()((
)(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(
()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()((
)(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()
()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()(()((
)()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()(
)(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()
()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()
(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()
()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()((
)(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(
()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()((
)(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(
()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()(()((
)()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()(
)(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()(
)(()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()((
)(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(
()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()
(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()((
)()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(
()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()
()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(
()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()
()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()((
)()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()(
)(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()
()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()(
)(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()
()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()
(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()(
)()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()
(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()(
)()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(
()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()
()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(
()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(
)()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()((
)(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(
()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()
(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()
()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(
()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()((
)()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(
()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()
()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()((
)()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()
()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()((
)()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()(
)(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()
()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()(
)(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()
()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()(
)(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()
()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()(
)(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()
()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()
(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()(
)()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(
()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()(
)(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(
()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()
()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()
(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()((
)()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()((
)(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()(
)(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(
()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()
()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()
(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()
()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(
()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(
()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()
(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
))))))))))))))))((())(()()())()((()(()))(()()()(()(()()()())))(()(()))((())
((())))(()(()))(()()()(()(()()(()))))(())(()()))()()))(()(()))(()()()(()(()
()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()(
)(()(()()()()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()((
)()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()(()))
))))))))))))))))))))))))))))))))))))))))(()(()))((())((()())))((()())))

The following does not use concat:

((())((((()()))()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()(
)(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()(
)()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(
()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()
(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()((
)(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()
()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()
(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()((
)()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(
()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()(
)(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()
()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()(
)(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()
()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()
(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(
)()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(()
(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()(
)()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()((
)()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()
()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()((
)()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()
(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()(
)(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()((
)(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()(
)()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()(()()(()
(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()
()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()
(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()
()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()(
)()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(
()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()
(()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()((
)(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()((
)()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(
()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()
()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()((
)()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()(()(
)(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()
()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()
(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()(
)()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()((
)(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()
()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()
(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()()
()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()((
)()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()(
)(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()(
)()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()
(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()
()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(
()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()
(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()
(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()()
()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()(()((
)()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()
(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()
(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()((
)(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()(
()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()
(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()((
)()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(
()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()
()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(
()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()
()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()((
)()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()(
)(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()
()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()(
)(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()
()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()
(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()(
)()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()
(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()(
)()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(
()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()
()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(
()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(
)()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()((
)(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(
()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()
(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()
()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(
()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()((
)()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(
()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()
()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()((
)()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()
()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()((
)()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()(
)(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()
()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()(
)(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()
()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()(
)(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()
()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()(
)(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()
()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()
(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()(
)()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(
()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()(
)(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(
()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()
()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(
()(()()(()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()()(
)()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()((
)(()()()()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(
()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()
()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()
(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()
(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()((
)()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()(
)(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(
()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(
()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()
()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()((
)()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()(()(
)(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()()()()(()((
)()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()
(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(()(()()(()()(()(()(
)(()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()()()()(()(()()()()()(
()(()()(()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()(()()(()(()()((
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
)))))))))))))))))))))))))))))))((())(()()())(((())))((()()())(((()))((())))
(((())())((())((())())(((()))((())))(()()))())(()()())((()())((())))(((())(
))(())(())((())())((()())((())))(()()))(()()))(()()))((((())))(()()())()(((
))((())())(()()()(()(()()()())))(())((())())((((())))((())))(())((())())(()
()()(()(()()(()))))(((())))(()()))()()))(())((())())(()()()(()(()()()()()((
)(()()()()()(()(()()()()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()(
)()()(()(()()()()()(()(()()()()()(()(()()()()()(()(()()(()()(()(()()()()()(
()(()()(()()(()(()()(()()(()(()()(()()(()(()()()()()(()(()()(()))))))))))))
))))))))))))))))))))))))))))))(())((())())((((())))((()())))((()())))

Monday, May 3, 2010

The tasq programming language

tasq is a macro-expansion language with a single task queue and a small set of operations.
Syntax

A tasq program is a sequence of declarations or comments, optionally separated by whitespace.

A comment starts with a dot (.) and continues to the end of the line.

A declaration is an identifier followed by zero or more operations, and ending with a dot (.). Whitespace may separate identifiers and operations, but is otherwise ignored.

An operation is an identifier or one of +, -, ~, or ?.

An identifier is an uninterrupted sequence of non-whitespace characters not including +, -, ~, and ?.

A declaration of an identifier with zero following operations adds the identifier to the end of the task queue.

A declaration of an identifier followed by one or more operations defines the identifier's expansion as those operations.

It is illegal to use undefined identifiers. It is illegal to have more than one definition for an identifier.
Execution

Execution consists of dequeuing and executing the top item of the task queue repeatedly while the task queue is not empty.
Operations

+ Write 1 to the output.
- Write 0 to the output.
~ Discard the next item in the task queue.
? Read one bit from the input. If 1 is read, do nothing. If 0 is read, discard the next item in the task queue. If at EOF, discard the next two items in the task queue.
identifier Append the identifier's expansion to the end of the task queue.
Examples

Hello world:

w-+--+----++--+-+-++-++---++-++---++-++++--+------+++-
+++-++-++++-+++--+--++-++---++--+----+----+----+-+-.w.

cat:

bit? 1 0. .Read a bit
0 -bit. .Write 0, handle next bit
1 +~. .Write 1, discard the ensuing -
bit. .Initial task queue

Self-printing program (formatted as multiple lines, but should be a 1-liner):

d.0-z.1+o.z--+-------++----.o--+-------++---+.q p.p--+------+++---+--+-+++--
---+-+-.d 0 1 1 0 0 1 0 0 0 0 1 0 1 1 1 0 0 0 1 1 0 0 0 0 0 0 1 0 1 1 0 1 0
1 1 1 1 0 1 0 0 0 1 0 1 1 1 0 0 0 1 1 0 0 0 1 0 0 1 0 1 0 1 1 0 1 1 0 1 1 1
1 0 0 1 0 1 1 1 0 0 1 1 1 1 0 1 0 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1
0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1
0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0 0 1 0 1 0 1 1 0
0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 1
0 0 1 1 0 1 1 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0 0 1 0 1
1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1
0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0 0 1 0 1 0 1 1 0 0 1 0 1 1 0 1 0
0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0 0 1 0 1 1 1 0 0 1 1 1 0 0 0
1 0 0 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 0 0 0 0 0 0 1 0 1
1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1
0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0
0 1 0 1 0 1 1 0 0 1 0 1 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0
1 0 0 1 0 1 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0 0 1 0 1
1 0 1 0 0 1 0 1 0 1 1 0 0 1 0 1 0 1 1 0 0 1 0 1 0 1 1 0 0 1 0 1 1 0 1 0 0 1
0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0
0 1 0 1 1 0 1 0 0 1 0 1 0 1 1 0 0 1 0 1 1 0 1 0 0 1 0 1 1 1 0 0 1 1 0 0 1 0
0 q.

Interpreter


module Main(main) where

import Data.Map(Map,empty,insert,(!))
import Data.Bits(testBit)
import Data.Char(chr,ord)
import System.Environment(getArgs)
import Text.ParserCombinators.Parsec(CharParser,anyChar,char,getState,many,many1,manyTill,newline,noneOf,runParser,setState,space,spaces,(<|>),(<?>))

data Task = Out0 | Out1 | In | Discard | Task String

parse :: String -> String -> (String -> [Task],[Task])
parse file src =
either (error . show) id (runParser program (empty,[]) file src)

type Parser a = CharParser (Map String [Task],[Task]) a

program :: Parser (String -> [Task],[Task])
program = do
many (space <|> comment)
many statement
(map,queue) <- getState
return ((map !),reverse queue)

comment :: Parser Char
comment = do
char '.'
manyTill anyChar newline
return ' '

statement :: Parser ()
statement = do
id <- name
body <- many task
char '.'
many (space <|> comment)
(map,queue) <- getState
setState (if null body then (map,Task id:queue)
else (insert id body map,queue))

task :: Parser Task
task = do
t <- (char '-' >> return Out0)
<|> (char '+' >> return Out1)
<|> (char '?' >> return In)
<|> (char '~' >> return Discard)
<|> fmap Task name
spaces
return t

name :: Parser String
name = do
str <- many1 (noneOf "+-?~. \r\n\t\f\v")
spaces
return str

run :: (String -> [Task]) -> [Task] -> [Bool] -> [Bool]
run _ [] _ = []
run defs (Out0:tasks) input = False : run defs tasks input
run defs (Out1:tasks) input = True : run defs tasks input
run defs (In:[]) _ = []
run defs (In:_:[]) [] = []
run defs (In:_:_:tasks) [] = run defs tasks []
run defs (In:_:tasks) (False:input) = run defs tasks input
run defs (In:tasks) (True:input) = run defs tasks input
run defs (Discard:[]) _ = []
run defs (Discard:_:tasks) input = run defs tasks input
run defs (Task name:tasks) input = run defs (tasks ++ (defs name)) input

main :: IO ()
main = do
(file:_) <- getArgs
src <- readFile file
interact (fromBits . uncurry run (parse file src) . toBits)

toBits = concatMap (flip map [7,6..0] . testBit . ord)

fromBits (b7:b6:b5:b4:b3:b2:b1:b0:rest) =
chr ((if b7 then 128 else 0) + (if b6 then 64 else 0)
+ (if b5 then 32 else 0) + (if b4 then 16 else 0)
+ (if b3 then 8 else 0) + (if b2 then 4 else 0)
+ (if b1 then 2 else 0) + (if b0 then 1 else 0)) : fromBits rest
fromBits _ = []