Paste: records as a library feature

Author: ks
Mode: factor
Date: Tue, 25 May 2010 07:16:22
Plain Text |
static Bool = NewBoolType();
static Int = NewIntType();
static Pointer[T] = NewPointerType(T);
static Array[T,n] = NewArrayType(T, n);
static Tuple[...T] = NewStructType(...T);


//
// RecordType
//

static RecordType[ID,F] = NewStructType(...FieldTypes(...F));

static FieldTypes = NewProcedure();
overload FieldTypes(first, ...rest) = (FieldType(...first), ...FieldTypes(...rest));
overload FieldTypes() = ();

static FieldType = NewProcedure();
overload FieldType(name, type) = type;

static RecordId = NewGlobalVariable(1);

static NextRecordId = NewProcedure();
overload NextRecordId() {
    var x = RecordId^;
    RecordId^ += 1;
    return x;
}


//
// Record
//

static Record = NewProcedure();
overload Record(fields) = RecordType[NextRecordId(), fields];


//
// fieldRef
//

static fieldRef = NewProcedure();

[ID, FIELDS]
overload fieldRef(x:RecordType[ID,FIELDS], ident) {
    return ref structRef(x, static FieldIndex(ident, static 0, ...FIELDS));
}


static FieldIndex = NewProcedure();

overload FieldIndex(ident, i, first, ...rest) {
    var name, type == first;
    if (name == ident)
        return i;
    return FieldIndex(ident, static i+1, ...rest);
}

overload FieldIndex(ident, i) {
    error("field not found");
}


//
// Node[T]
//

static Node[T] = Record((#value, T), (#next, Pointer[Node[T]]));

New Annotation

Summary:
Author:
Mode:
Body: