1 #include "system/stacktrace.h"
10 static bool equal_atoms(struct Atom *atom1, struct Atom *atom2)
15 if (atom1->type != atom2->type) {
19 switch (atom1->type) {
21 return strcmp(atom1->sym, atom2->sym) == 0;
24 return atom1->num == atom2->num;
27 return strcmp(atom1->str, atom2->str) == 0;
30 return atom1 == atom2;
33 return atom1->native.fun == atom2->native.fun
34 && atom1->native.param == atom2->native.param;
40 static bool equal_cons(struct Cons *cons1, struct Cons *cons2)
44 return equal(cons1->car, cons2->car) && equal(cons1->cdr, cons2->cdr);
47 bool equal(struct Expr obj1, struct Expr obj2)
49 if (obj1.type != obj2.type) {
55 return equal_atoms(obj1.atom, obj2.atom);
58 return equal_cons(obj1.cons, obj2.cons);
67 bool nil_p(struct Expr obj)
70 && strcmp(obj.atom->sym, "nil") == 0;
74 bool symbol_p(struct Expr obj)
76 return obj.type == EXPR_ATOM
77 && obj.atom->type == ATOM_SYMBOL;
80 bool number_p(struct Expr obj)
82 return obj.type == EXPR_ATOM
83 && obj.atom->type == ATOM_NUMBER;
86 bool string_p(struct Expr obj)
88 return obj.type == EXPR_ATOM
89 && obj.atom->type == ATOM_STRING;
92 bool cons_p(struct Expr obj)
94 return obj.type == EXPR_CONS;
97 bool list_p(struct Expr obj)
103 if (obj.type == EXPR_CONS) {
104 return list_p(obj.cons->cdr);
110 bool list_of_symbols_p(struct Expr obj)
116 if (obj.type == EXPR_CONS && symbol_p(obj.cons->car)) {
117 return list_of_symbols_p(obj.cons->cdr);
123 bool lambda_p(struct Expr obj)
125 return obj.type == EXPR_ATOM
126 && obj.atom->type == ATOM_LAMBDA;
129 long int length_of_list(struct Expr obj)
133 while (!nil_p(obj)) {
141 struct Expr assoc(struct Expr key, struct Expr alist)
143 while (cons_p(alist)) {
144 if (cons_p(CAR(alist)) && equal(CAR(CAR(alist)), key)) {
154 const char *specials[] = {
155 "set", "quote", "begin",
156 "defun", "lambda", "λ",
160 bool is_special(const char *name)
164 size_t n = sizeof(specials) / sizeof(const char*);
165 for (size_t i = 0; i < n; ++i) {
166 if (strcmp(name, specials[i]) == 0) {
176 list_rec(Gc *gc, const char *format, va_list args)
179 trace_assert(format);
187 long int p = va_arg(args, long int);
188 return CONS(gc, NUMBER(gc, p),
189 list_rec(gc, format + 1, args));
193 const char* p = va_arg(args, const char*);
194 return CONS(gc, STRING(gc, p),
195 list_rec(gc, format + 1, args));
199 const char* p = va_arg(args, const char*);
200 return CONS(gc, SYMBOL(gc, p),
201 list_rec(gc, format + 1, args));
205 struct Expr p = va_arg(args, struct Expr);
206 return CONS(gc, p, list_rec(gc, format + 1, args));
210 fprintf(stderr, "Wrong format parameter: %c\n", *format);
219 list(Gc *gc, const char *format, ...)
222 va_start(args, format);
223 struct Expr result = list_rec(gc, format, args);
229 struct Expr bool_as_expr(Gc *gc, bool condition)
231 return condition ? T(gc) : NIL(gc);