Look at this Stuff

Earlier I wrote about Ariel, my new bytecode, stack-based interpreter. One of the biggest grievances I had with it was the fact that it was written in Java. Ordinarily I would never shy away from using Java, but the recursive nature of that proposition in this context is mind-numbingly stupid. Coming from a strict C-based shop my first instinct was to use C. Five hours of reinventing the “standard library” wheel, I realized I was just spinning my own wheels: I don’t know how to make that wheel rounder, and it wasn’t the time to learn more about it either. C is largely ill suited for the object oriented programming I am used to. A coworker suggested I try C++. After getting over my initial nausea, I sat down to write.

Work on porting Ariel to C++ is on its third day, and is more or less complete. The same sample factorial program takes less time to execute than the JVM needs to startup on my aging T43 when compiled using Cygwin’s GCC; using MSVC or ICC cuts that number roughly in half. On my MacBook Pro, though, that same program executes in less than hundredth of a second. This is not bad for a few days of work, if I do say so myself. The only part of the original implementation is the more robust syntax features (specifically block definition) that required some ugly regular expressions. I’ve decided to ignore these features for now and instead focus on bolting on a true grammar.

I’ll be using LEG (a slight variation of a PEG) and I’ll hopefully be inventing my own AST. peg/leg is a C project, but I did find a useful port to C++. I forked it to produce warning free compiles using GCC. All that remains after that is to figure out how to traverse that tree to produce usable bytecode. In my limited experience with C++, I’d always stuck with Qt. Ariel’s C++ implementation though uses only the STL, and that hasn’t been a bad experience at all. Sticking with member initialization on the stack greatly simplifies memory management and STL containers are provide everything I need. The only issue I’ve been having is ridiculous error messages from the compiler when using templates- gah! I dare say, though, I’m having fun! and may have to revoke my previous sentiments on the topic. I, for one, welcome our new C++ overlords.

Wheel Making

For some time now, I’ve been flirting with the idea of creating a programming language for my own selfish desires and needs. The fact of the matter is, though, that I have precious little time to do what have to do, let alone what I would like to do. The vacation I recently took to Ireland left me with substantial flight time with my laptop, and my trusty IDE, Eclipse. I’d like to introduce you to the result of two trans-Atlantic flights: Ariel.

Although I’d previously salivated over writing and using my very own language, it turns out I am largely more interested in virtual machines, i.e., a sandboxed software interpreter. Ariel is newest step in that learning process; it is a stack-based, bytecode interpreter coupled with a simple assembly-like language. Despite that simplicity, the assembly language is enough to compute any well-defined algorithm (barring any bugs), making it Turing-complete (insofar as machines can actually be). In concept, this makes Ariel as powerful as any other known computing device, which is pretty darn awesome. In practice, the VM is particularly limited by Java’s int size. I know, everyone just skipped a beat.

Ariel itself is written in Java. I hope everyone remembers to take a breath before moaning; as I’ve said, the primary purpose of this VM is education. My goal for these two flights was to implement a working system in a language and platform I’m comfortable with. Eventually, translating the source to another language (e.g., C++, D) will be trivial. Another potential issue is the fact that there are many other VMs which are much more successful, faster, and generally better thought-out. The existence of these commercial grade platforms is a non-issue for me, though.

The basic language I’ve cobbled up so far is a nasty mix of regular expressions and convoluted logic. And beside that, the language isn’t at all joyful to write in. And there’s still that nagging irritation that I’m interpreting bytecode in bytecode, which itself is usually intepreted and may occasionly be compiled to native machine code. The headaches explaining what this could cause, cause headaches. Regardless, I have a good idea of where I want to move next. I’m currently working on implementing constant folding for the bytecode, and by extension, a framework to add other optimization passes. There is an obvious need to write a better, higher level language; I’ll either be writing my own lexer and parser for this, or building from an existing framework. I’ve even got ideas of writing my own JIT compiler for Ariel, targeting JVM bytecode instead of the usual machine code. Outside these immediate goals, I’ll be moving toward the full-scale production of the language I’ve been calling Casper, whose design will be heavily impacted by what I have learned here.

This is what has been keeping me busy en route this past week. I’ve learned enough to be even more interested in the subject, and will surely continue. So to the naysayers and those interested alike, I offer a new adage. Reinventing the wheel is usually a waste of time, unless you happen to know how to make the wheel rounder or you intend on learning a whole lot more about wheels.

Oh Honey, We’re Ridiculous

This morning I was pretty grumpy. I woke up late, burnt my coffee, and had to deal with a variety of family drama. Worse of all, though, was the state of the bathroom sink, with puddles of water and globs of toothpaste everywhere I tried to clean my hands. It made me very grumpy. I drove that way all the way to work, and I’m sure I stalked ito the building grumpily, too. I threw my bag into the corner, spilling my (burnt) coffee, and sat rather heavily onto the chair, though thankfully nothing broke off it. At this point, my whole cubicle was trying its damndest to budge itself away from wrath, and I can only imagine what would have happened to someone trying to intercept me this morning.

Thankfully, no one noticed me until I remembered a special perk that comes with working at Intel: free coffee. Now I know what you’re thinking, but they didn’t skimp on this stuff. Decidedly lightened, I walked downstairs making quite a bit less noise, and as I was walking back upstairs with my (free) coffee, I noticed just how awesomely professional expensive dress shoes sound in stairwells. This left me with such a smile, that when I went to the bathroom, and seeing my favorite sign, bust out laughing. Other people looked at me, but this didn’t bother me one bit. After finishing my business, I started to wash my hands, only to see a new sign, this one asking you to “wipe up any mess you leave for the courtesy of others.” Imagine my surprise.

I’m sure that you might be able to look into this and find a special cosmological meaning in this circle, but I found something better. After realizing all this, things became easier. I solved a problem in minutes whereas before I had been working on it all morning. I wrote this gem in perl, which finds and isolates a specific file, but unfortunately comes under two different names and two different flavors.

find(sub { push(@vsvars, $File::Find::name) if m/^v[sc]vars(?:32|64)\.bat$/}, $vs_dir);

Dealing with Microsoft is always such a pain, but after this single line and bit of noise, the rest was just easier; find the little things that give you the confidence to know that things do occasionally go right. So I’m not advocating never being grumpy, not at all. I do want you to remember, though, to look out for those little things that can turn a frown upside down.

Sound Purchase

Anyone who has a decently new Mac has the ability to use TOSLINK (commonly known as an optical cable) to transmit digital audio streams. Practically, this means that you hook your computer to a decent audio receiver and enjoy surround sound. This is one of my favorite features of the Macs I own, and something I use quite a bit, considering my Mac is my only entertainment system.

Optical cables are pretty easy to find, but the trouble is, that most Macs realize this feature as a Mini-TOSLINK jack, which for all intents and purposes is a 3.5 mm headphone jack. This design makes sense, but I’ve found it next to impossible to actually find the correct cable. Not even the local Apple store carries this obscurity (which is particularly odd, because they don’t sell a computer without this feature). Once upon a time, I did buy such a cable from Apple, and all was well about two years. Moving in and out of dorms eventually took its toll, snapping the adapter piece off the main cable. After that, my digital audio experience has never been the same, and I have spent many hours taping and adjusting the broken cable for sub-par, crackly audio at best. I’ve been an unhappy customer for a while.

I finally found time to go to the store (Fry’s electronics, which I actually semi-despise, but who doesn’t?) and lo and behold they had just the cable I needed. This is a new development at my local Fry’s; the best they could offer me was a cheap adapter that didn’t quite fit, meaning a similarly bad experience. The Shaxon optical cable (model: POTL35P2203MBK) I bought yesterday, in comparison, works perfectly. At only $15 for 3 meters, it wasn’t too expensive either. For advice to a Mac user needing an optical cable, let me point you there. And from a quality standpoint, let me tell you, that this thing is built like a beast. I can’t speak for durability quite yet, but the thing feels like quality in your hand, and that’s something that makes me a happy customer.