3 ms·
In 2008 I was working as a contractor for somebody who needed many J2ME MIDlets. Some of those MIDLets had to get information from a server, in most cases the M
by magoghm 12y ago
In 2008 I was working as a contractor for somebody who needed many J2ME MIDlets. Some of those MIDLets had to get information from a server, in most cases the MIDlet did an HTTP GET and the server would respond with some simple data (usually a number or a string).
But one day I had to write a MIDlet that would need to receive a more complicated data structure. As I was the one person in charge of writing both the MIDLet and the server side servlet I could choose any format I wanted for the data.
I decided that XML was too verbose, and I decided to use lisp-like S-expressions to encode the data. Writing the parser, which took a string and converted it to a list of cons cells and atoms, took me about 2 hours. Then I decided that the best way to debug it was to write another routine to translate those cons cells and atoms back into a string, then to write a loop that read a string parsed it and then took the data structure, converted it back to a string and printed it. About half an hour later, I had a program that could read S-expressions and print them back (running it on my computer using the subset of Java that could run on a mobile phone).
Then I realized that I had a read-print-loop, and if I added an eval to that loop then I would have a lisp interpreter. Nine hours later, I had a simple Lisp interpreter that could run on J2ME devices!
I was so happy about my J2ME lisp interpreter that I decided to add support for lisp macros. That turned out to be harder, it took me three days to have a version with a working defmacro.
Then I added a way to open a bluetooth connection to my computer, where I had a small program that would send anything it read from standard input to the mobile phone and then print out on standard output the response it got from the phone. I now had an interactive REPL to execute Lisp code on my phone. With this I could write and debug MIDlets much faster than before.
All the MIDlets I wrote in the following weeks were written in Lisp.
I then decided that my Lisp interpreter was too slow for some applications and decided to write a compiler that turned Lisp code into bytecodes I could efficiently interpret in a J2ME lisp virtual machine. I took the scheme compiler described in Norvig's "Paradigms of Artificial Intelligence Programming" and translated it into my version of Lisp (which was very similar to scheme). Then I used my Lisp interpreter to compile the compiler itself into bytecodes. This part turned out to be much harder than I expected, it took one month of hard work to make the transition from interpreted Lisp to compiled Lisp.
The result was a very compact J2ME Lisp environment. A MIDLet with the Lisp virtual machine, Lisp compiler and basic runtime libraries was about 32 KB (and a full MIDlet with application specific code was usually less than 50 KB). It could connect to a server over bluetooth or TCP/IP to give me an interactive REPL, and it could also dynamically download new code to extend its functionality without requiring the user to install a new version of the MIDlet.
- phatak-dev 12y agoAwesome. Do you have link to code or some references?
- magoghm 12y agoUnfortunately, I do not own the rights to the code. If I did I would have released it as open source.