commit be2fc792de2dc3c11eb8750b74db8791495c8ff4
parent df9e94f3ba048cdaac8a87d501313bfa1f529c0a
Author: glenda <glenda@device>
Date: Mon, 31 Oct 2022 20:41:24 +0000
sync list
Diffstat:
3 files changed, 41 insertions(+), 52 deletions(-)
diff --git a/src/list/list.c b/src/list/list.c
@@ -3,59 +3,20 @@
#include "list.h"
-enum {
- CNull = 0,
- CPair,
- CAtom,
- CError = -1,
-};
-
-typedef struct Cell Cell;
-struct Cell {
- char type;
- void *p;
-};
-
-typedef struct Pair Pair;
-struct Pair {
- union {
- Cell p[2];
- struct {
- Cell fst;
- Cell snd;
- };
- };
-};
-
-Cell
-cons(Cell fst, Cell snd)
-{
- if ((snd.type != CPair) && (snd.type != CNull)) snd = cons(snd, (Cell){CNull, 0});
- Pair *p = malloc(sizeof(Pair));
- p->fst = fst;
- p->snd = snd;
- return (Cell){CPair, p};
-}
-
-Cell
-head(Cell c)
-{
- return (c.type == CPair) ? ((Pair *)c.p)->fst : (Cell){CError, "not a pair"};
-}
-
-Cell
-tail(Cell c)
+void
+printlist(Cell c)
{
- return (c.type == CPair) ? ((Pair *)c.p)->snd : (Cell){CError, "not a pair"};
+ if (c.type == CNull) return;
+ Cell fst = head(c);
+ print("%s\n", fst.p);
+ printlist(tail(c));
}
void
main(void)
{
- char *a = "hello", *b = "world";
- Cell A = {CAtom, a}, B = {CAtom, b};
- Cell C = cons(A, B);
- Cell r = head(tail(C));
- char *s = r.p;
- print("%s\n", s);
+ char *a = "uno", *b = "duo", *c = "tres";
+ Cell A = {CAtom, a}, B = {CAtom, b}, C = {CAtom, c};
+ Cell D = cons(A, cons(B, C));
+ printlist(D);
}
diff --git a/src/list/list.h b/src/list/list.h
@@ -0,0 +1,28 @@
+enum {
+ CNull = 0,
+ CPair,
+ CAtom,
+ CError = -1,
+};
+
+typedef struct Cell Cell;
+struct Cell {
+ char type;
+ void *p;
+};
+
+typedef struct Pair Pair;
+struct Pair {
+ union {
+ Cell p[2];
+ struct {
+ Cell fst;
+ Cell snd;
+ };
+ };
+};
+
+Cell cons (Cell, Cell);
+Cell head (Cell);
+Cell tail (Cell);
+int eq(Cell, Cell);
diff --git a/src/list/mkfile b/src/list/mkfile
@@ -1,7 +1,7 @@
</$objtype/mkfile
-TARG=list
-OFILES=list.$O
+LIB=liblist.a$O
+OFILES=liblist.$O
HFILES=list.h
-</sys/src/cmd/mkone
+</sys/src/cmd/mklib