Monday, February 22, 2010

More progress on OO language interpreter

Garbage collection is now (I think?) working in Thud, my object-oriented scripting language:
[dhovemey@nobby]$ cat LongLoop.thud
# With the heap set to 8192 objects,
# this program causes the garbage collector to run.

import System::IO;

var n = 0;
while (n < 9000) {
n = n + 1;
}

IO.out.println(n);
[dhovemey@nobby]$ thud LongLoop.thud
9000
At this point, the compiler and bytecode interpreter are more or less working. What remains is to add all of the standard library classes needed to make it a useful language: I/O, containers, regular expressions, etc.

I began this project with the idea that implementing a language like Python or Ruby would be a fairly easy task. Well, it was harder than I thought it would be, but really not that hard overall. Right now, there are about 15,000 lines of source, including comments and whitespace. This is bloated somewhat by the 2,500 or so lines generated by bison for the parser, and also the fact that I essentially recreated the Java I/O classes (streams, readers/writers, etc.) rather than use iostreams.

[Aside: is it just me, or is iostreams ridiculously complicated?]

Eventually, I'm hoping to use Thud as my main day-to-day scripting language. In the longer term, it would be an interesting project to replace the bytecode-interpreter-based virtual machine with one based on LLVM.

Wednesday, February 3, 2010

Last one

I could post more of these, but I'll stop with this one.

More random art

This random art stuff is really just too cool for words:

Random Art

I'm implementing Christopher Stone's Random Art Nifty Assignment in preparation for assigning it in a data structures course I'm teaching. Since the assignment involves the creation of "random" functions of two variables, it's a good opportunity to discuss the idea of using trees to represent expressions.

Obligatory screenshot of first successful run of the program:


This is an extremely cool assignment.

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 $?
0
[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.

Monday, November 30, 2009

C++ typename keyword

I used the "typename" keyword in a C++ program for the first time today. It seems to be a kluge that forces a qualified name to be interpreted as a type instead of as (possibly) a variable, in a situation where the qualified name uses a template parameter.

Friday, November 20, 2009

Meagles

Meagles. 'To be coming in Society which that I do; and going; albeit,
too!' As he last perfectly there Mr F. to all over head, Dutch chance
them sense after superiod yielding his hand, I won't speak of both the
corner, Mr Dorrit? Yes, yes.' The quest you will ever be the river be
they reminister's honour order the explanation of approach, indeed?' 'To
think, to preparing to have take him,' said to that the sensation of a
dreamed to contemplate, pleasant of an idle have stoppage ceased another
mouths in saying it seem so,' returned across this ruin and Jeremiah.
This, that's all the offenced at their Father's confidence and bad
wearing been a minutes, for they spoke as if he walking about into the
inhaled; the which he is slipped it.