Introducing the L+ Compiler (and it’s free!)

I’m glad to announce that Emilio Pombo and I have just released the full source code for a custom compiler we built back in 2006!

The compiler, built as a mandatory Compilers assignment at the Catholic University of Uruguay, translates programs written in a C-like programming language, called L+, into Java Bytecode, suitable for execution on any platform with a Java Virtual Machine.

L+ supports most of C constructs (variables, branching, loops, functions, recursion, I/O), with some limitations and some additions (like native string handling). Here’s the mandatory “hello world” program written in L+.

// hello.lp
void main()
{
	print("Hello, World!");
}

The full source code is available as of now at google code, at http://code.google.com/p/lpcompiler/.

We have licensed the L+ compiler under the GPLv3, so you are free to download, compile, study, modify and ultimately redistribute your changes to anyone. I encourage you to check out the code, build it and try the examples that come bundled, so you can see what our little language can do :)

L+ is written in pure ANSI/ISO C++ and supports GNU/Linux, Mac OS X and Microsoft Windows. Dependencies are minimal and should build on any UNIX-like environment.

I want to thank Emilio Pombo and Leonardo Val for subscribing to the idea of releasing this program as Free Software and for also encouraging me to do so.

I hope you find the project as interesting as we found it fun to write it!

C as a programming learning language

Recently we were talking with a colleague from my University about how programming is taught nowadays in college.

In my University, the programming language used for teaching how to program is Java. I was discussing with a colleague the disadvantages I saw in using it as a language for teaching programming basics and why, in my mind, it didn’t stack up against teaching a language such as plain C. The following story aims to reproduce the high points in the conversation.

Suppose you are a first year professor, teaching programming fundamentals, and I’m one of your students.

How would you start? If you’re anything like me, the first “hands-on” lab, you’ll probably write a classic “Hello World” program on the whiteboard and ask your students to copy that.

In order to do so, you instruct them to open up their “IDEs”, create a new class and copy that into a “method”…

Wait… what? As your student, chances are that by now, I’m probably wondering what an “IDE” is anyway.

As a teacher on a basic programming course, you’ll probably just tell me that IDEs are used for programming and that I’ll most likely have to use that every time I want to write a program (otherwise, what are we using it for?)

Okay, but “why don’t I have to use an IDE every time I want to use an application in my Windows”? Bam… now you must either explain the difference between code “in development” and code “in production” or ask me just to trust you and please focus on the example on the board for now.

Okay, let’s move on. You said I had to copy what you wrote into a “class’ main method”… What is a “class” and what is a “method”? Ahh, okay. Here, you are trapped. Clearly these are OOP concepts, and you certainly don’t want to start digging into that before your students even write their first “Hello World” application. All you can do here is to ask them, once again, to just trust you by saying that “that’s the way things work in Java”.

So, your students have finally created a project in an “IDE”, created a “class”, defined a “public static” “method” and wrote the snippet you typed at the whiteboard in their IDE’s text editor.

The final code looks something like this:

public class HelloWorld
{
    public static void main(String[] args)
    {
        System.out.println("Hello, world!");
    }
}

They press “Play” on their “IDEs” and it just works. Everything is good.

Quick question: how many things in this code do you think are completely strange to your students? Probably all of it, since it’s the first time they’ve seen a program being coded. That’s okay, but how many of these things do you think are feasible to be explained in a programming 101 course? just println, maybe?

Why? Because in order to explain what a “class” is, what a “method” is, what “public”, “static”, “String[]”, “System” and “out” mean, you have to start digging into OOP concepts from day one. And you don’t want to do that, because chances are 1) you’ll end up confusing everyone and 2) most of that stuff will not really be useful for the course at all!

So… if teaching this way seems to be so complicated, why are we using Java for teaching programming? Well, I think we shouldn’t. C would be a much better candidate.

Compare the previous Java program with the following C equivalent:

#include <stdio.h>
int main()
{
    printf("Hello, world!");
    return 0;
}

Which one do you think is easier to explain? At most, what your will probably end up asking is “what does #include mean?”. You can just answer that it’s a file where printf is defined. That’s it. No strings attached. No “public static void Main(String[] args)”. Just a plain and simple printf.

What about all that “IDE” mess? Well, it turns out you can just use a plain text editor with some syntax highlighting and a C compiler and you are good to go. Compiling C code is extremely simple, if the system is correctly configured. Just issuing a command like the following will be enough for producing an executable file:

gcc hello.c -o hello.exe

It’s that easy. And best of all is that you get an executable. A plain .exe file on Windows. That’s what your students are expecting. That’s what a program is to them. An .exe file! In Java, your compiler would’ve generated a “.class” file, and then you would have to teach your students how to invoke the Java Virtual Machine, explain what a Runtime is and why is it needed to have your code (which is already complied, isn’t it?) run.

Another final advantage? By asking them to compile their code by hand, you indirectly have them learn how to use the Terminal application (believe me, most students don’t know what that is or how it’s used nowadays) and you are laying the foundation for them to learn how to use a non-GUI operating system.

You can achieve all this (and more) without having to ask them to “trust you” or to hold their questions for one to two years, until they have their first OOP course.

You could argue that, even though Java might seem more complicated at first, it’s more popular and thus it could never be a mistake to teach it. I completely agree, but I think it should be taught as a second or third language, not as the first one. Not only does Java require the student to wait until he or she goes through 2 or 3 programming courses in order to be able to fully “get” the language, but also, there are some concepts that they will just never learn this way, such as memory management or pointers.

So, please, if you’re a programming 101 teacher who’s doing Java, I ask you to reconsider. C does not have to be hard, and your students will thank you for conducting a exhaustive, self-contained course.