How to Write a Programming Language

30 October, 2008

It’s actually quite easy to write a programming language in C++, believe it or not. All you really have to do is follow Stroustrup’s own example: write a program that can translate your language’s code into C and compile that. After all, that’s how C++ started out! Here’s a step-by-step walkthrough of a simple programming language where the line printstr Hello, World! prints Hello, World! as the output. (Note: the system() calls used in this code might or might not work on non-Unix systems)

1. Include the following headers: iostream, fstream, vector, string, and cstdlib. Use the std namespace. Set up a simple main function with (int argc, char *argv[]) as arguments.
2. Let’s call this language Examplang. We want the user to run the command as examplang file.expl, so let’s make sure they always enter an argument following the program name:

if (!argv[1]) {
cout << “usage: ” <

return 1; 
3. Let’s declare some basic, yet very important, variables: a vector to read program lines into, an input file stream to read the source code, an output file stream to write to the c source code, and a string variable that holds the current line of the source code.
vector source;
ifstream source_code;
ofstream c_code;
string current_line;
4. Open up the source code and push back all its lines onto the vector. Then, get the number of lines in the source code by getting the vector’s size. (argv[1]);
while (getline (source_code, current_line)) 
source.push_back (current_line);
int source_code_size = source.size( );

5. Now, begin looking for keywords in the source code… this part gets tricky.

As a custom, i is used as the index variable in the for loop. The for loop reads each line of the file and stores the string available in the current line in the current_string variable. Each keyword name is stored in its own string before it’s used to ease access to its length later on if it’s found.

Then number of characters between the space after the keyword and the last character of the line is calculated and that value is used as programmed. In the case of the printstr keyword, the string after the space following the keyword until the end of the line is printed.

int i;“c_output.c”);

//note:due to technical restrictions, the greater-than and less-than signs around stdio.h could not be displayed  in the following line
c_code << “#include stdio.h
” <
for (i = 0; i
current_line =; 
string printstr = “printstr”; 
if ( (0, 9, “printstr”)) {
c_code << “printf (\””; 
int until_rest_of_string = current_line.length() – printstr.length(); 
string fstring = current_line.substr (8, until_rest_of_string); 
c_code <
6. Finish the outputted C source code and compile it. Once that’s done, close the file streams and end the main function.

c_code << “return 0;” <

system (“gcc c_output.c”); 
return 0;
That’s it! After this, one can easily extend the language to have variables, conditional statements, loops, and whole lot else. The code worked fine on OS X (I haven’t been able to test it on Fedora yet), but if there were any problems in copying the code into the post or if it doesn’t work on your system, please leave a comment, which will be followed up as soon as possible.

Windows 7 copies Fedora

28 October, 2008

200th post!

Windows 7 copied Linux again. Milestone 3 was recently announced at PDC, and now, Microsoft got another inspiration from Linux, and this time it wasn’t even a user feature. Compare the default desktops: Windows 7 and Fedora 9 GNOME (screenshots courtesy of the WinSuperSite and Wikipedia).

Larger Image:

Bringing Back the Old Aqua Theme

24 October, 2008

Did you know it’s possible to bring back the old Tiger and Panther Aqua theme back to Leopard? You can do it in one simple step – type this in (/Applications/Utilities/

defaults write -g AppleUseCoreUI -bool NO && defaults write NSGlobalDomain NSUseLeopardWindowValues NO && killall Finder

If you quit and restart all open applications, you’ll see they have the old window decoration. If you want to complete your experience, follow these instructions to get a Tiger-like opaque menu bar and a transparent dock.

To undo changes, change NO to YES, and vice-versa if necessary.

Good luck!

ImageBoot Doesn’t Surprise Me

24 October, 2008

I’m glad Apple’s finally (probably) bringing a Cocoa Finder in Snow Leopard, which may indicate some sort of Carbon wrapper for Cocoa applications in the future, but what doesn’t surprise me is ImageBoot. According to MacRumors, ImageBoot “should allow Macs to boot from a disk image”…. except that already exists. 

In fact, that’s how installed Ubuntu on my MacBook a few months back. I had the disk image, I went into Disk Utility, made a special partition on my hard drive for the Ubuntu installation, selected the hard drive in the source view, selected the “Restore” tab, and just followed the instructions.
Sure, it’s not booting directly off the disk image, but it has the same effect. 

OpenOffice 3 "Aqua" Interface Problems

15 October, 2008

Overall, I really like OOo 3 on OS X. It’s nice to be able to run it without X11, and the interface is faster, the new icons are nicer… but the interface can be terrible at points. Here is a list of the main “ugly spots” of the OOo “Aqua” interface.

  1. Just because it runs natively on OS X, it’s not necessarily Aqua. The ability to have pictures in the menus? That’s definitely anti-aqua. What’s with the odd button sizes and spacing? Not Aqua, again.
  2. If the user didn’t specify it in the System Preferences, buttons should not have an outer glow. It’s available so users can tab between controls (SysPrefs > Keyboard & Mouse > Shortcuts > Full keyboard access). If the user didn’t say he wants it, it shouldn’t be there.
  3. In OpenOffice Draw, there’s some odd coloring going on behind some controls.
  4. If I’m in the Help search tab, with the text field selected, and I press command-A, I expect all the text in the text field to be selected, not for the Database sub-application to open!
  5. Leopard and pinstripes don’t mix.
  6. Why a Windows 2000 My Documents icon in the Templates window? In OS X, either have Documents there, or the Home folder. But “My Documents” and an ancient Windows icon? Sorry, but that was just a bad move.
  7. Here’s the worst, in my opinion: the toolbars. The white OOo toolbars completely take away the ability for the app to be called “Aqua”! Take a look at all Aqua apps in Leopard. Unified toolbars, hello? I complained about the same thing in a previous post.

That’s about it for the worst parts of the interface, but on a positive note, I think it’s just about ready to be widely used, except for the interface! Please, please, please fix the interface!


Why Two MacBooks?

14 October, 2008

Seems that Apple doesn’t want to stop making MacBooks…

Let’s take a look at the history of it all. First off, the MacBook Pro was born. Pretty cool – built-in iSight, MagSafe, same old (but awesome) aluminum, great! Next, the nice white MacBook we have known for about almost three and a half years at the time of the writing of this post. Then, earlier this year, the MacBook Air was born as a very thin version of the MacBook Pro. Now the original MacBook Pro is dead, replaced by a new one, and MacBook has a brother!
I don’t get this – I think Apple should just phase out the white version and keep the new aluminum one, and charge US$1099 for it again! I mean, isn’t it faster and cheaper to produce the new aluminum MacBooks? Or is Apple trying to sell all of its remaining white MacBooks to ease the move to the new one?

The Better Interface of Google Docs

10 October, 2008

I remember when I first met Google Docs and how similar it was to Office 2007. Wow, how much has changed! 

I accessed it recently, and the interface for keeping tracks of your documents has been really useful with features such as nested folders.
The new layout also makes it easier to find a justified layout (see image), which is why I left Google Docs for a while.