compiler + imagination = super*funtimes

At work we have an interesting requirement…

We started off needing to parse and evaluate simple statements. It kind of grew a bit, as you can imagine. This is in the context of an app that needs to keep track of typed user data, and I was intrigued by the fact we could use the internal type system to keep some control over the thing instead of trying to translate it into a string for the native environment to eval() (which is hard when you’re dealing with rich types).

I wrote an arithmetic evaluator first and then started plugging into it the type system. It first occurred to me that this might actually be pretty awesome when I started defining operators on the numeric type, i.e. operators are implemented by different types differently. That means I can do all sorts of crazy things. I can easily define arbitrary symbols to perform arbitrary operations on arbitrary types! I can subtract dates. I can do complex number arithmetic. It doesn’t matter that the language we’re writing it in can’t natively represent complex numbers. Thanks, Turing. I’ve essentially written a language where we can overload operators, a la C++. By the time I added dot-notation syntax to it to allow a binding between the native environment and the app’s statement language I was positively giggling like a schoolgirl. It’s *awesome*. I can define a property called ‘squareroot’ on Number, implement it however I want, and the string ’16.squareroot’ gets evaluated to 4. And ’16.squareroot.squareroot’ => 2.

And it gets better. Since it’s all typed internally, all I have to do is make sure the return value’s type gets inferred somehow and then I can start mixing and matching. ‘”1234″.length.squareroot’ => 2. That’s a string property that returns an integer and then uses an integer property. And everything still works fine with barely any extra effort.

Who says commercial programming has to be boring?

Advertisements

I like blogging

Posted in Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: