9 static bool equal_atoms(struct Atom *atom1, struct Atom *atom2)
14 if (atom1->type != atom2->type) {
18 switch (atom1->type) {
20 return strcmp(atom1->sym, atom2->sym) == 0;
23 return atom1->num == atom2->num;
26 return strcmp(atom1->str, atom2->str) == 0;
29 return atom1->native.fun == atom2->native.fun
30 && atom1->native.param == atom2->native.param;
36 static bool equal_cons(struct Cons *cons1, struct Cons *cons2)
40 return equal(cons1->car, cons2->car) && equal(cons1->cdr, cons2->cdr);
43 bool equal(struct Expr obj1, struct Expr obj2)
45 if (obj1.type != obj2.type) {
51 return equal_atoms(obj1.atom, obj2.atom);
54 return equal_cons(obj1.cons, obj2.cons);
63 bool nil_p(struct Expr obj)
66 && strcmp(obj.atom->sym, "nil") == 0;
69 bool symbol_p(struct Expr obj)
71 return obj.type == EXPR_ATOM
72 && obj.atom->type == ATOM_SYMBOL;
75 bool cons_p(struct Expr obj)
77 return obj.type == EXPR_CONS;
80 bool list_p(struct Expr obj)
86 if (obj.type == EXPR_CONS) {
87 return list_p(obj.cons->cdr);
93 bool list_of_symbols_p(struct Expr obj)
99 if (obj.type == EXPR_CONS && symbol_p(obj.cons->car)) {
100 return list_of_symbols_p(obj.cons->cdr);
106 bool lambda_p(struct Expr obj)
112 if (length_of_list(obj) < 2) {
116 if (!symbol_p(obj.cons->car)) {
120 if (strcmp("lambda", obj.cons->car.atom->sym) != 0) {
124 if (!list_of_symbols_p(obj.cons->cdr.cons->car)) {
131 long int length_of_list(struct Expr obj)
135 while (!nil_p(obj)) {
143 struct Expr assoc(struct Expr key, struct Expr alist)
145 while (cons_p(alist)) {
146 if (cons_p(alist.cons->car) && equal(alist.cons->car.cons->car, key)) {
147 return alist.cons->car;
150 alist = alist.cons->cdr;
156 static struct Expr list_rec(Gc *gc, size_t n, va_list args)
162 struct Expr obj = va_arg(args, struct Expr);
163 return CONS(gc, obj, list_rec(gc, n - 1, args));
166 struct Expr list(Gc *gc, size_t n, ...)
170 struct Expr obj = list_rec(gc, n, args);