quakeforge/ruamoko/scheme/Cons.r

99 lines
1.7 KiB
R
Raw Normal View History

#include "string.h"
#include "Cons.h"
#include "Nil.h"
#include "defs.h"
#include "SchemeString.h"
2011-02-14 13:39:43 +00:00
Cons *cons (SchemeObject *car, SchemeObject *cdr)
{
return [Cons newWithCar: car cdr: cdr];
}
2011-02-14 13:39:43 +00:00
integer length (SchemeObject *foo)
{
local integer len;
2011-02-14 13:39:43 +00:00
for (len = 0; [foo isKindOfClass: [Cons class]]; foo = [(Cons *) foo cdr]) {
len++;
}
return len;
}
2011-02-14 13:39:43 +00:00
BOOL isList (SchemeObject *ls)
{
return ls == [Nil nil] ||
([ls isKindOfClass: [Cons class]] &&
2011-02-14 13:39:43 +00:00
isList([(Cons*) ls cdr]));
}
@implementation Cons
2011-02-14 13:39:43 +00:00
+ (id) newWithCar: (SchemeObject *) a cdr: (SchemeObject *) d
{
return [[self alloc] initWithCar: a cdr: d];
}
2011-02-14 13:39:43 +00:00
- (id) initWithCar: (SchemeObject *) a cdr: (SchemeObject *) d
{
car = a;
cdr = d;
if (!car) {
2011-01-14 03:07:40 +00:00
print("Cons: WARNING: nil car\n");
} else if (!cdr) {
2011-01-14 03:07:40 +00:00
print("cons: WARNING: nil cdr\n");
}
return [super init];
}
2011-02-14 13:39:43 +00:00
- (SchemeObject *) car
{
return car;
}
2011-02-14 13:39:43 +00:00
- (void) car: (SchemeObject *) a
{
car = a;
}
2011-02-14 13:39:43 +00:00
- (SchemeObject *) cdr
{
return cdr;
}
2011-02-14 13:39:43 +00:00
- (void) cdr: (SchemeObject *) d
{
cdr = d;
}
- (void) markReachable
{
[car mark];
[cdr mark];
}
- (string) printForm
{
local string acc = "";
2011-01-14 03:07:40 +00:00
local id cur, next = nil;
for (cur = self; cur; cur = next) {
next = [cur cdr];
acc = acc + [[cur car] printForm];
if (next == [Nil nil]) {
2011-01-14 03:07:40 +00:00
next = nil;
} else if (next && ![next isKindOfClass: [Cons class]]) {
acc = acc + " . " + [next printForm];
2011-01-14 03:07:40 +00:00
next = nil;
} else if (next) {
acc = acc + " ";
}
}
return [[String newFromString: sprintf("(%s)", acc)] stringValue];
}
@end