Today is a good day to code

Trials and Tribulations of a PHP Interpreter / Compiler Writer

Posted: June 7th, 2009 | Author: | Filed under: android, java, Programming | Tags: , , , , , , , , , | No Comments »

For the past couple of months, I have been struggling to write a PHP interpreter for an upcoming Android version of Mides.  The tokenizer was fairly easy, building the Abstract Syntax Tree was relatively uncomplicated, but now we come to actually writing the executor.  This is giving me pause.

I totally understand why Joel Spolsky says that there are two classes of programmers, one class that sort of hacks their way through life, and another that writes compilers for fun.  I guess I am trying to blunder my way into the latter class.  At first, it didn’t seem to be so difficult, but eventually it has gotten so hard that sometimes I feel like just dropping the whole thing entirely.  However, when I feel like bashing my head into the desk, and / or throwing my MacBook Pro out a window, I know I am on the right track.  That is how I felt when I was trying to learn Java and Objective-C a few years ago, or attempting to understand domain modeling as it pertained to Object Oriented Programming, before understanding Object Oriented Programming.

I guess these are just things that we all must get through, I also see why most CS majors don’t every actually finish their compilers.  I, however have a dream that I can develop web applications without being encumbered by any sort of connections from my handheld.

The first part that slowed me down was that I was trying to approach the executor by compiling the program to opcodes and then running them against the system, sort of like the asm function in C.  That won’t work with Dalvik, at least not in an elegant way.  Then I asked this question on StackOverflow, and the answer made me think;   I don’t think I quite got it immediately, but now I understand.  It basically comes down to a large hashmap of keys ( variable names ) and their values, ( the statements to evaluate into them ).  OK, I get that, but the new problem I am facing with the top-down recursive descent parser is how to handle classes and functions efficiently.

I realize that generally a class is just a new hashmap of values, scoped to a variable handle in the global hashmap, a function is similar in that it has its own scope that is not shared with other objects in the main hashmap.  The recursion here can make your head hurt.  The problem I am facing now is that if I have a statement ( series of tokens ) mapped to a variable, how do I handle, a ) parameters, and b ) not re-interpreting the tokens each time the class is instantiated.  Another issue is how to keep the scope of a single instance different than another instance, unless there is a shared static variable in the hashmap between them.  I am looking at V8’s source to try to understand the concepts behind their VM.  I think that is how they boosted speed so greatly for OO based JavaScript applications.  They must only parse those tokens once.  There are all kinds of bugs I can see introducing into my fledgling interpreter by doing that.

I guess what I am understanding is why PHP has taken more than a couple years or more to get where it is today, why Ruby still has quite a few oddities to the language, and why Python basically needed a complete rewrite for version 3.  This is not easy stuff, but nothing worthwhile is.  I have never considered the possibility that this may not fully work in any practical sense.  When I do release the Android Mides, the PHP parser functionality is likely to be beta.  I did look at the code once for Quericus, but without some sort of object diagram, I can’t really understand what I am looking at.  Much of the code is mixed up with the server code so it is difficult to figure out what is doing what.  I have read a few pages of the Mak book and I’m thinking about buying it just to see how he implemented the parser, but I am not sure it will do me any good.

I may consider creating my own language for use with Mides, and then compilers that generate PHP and Ruby source code from it while I work on versions for Apache, Nginx, etc… In lots of ways I think it would be much easier, and I could offer type checking and other things from compiled languages that help build more robust code.  I would maintain the PHP documentation that is present in Mides, and naturally you could FTP up the code to your server and test it there.  But I think that would be copping out, and I don’t like to have something like this beat me.  I do however have to realize that PHP wasn’t built in a day, and that this process will not happen immediately.

I keep asking myself what would Steve Jobs do, and the answer I keep coming up with is that he would go his own way, well I’ll keep stewing on it, and hopefully the end result is that I will become the sort of programmer that Joel would want to hire, although somehow I doubt I could ever achieve that level of software engineering excellence ;-).


Cupcake is on Again

Posted: May 22nd, 2009 | Author: | Filed under: android | Tags: , , , | No Comments »

Yahoo!!!

OFFICIAL: CUPCAKE IS GO!!!


Writing a PHP Interpreter for Mides

Posted: May 7th, 2009 | Author: | Filed under: android, iPhone, java, mides, PHP, Programming | Tags: , , , , , , , | No Comments »

For the past few weeks I have been working on writing a PHP interpereter for Mides.  As I know that I am not allowed to do it for iPhone, as it would involve downloading and executing scripting code, which is not allowed.  I have only been looking at the tokenizer for the iPhone, while I have been looking at the full monty for Android.

I actually have gotten to the point where the tokenizer is passing a lot of my tests, but there is a lot of ground to cover with PHP. Tommy Carlier’s blog on writing a parser has helped tremendously.  Still, even after getting the tokenizer working, I have to write an interpreter to execute the tokens.  This involves implementing the many methods that PHP has built in, so it will be a while until I get that completely done.

I am pretty frustrated with Mides for iPhone, actually, I wanted it to be a full PHP implementation and editor, but until the Terms and Conditions change it isn’t worth the effort to build an entire PHP interpreter just to have the app rejected. I am still improving the iPhone version, and the next update should be a huge improvement over what is currently there.  I had to remove the nesting because I need the memory for documentation search as well as the PHP tokenizer that I am working on.  Overall, the next update for Mides will make it better, it will be close to what I was hoping for, but it will never be completely what I was hoping for on iPhone.

The Android version of Mides on the other hand is shaping up nicely.  The Android text view supports color so I have some syntax highlighting happening which doesn’t hurt performance on the G1 too badly.  The tokenizer is mostly done, and now I am designing the interpreter.  Development on Android is going a lot faster since I don’t have to worry quite as much about memory leaks, although if you try you can still make them happen.

At any rate that is why there haven’t been any updates for Mides in a while, I have been working on localization, parsers, and interpreters.  I am spending most of my time on the iPhone version of Mides, but it seems that I am getting farther with the Android version, go figure.


From the Horse’s Mouth: Cupcake 1.5 Coming Out for Android

Posted: May 6th, 2009 | Author: | Filed under: android | Tags: , , , , | No Comments »

Finally T-Mobile, seriously:

http://forums.t-mobile.com/tmbl/board/message?board.id=Android_MR&thread.id=1


G1 Playback Audio Quality

Posted: February 27th, 2009 | Author: | Filed under: android | Tags: , , , , | No Comments »

While the G1 is less polished than the iPhone in many ways, the audio playback is not one of them.

Using the same headphones, and the same 256kbps, 44.1 kHz AAC source, the G1 at maximum volume has far less distortion and noise. At lower volumes there is a noticable increase in spatial realism. It’s interesting, having been in the Apple ecosystem for nearly 10 years, I had gotten used to assuming that Apple’s products would always use the highest quality components and represented the technical state of the art, however I now realize that that may not necessarily be true.

Apple’s design and usability are definitely still market leading, however their component quality can be occasionally questionable. Hopefully the 3rd generation iPhone will set a new standard for pereformance and quality, because if not, I’m going to have some difficulty justifying the price premium.


Idea For Making the G1 Screaming Fast

Posted: February 23rd, 2009 | Author: | Filed under: android, Companies, Google, java, Programming | Tags: , , , , , , , | No Comments »

There are a lot of cool things about the G1, but most people don’t talk about he hardware very often. It has a pretty snappy CPU, a good amount of RAM, and expandable storage. The coolest thing about the CPU is that it is a CPU/GPU dual-core combo unit. That gave me some ideas.

I know that Apple is working on their OpenCL that will allow applications on Mac OS X Snow Leopard to leverage the GPU for tasks that use it to advantage, like say floating point math, etc… It abstracts away all of the lower level coding that one would normally have to do for this type of functionality. Nvidia has some APIs for this type of programming, and I believe that Intel does as well. But one of the spaces where I see this as being especially beneficial is in the mobile space for phones and the like. The reason is simple, there is usually no math coprocessor like their x86 counterparts, and while most software doesn’t use it, others, like say speech recognition and image processing could get a huge boost out of it, and at a modest power cost.

What would be awesome is if Qualcomm published some sort of GPGPU language or spec to the Android foundation, and that made it’s way into the Android framework. Then when the GPU wasn’t being used, or when it was being used lightly, it could be used by code in the way one would spawn a thread to be run, but instead of running in the main CPU it could be run in the GPU, and free up the CPU to spend time doing something else.

I’m sure this has already been thought of and discussed by the Android brain trust, but it just occurred to me, and it would be 100% awesome if it made it into the framework.


Wow, the G1 Lets me Talk and Listen at the Same Time

Posted: February 19th, 2009 | Author: | Filed under: android | Tags: , , | No Comments »

I just found an additional fringe benefit of using the G1 over the iPhone, I can talk and hear at the same time. It was amazing, I noticed that I could clearly hear background sounds while talking. I guess I had gotten used to having a half-duplex channel for communication, and I had forgotten what a real phone sounds like. Bravo T-Mobile on remembering what the device’s primary function is!


HTML 5 Databases on iPhone

Posted: February 18th, 2009 | Author: | Filed under: android, Apple, Companies, Google, iPhone, java, Programming | Tags: , , , , , | No Comments »

Today I as looking into using HTML 5 databases on Android and iPhone. It turns out that the Android browser doesn’t seem to support window.createDatabase at all. It may be that it does work with Gears or something, but I didn’t try it.

What I did find, when enabling the developer menu for iPhone Safari was that in the 2.2.1 firmware, a user can view the databases that are currently stored on their device in the web browser by domain and delete them.

In the detail screen, it shows you how much data is currently stored in it, and it has a max, which appears to be stuck at 5 MB. I wonder if Apple has plans to improve the mobile Safari dev environment to allow for richer web applications at some point in the future when the AppStore revenues have died off a bit.

 

*EDIT*

Actually I did figure out that the G1 uses gears, so I guess they are equal, but gears doesn’t seem to care too much how much space I can use.  I haven’t tried the WorkerPool, or the local caching stuff, but I found another blog where the guy had an icon on his screen for a web-app.


I Got The G1

Posted: February 16th, 2009 | Author: | Filed under: android, Companies, Google, java, Programming | Tags: , , , | No Comments »

Yesterday I got the T-Mobile G1. I’ll be putting it through its paces as I work to port Mides IDE to it. So far, so good, however ido have to say that I’m a little slower with the physical keyboard than I anticipated. I’ll be glad when the cupcake firmware comes out and I can use the virtual keyboard.

The apps so far are mostly good, the device is definitely quicker than the iPhone as far as raw hardware performance, but the navigation around the OS is a bit slower. Still, I’m excited to see what I can do with this SDK.

Overall I’d say that the philosophy of the device is different. The G1 and Android are definitely aiming to be a little computer in your pocket, while the iPhone is still an iPod first, which makes it more of an internet appliance than a computer. As such, it makes a direct comparison challenging at best.

The G1 is ugly, and Android is unpolished, but for a company that doesn’t make music players, it is pretty good. More importantly, it fits in better with my way of thinking, and I appreciate the freedom that Google and T-Mobile have given us. Hopefully it will continue to improve, but either way I am pretty happy with it. Hopefully Mides will turn out to be as good as it is in my head for the G1, and eventually the iPhone. Competition is a good thing.