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->fun == atom2->fun;
35 static bool equal_cons(struct Cons *cons1, struct Cons *cons2)
39 return equal(cons1->car, cons2->car) && equal(cons1->cdr, cons2->cdr);
42 bool equal(struct Expr obj1, struct Expr obj2)
44 if (obj1.type != obj2.type) {
50 return equal_atoms(obj1.atom, obj2.atom);
53 return equal_cons(obj1.cons, obj2.cons);
62 bool nil_p(struct Expr obj)
65 && strcmp(obj.atom->sym, "nil") == 0;
68 bool symbol_p(struct Expr obj)
70 return obj.type == EXPR_ATOM
71 && obj.atom->type == ATOM_SYMBOL;
74 bool cons_p(struct Expr obj)
76 return obj.type == EXPR_CONS;
79 bool list_p(struct Expr obj)
85 if (obj.type == EXPR_CONS) {
86 return list_p(obj.cons->cdr);
92 bool list_of_symbols_p(struct Expr obj)
98 if (obj.type == EXPR_CONS && symbol_p(obj.cons->car)) {
99 return list_of_symbols_p(obj.cons->cdr);
105 bool callable_p(struct Expr obj)
111 if (length_of_list(obj) < 2) {
115 if (!symbol_p(obj.cons->car)) {
119 if (strcmp("lambda", obj.cons->car.atom->sym) != 0) {
123 if (!list_of_symbols_p(obj.cons->cdr.cons->car)) {
130 long int length_of_list(struct Expr obj)
134 while (!nil_p(obj)) {
142 struct Expr assoc(struct Expr key, struct Expr alist)
144 while (cons_p(alist)) {
145 if (cons_p(alist.cons->car) && equal(alist.cons->car.cons->car, key)) {
146 return alist.cons->car;
149 alist = alist.cons->cdr;
155 static struct Expr list_rec(Gc *gc, size_t n, va_list args)
161 struct Expr obj = va_arg(args, struct Expr);
162 return CONS(gc, obj, list_rec(gc, n - 1, args));
165 struct Expr list(Gc *gc, size_t n, ...)
169 struct Expr obj = list_rec(gc, n, args);