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