Bobbing for Kernels

See Bob. See Bob bob. Bob, Bob, bob!

Scheming, part 6

Posted by kernelbob on December 13, 2008

This morning I added vectors to my Scheme interpreter.  It was surprisingly easy.  It was nearly test-driven development — now that the scheme interpreter has a full unit test harness built in, I got the tests written before I finished the code (though not before I started).

Scheme R6RS does not define any operations that change a vector’s size.  I did not realize that until today.  It’s much easier to implement a vector that doesn’t have to grow or shrink.

If you want to see the code, it’s all checked in to Gitorious.

I did develop a unit test framework for the interpreter a while ago.  It abuses the C preprocessor to automatically collect test cases, and the “-t” switch tells the interpreter to run all the tests and exit.

Here’s the complete implementation of the vector-length procedure.  The single test is at the bottom.

DEFINE_PROC(L"vector-length")
{
    RETURN(make_fixnum(vector_len(pair_car(F_SUBJ))));
}

TEST_EVAL(L"(vector-length '#(a b c))", L"3");

TEST_EVAL takes two args: a string of one or more expressions to evaluate, and a string that should match the last expression’s value.  Both strings are wide, because the interpreter is Unicode throughout.

Did I mention that I’m learning Unicode in this project, too?

Anyway, it’s becoming more like a real interpreter, and it’s still fun.

Advertisements

One Response to “Scheming, part 6”

  1. […] Scheming, part 6 […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: