quakeforge/ruamoko/scheme/Cons.r
Brian Koropoff 281b683e14 Initial commit of a future partial implementation of the R5RS Scheme
standard, implemented in Ruamoko.  Currently works for a few simple
"Hello, world!" programs.
2005-05-01 11:48:36 +00:00

82 lines
1.4 KiB
R

#include "string.h"
#include "Cons.h"
#include "Nil.h"
#include "defs.h"
Cons cons (SchemeObject car, SchemeObject cdr)
{
return [Cons newWithCar: car cdr: cdr];
}
@implementation Cons
+ (id) newWithCar: (SchemeObject) a cdr: (SchemeObject) d
{
return [[self alloc] initWithCar: a cdr: d];
}
- (id) initWithCar: (SchemeObject) a cdr: (SchemeObject) d
{
car = a;
cdr = d;
if (!car) {
print("Cons: WARNING: NIL car\n");
} else if (!cdr) {
print("cons: WARNING: NIL cdr\n");
}
return [super init];
}
- (SchemeObject) car
{
return car;
}
- (void) car: (SchemeObject) a
{
car = a;
}
- (SchemeObject) cdr
{
return cdr;
}
- (void) cdr: (SchemeObject) d
{
cdr = d;
}
- (void) mark
{
[super mark];
[car mark];
[cdr mark];
}
- (string) printForm
{
local string acc = "", res;
local id cur, next = NIL;
for (cur = self; cur; cur = next) {
next = [cur cdr];
acc = acc + [[cur car] printForm];
if (next == [Nil nil]) {
next = NIL;
} else if (next && ![next isKindOfClass: [Cons class]]) {
acc = acc + " . " + [next printForm];
next = NIL;
} else if (next) {
acc = acc + " ";
}
}
res = str_new();
str_copy(res, sprintf("(%s)", acc));
return res;
}
@end