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;
70 bool symbol_p(struct Expr obj)
72 return obj.type == EXPR_ATOM
73 && obj.atom->type == ATOM_SYMBOL;
76 bool number_p(struct Expr obj)
78 return obj.type == EXPR_ATOM
79 && obj.atom->type == ATOM_NUMBER;
82 bool string_p(struct Expr obj)
84 return obj.type == EXPR_ATOM
85 && obj.atom->type == ATOM_STRING;
88 bool cons_p(struct Expr obj)
90 return obj.type == EXPR_CONS;
93 bool list_p(struct Expr obj)
99 if (obj.type == EXPR_CONS) {
100 return list_p(obj.cons->cdr);
106 bool list_of_symbols_p(struct Expr obj)
112 if (obj.type == EXPR_CONS && symbol_p(obj.cons->car)) {
113 return list_of_symbols_p(obj.cons->cdr);
119 bool lambda_p(struct Expr obj)
125 if (length_of_list(obj) < 2) {
129 if (!symbol_p(obj.cons->car)) {
133 if (strcmp("lambda", obj.cons->car.atom->sym) != 0) {
137 if (!list_of_symbols_p(obj.cons->cdr.cons->car)) {
144 long int length_of_list(struct Expr obj)
148 while (!nil_p(obj)) {
156 struct Expr assoc(struct Expr key, struct Expr alist)
158 while (cons_p(alist)) {
159 if (cons_p(alist.cons->car) && equal(alist.cons->car.cons->car, key)) {
160 return alist.cons->car;
163 alist = alist.cons->cdr;
169 static struct Expr list_rec(Gc *gc, size_t n, va_list args)
175 struct Expr obj = va_arg(args, struct Expr);
176 return CONS(gc, obj, list_rec(gc, n - 1, args));
179 struct Expr list(Gc *gc, size_t n, ...)
183 struct Expr obj = list_rec(gc, n, args);