Friday, January 22, 2010

Hello, world

I have been working on an interpreter for a simple dynamic OO programming language (vaguely like Java, but dynamically typed like Ruby.)

Today I got a hello world program to run:
[dhovemey@nobby]$ ThudC Hello.thud
[dhovemey@nobby]$ ThudDisAsm Main\$.thudc
class Main$ {
method main$() {
0: push_module_ref 2(System::IO)
3: load_field 4(out)
6: push_const 5(Hello, world!)
9: call 7(println) 2
13: pop
14: push_nil
15: return
[dhovemey@nobby]$ thud Hello.thud
Hello, world!
[dhovemey@nobby]$ echo $?
[dhovemey@nobby]$ cat Hello.thud
import System::IO;

IO.out.println("Hello, world!");
Oh yeah.

Note that in the example above, the source file "Hello.thud" produces a compiled class called "Main$" because that is a special class generated for "top-level" statements in a source file. The idea is to make the language useful as a scripting language, where many programs will be a sequence of statements that use the built-in classes.

The language (and its implementation) is currently called "Thud", which is one of the standard metasyntactic variables. It's also the title of one of the recent Discworld novels by Terry Pratchett.

There is still a lot of work to do before the interpreter is actually useful:
  • Currently, there is no garbage collection.
  • The built-in types support very few methods. For example, the built-in Int32 type does not support any arithmetic methods.
  • The IO facility is limited to printing to stdout.
I started this project quite a while ago because I was frustrated with both Perl and Ruby. I know Perl quite well, but find it to be exceedingly ugly to write programs in. I tend to like Ruby in theory, especially because it is a pure OO language (where all values are objects). However, I find that Ruby suffers from cryptic syntax. I gave up trying to learn Ruby on rails because I find that significant chunks of rails code---especially the ones that look like anonymous hashes hanging in space---to be deeply impenetrable.

I freely admit that the world does not need another OO scripting language, but it has been a lot of fun designing and implementing one.

The Thud virtual is based on a bytecode interpreter. There are exactly 17 opcodes. There are no static fields or static methods---instead, the same effect is achieved by having "modules", which are singleton classes. (For example, System::IO is a module.)

I will be posting the source code (free software, of course) in the near future.