version 1.1, 1999/10/08 02:12:01 |
version 1.7, 2005/07/03 11:08:54 |
|
|
|
/* $OpenXM: OpenXM/src/kan96xx/Kan/list.c,v 1.6 2005/06/16 05:07:23 takayama Exp $ */ |
/* list.c */ |
/* list.c */ |
#include <stdio.h> |
#include <stdio.h> |
|
#include <stdlib.h> |
#include "datatype.h" |
#include "datatype.h" |
#include "stackm.h" |
#include "stackm.h" |
#include "extern.h" |
#include "extern.h" |
Line 15 static warningList(char *s); |
|
Line 17 static warningList(char *s); |
|
*/ |
*/ |
|
|
struct object *newList(objp) |
struct object *newList(objp) |
struct object *objp; |
struct object *objp; |
{ |
{ |
struct object *op; |
struct object *op; |
op = (struct object *)sGC_malloc(sizeof(struct object)); |
op = (struct object *)sGC_malloc(sizeof(struct object)); |
Line 26 struct object *objp; |
|
Line 28 struct object *objp; |
|
/* Warning!! Make a copy of the object. It is important. */ |
/* Warning!! Make a copy of the object. It is important. */ |
*(op->lc.op) = *(objp); |
*(op->lc.op) = *(objp); |
op->rc.op = (struct object *)NULL; |
op->rc.op = (struct object *)NULL; |
|
op->attr = NULL; |
return(op); |
return(op); |
} |
} |
|
|
int klength(objp) |
int klength(objp) |
struct object *objp; |
struct object *objp; |
{ |
{ |
if (objp->tag != Slist) { |
if (objp->tag != Slist) { |
warningList("use klength() for object-list."); |
warningList("use klength() for object-list."); |
Line 43 struct object *objp; |
|
Line 46 struct object *objp; |
|
} |
} |
|
|
struct object listToArray(objp) |
struct object listToArray(objp) |
struct object *objp; |
struct object *objp; |
/* This function copies only the top level of the list */ |
/* This function copies only the top level of the list */ |
{ |
{ |
int n; |
int n; |
struct object ans; |
struct object ans = OINIT; |
int i; |
int i; |
if (objp->tag != Slist) { |
if (objp->tag != Slist) { |
warningList("use objectListToObjectArray() for object-list."); |
warningList("use objectListToObjectArray() for object-list."); |
Line 65 struct object *objp; |
|
Line 68 struct object *objp; |
|
} |
} |
|
|
struct object *arrayToList(obj) |
struct object *arrayToList(obj) |
struct object obj; |
struct object obj; |
/* obj.tag must be Sarray */ |
/* obj.tag must be Sarray */ |
{ |
{ |
struct object *op; |
struct object *op; |
struct object *list; |
struct object *list; |
Line 84 struct object obj; |
|
Line 87 struct object obj; |
|
} |
} |
|
|
struct object *vJoin(list1,list2) |
struct object *vJoin(list1,list2) |
struct object *list1,*list2; |
struct object *list1,*list2; |
/* Join[(a b), (c d)] ---> (a b c d) */ |
/* Join[(a b), (c d)] ---> (a b c d) */ |
/* Join [(),(a b)] ---> (a b) */ |
/* Join [(),(a b)] ---> (a b) */ |
/* We do not copy. NullList is express by (struct object *)NULL. |
/* We do not copy. NullList is express by (struct object *)NULL. |
cf. isNullList() |
cf. isNullList() |
*/ |
*/ |
{ |
{ |
/* |
/* |
printf("list1="); printObjectList(list1); |
printf("list1="); printObjectList(list1); |
printf("\nlist2="); printObjectList(list2); printf("\n"); |
printf("\nlist2="); printObjectList(list2); printf("\n"); |
*/ |
*/ |
|
|
if (isNullList(list1)) return(list2); |
if (isNullList(list1)) return(list2); |
Line 112 struct object *list1,*list2; |
|
Line 115 struct object *list1,*list2; |
|
} |
} |
|
|
struct object car(list) |
struct object car(list) |
struct object *list; |
struct object *list; |
{ |
{ |
if (list->tag != Slist) { |
if (list->tag != Slist) { |
warningList("car() is called for a non-list object."); |
warningList("car() is called for a non-list object."); |
Line 124 struct object *list; |
|
Line 127 struct object *list; |
|
} |
} |
|
|
struct object *cdr(list) |
struct object *cdr(list) |
struct object *list; |
struct object *list; |
{ |
{ |
if (list->tag != Slist) { |
if (list->tag != Slist) { |
warningList("cdr() is called for a non-list object."); |
warningList("cdr() is called for a non-list object."); |
Line 135 struct object *list; |
|
Line 138 struct object *list; |
|
} |
} |
|
|
|
|
void printObjectList(op) |
static void printObjectList0(op,br) |
struct object *op; |
struct object *op; int br; |
{ |
{ |
if (op == NULL) return; |
if (op == NULL) return; |
if (isNullList(op)) return; |
if (isNullList(op)) return; |
if (op->tag == Slist) { |
if (op->tag == Slist) { |
printObjectList(op->lc.op); |
if (br) printf("<"); |
|
printObjectList0(op->lc.op,1); |
printf(", "); |
printf(", "); |
printObjectList(op->rc.op); |
printObjectList0(op->rc.op,0); |
|
if (br) printf(">"); |
}else { |
}else { |
printObject(*op,0,stdout); |
printObject(*op,0,stdout); |
} |
} |
} |
} |
|
|
|
void printObjectList(op) |
|
struct object *op; |
|
{ |
|
printObjectList0(op,1); |
|
} |
|
|
memberQ(list1,obj2) |
memberQ(list1,obj2) |
struct object *list1; |
struct object *list1; |
struct object obj2; |
struct object obj2; |
/* If obj2 is an member of list1, the functions the position. |
/* If obj2 is an member of list1, the functions the position. |
memberQ( (1 (2 3) 4), 4) ----> 3. |
memberQ( (1 (2 3) 4), 4) ----> 3. |
*/ |
*/ |
{ |
{ |
Line 167 struct object obj2; |
|
Line 178 struct object obj2; |
|
} |
} |
|
|
static errorList(str) |
static errorList(str) |
char *str; |
char *str; |
{ |
{ |
fprintf(stderr,"list.c: %s\n",str); |
fprintf(stderr,"list.c: %s\n",str); |
exit(10); |
exit(10); |
} |
} |
|
|
static warningList(str) |
static warningList(str) |
char *str; |
char *str; |
{ |
{ |
fprintf(stderr,"Warning. list.c: %s\n",str); |
fprintf(stderr,"Warning. list.c: %s\n",str); |
} |
} |
|
|
|
struct object KvJoin(struct object listo1,struct object listo2) { |
|
struct object rob = OINIT; |
|
struct object *op1,*op2; |
|
if (listo1.tag == Snull) return listo2; |
|
if (listo2.tag == Snull) return listo1; |
|
if ((listo1.tag == Slist) && (listo2.tag == Slist)) { |
|
op1 = (struct object *)sGC_malloc(sizeof(struct object)); |
|
op2 = (struct object *)sGC_malloc(sizeof(struct object)); |
|
if ((op1 == NULL) || (op2 == NULL)) errorKan1("%s\n","KvJoin, No more memory."); |
|
*op1 = listo1; *op2 = listo2; |
|
rob = *(vJoin(op1,op2)); |
|
return rob; |
|
}else{ |
|
errorKan1("%s\n","KvJoin(Slist,Slist)"); |
|
} |
|
} |
|
struct object Kcar(struct object listo) { |
|
if (listo.tag == Snull) return listo; |
|
if (listo.tag == Slist) { |
|
return car(&listo); |
|
}else{ |
|
errorKan1("%s\n","Kcar(Slist)"); |
|
} |
|
} |
|
struct object Kcdr(struct object listo) { |
|
struct object *op; |
|
struct object rob = OINIT; |
|
if (listo.tag == Snull) return listo; |
|
if (listo.tag == Slist) { |
|
op = cdr(&listo); |
|
if (isNullList(op)) { |
|
rob = NullObject; |
|
}else{ |
|
rob = *op; |
|
} |
|
return rob; |
|
}else{ |
|
errorKan1("%s\n","Kcar(Slist)"); |
|
} |
|
} |
|
struct object KlistToArray(struct object listo) { |
|
if (listo.tag == Snull) { |
|
return newObjectArray(0); |
|
} |
|
if (listo.tag == Slist) { |
|
return listToArray(&listo); |
|
}else{ |
|
errorKan1("%s\n","KlistToArray(Slist)"); |
|
} |
|
} |
|
struct object KarrayToList(struct object ob) { |
|
struct object *op; |
|
if (ob.tag != Sarray) { |
|
errorKan1("%s\n","KarrayToList(Sarray)"); |
|
} |
|
op = arrayToList(ob); |
|
if (isNullList(op)) return NullObject; |
|
return *op; |
|
} |
|
|
/********************** test codes for Stest: ********************/ |
/********************** test codes for Stest: ********************/ |
/* test of objectArrayToObjectList. in Stest: stackmachine.c |
/* test of objectArrayToObjectList. in Stest: stackmachine.c |
{ |
{ |
struct object *list; |
struct object *list; |
list = arrayToList(ob1); |
list = arrayToList(ob1); |
ob1 = listToArray(list); |
ob1 = listToArray(list); |
push(ob1); |
push(ob1); |
} |
} |
test for memberQ(). |
test for memberQ(). |
{ |
{ |
struct object *list; |
struct object *list; |
list = objectArrayToObjectList(pop()); |
list = objectArrayToObjectList(pop()); |
printf("\nmemberQ()=%d\n",memberQ(list,ob1)); |
printf("\nmemberQ()=%d\n",memberQ(list,ob1)); |
} |
} |
|
|
*/ |
*/ |