Perpetual Weekend

6 April 2007

Integrated Record Types

Filed under: Uncategorized — shaurz @ 17:30

The interpreter for Amp (the code name for my Lisp-like language) has been largely re-written since my first release. The code is not yet available. I am going to set up a darcs repo to reduce the effort of distributing new versions.

I have switched back to an AST representation of programs. My next goal is to use explicit continuations in the interpreter. This will stop the C stack from blowing up and allow implementation of first-class continuations and tail-call optimisation.

I decided to switch the implementation language from C to C++ (eventually it will be self-compiling). I’m not sure if this was a good idea or not but it has worked out quite well so far. I have lightweight classes abstracting values (pointers or fixnums) and heap objects (which have a header with type information) so I can easily change the low-level representation when needed.

I have implemented a rudimentary record type protocol similar in style to R6RS. They are integrated in the implementation rather than botched on top of vectors as a second-class feature. The current interface is simplistic but will suffice for now.

(make-record-type name slot-names)
(constructor type)
(accessor type slot-name)
(mutator type slot-name)

Pairs (a.k.a. “cons cells”) are simply records with two slots, car and cdr. The functions cons, car and cdr are no longer defined as primitives since they are merely constructors and accessors of the pair type.

(define cons (constructor <pair>))
(define car (accessor <pair> 'car))
(define cdr (accessor <pair> 'cdr))
(define set-car! (mutator <pair> 'car))
(define set-cdr! (mutator <pair> 'cdr))

The pair type must still be defined as a primitive since knowledge of its definition is required by the implementation - in other words it is also defined as a C struct. We could define it like this:

(define <pair> (make-record-type 'pair '(car cdr))

Please disregard the quasiquote code from my previous posts. After reading the standard I realised that nesting worked quite differently than I thought. In the latest unreleased code the examples in R5RS now work as advertised. Hopefully I got it right this time.

Scheme’s quasiquote seems to be subtly different than Common Lisp’s. For example, ``,,0 evaluates to 0 in SBCL and (quasiquote (unquote 0)) in MzScheme.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.