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;
32 static bool equal_cons(struct Cons *cons1, struct Cons *cons2)
36 return equal(cons1->car, cons2->car) && equal(cons1->cdr, cons2->cdr);
39 bool equal(struct Expr obj1, struct Expr obj2)
41 if (obj1.type != obj2.type) {
47 return equal_atoms(obj1.atom, obj2.atom);
50 return equal_cons(obj1.cons, obj2.cons);
59 bool nil_p(struct Expr obj)
62 && strcmp(obj.atom->sym, "nil") == 0;
65 bool symbol_p(struct Expr obj)
67 return obj.type == EXPR_ATOM
68 && obj.atom->type == ATOM_SYMBOL;
71 bool cons_p(struct Expr obj)
73 return obj.type == EXPR_CONS;
76 bool list_p(struct Expr obj)
82 if (obj.type == EXPR_CONS) {
83 return list_p(obj.cons->cdr);
89 long int length_of_list(struct Expr obj)
101 struct Expr assoc(struct Expr key, struct Expr alist)
103 while (cons_p(alist)) {
104 if (cons_p(alist.cons->car) && equal(alist.cons->car.cons->car, key)) {
105 return alist.cons->car;
108 alist = alist.cons->cdr;
114 static struct Expr list_rec(Gc *gc, size_t n, va_list args)
120 struct Expr obj = va_arg(args, struct Expr);
121 return CONS(gc, obj, list_rec(gc, n - 1, args));
124 struct Expr list(Gc *gc, size_t n, ...)
128 struct Expr obj = list_rec(gc, n, args);