Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

What Scheme does /prog/ use?

Name: Anonymous 2012-01-28 22:31

I have been using Chicken for a little bit. Used Racket for like 5 minutes. R6RS is PIG DISGUSTING!

That is all.

Name: Anonymous 2012-01-29 7:43

>>7
Well then write your own. It's not hard and it's a lot of fun. In fact, programming a Scheme interpreter in C is a lot like programming in Scheme once all the basic abstractions are laid out. Start with a basic generic object type.

typedef struct object {
    int type;
    union {
        struct {
            object *car;
            object *cdr;
        } pair;
        char *symbol;
        char *string;
        double number; /* or implement bignums if you want */
        ...
    } val;
} object;


Now write cons, car, cdr, symbol, string, number, etc. and you're ready to the write the parser.

Since code is data in Scheme, you can write a parser that simply translates text to data and not worry about executing it. Write it just like you would a recursive function in Scheme. The parse takes a "list" (either an array of chars or a FILE stream) and returns a tree (of objects) that represent the code as data.

You should also write display concurrently so you can have display(parse(stdin)); in your main loop until the parser is completed. Once the output is equivalent to the input, you're ready to move on.

Next step is eval. Once you have your generic Scheme object and all the necessary primitive-like constructors, predicates, accessors and mutators, you could translate the metacircular evaluator from SICP to C in a few hours. The MCE is not that elegant, however, and will certainly be slow as balls. The way it stores the environment for instance is too complex and can be greatly simplified. Primitive and compound procedures are better represented as native C structs contained in object rather than "tagged lists". MCE's eval however is a good starting point for learning how to implement the define, lambda, and if special forms.

Next, write apply. Binding primitives to your application will be time consuming and tedious, but it's rewarding once your able to start running real Scheme programs. Read R5RS for implementation details.

At this point, you can either write special forms for and, or, cond, let, begin, etc. or you can just implement define-syntax and do the rest in Scheme. It all depends on whether you want a pure C implementation or just a tiny C core. Both have pros and cons (no pun, etc. etc.).

Good luck.

Name: Anonymous 2012-01-29 13:09

>>8
I'd argue that a bytecode (stack machine) compiler would be easier to write.  Your move.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List