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->native.fun == atom2->native.fun
31 && atom1->native.param == atom2->native.param;
37 static bool equal_cons(struct Cons *cons1, struct Cons *cons2)
41 return equal(cons1->car, cons2->car) && equal(cons1->cdr, cons2->cdr);
44 bool equal(struct Expr obj1, struct Expr obj2)
46 if (obj1.type != obj2.type) {
52 return equal_atoms(obj1.atom, obj2.atom);
55 return equal_cons(obj1.cons, obj2.cons);
64 bool nil_p(struct Expr obj)
67 && strcmp(obj.atom->sym, "nil") == 0;
71 bool symbol_p(struct Expr obj)
73 return obj.type == EXPR_ATOM
74 && obj.atom->type == ATOM_SYMBOL;
77 bool number_p(struct Expr obj)
79 return obj.type == EXPR_ATOM
80 && obj.atom->type == ATOM_NUMBER;
83 bool string_p(struct Expr obj)
85 return obj.type == EXPR_ATOM
86 && obj.atom->type == ATOM_STRING;
89 bool cons_p(struct Expr obj)
91 return obj.type == EXPR_CONS;
94 bool list_p(struct Expr obj)
100 if (obj.type == EXPR_CONS) {
101 return list_p(obj.cons->cdr);
107 bool list_of_symbols_p(struct Expr obj)
113 if (obj.type == EXPR_CONS && symbol_p(obj.cons->car)) {
114 return list_of_symbols_p(obj.cons->cdr);
120 bool lambda_p(struct Expr obj)
126 if (length_of_list(obj) < 2) {
130 if (!symbol_p(obj.cons->car)) {
134 if (!is_lambda(obj.cons)) {
138 if (!list_of_symbols_p(obj.cons->cdr.cons->car)) {
145 long int length_of_list(struct Expr obj)
149 while (!nil_p(obj)) {
157 struct Expr assoc(struct Expr key, struct Expr alist)
159 while (cons_p(alist)) {
160 if (cons_p(CAR(alist)) && equal(CAR(CAR(alist)), key)) {
170 bool is_lambda(struct Cons *cons) {
171 return (strcmp(cons->car.atom->sym, "lambda") == 0) ||
172 (strcmp(cons->car.atom->sym, "λ") == 0);
175 const char *specials[] = {
176 "set", "quote", "begin",
177 "defun", "lambda", "λ",
178 "defun", "when", "quasiquote"
181 bool is_special(const char *name)
185 size_t n = sizeof(specials) / sizeof(const char*);
186 for (size_t i = 0; i < n; ++i) {
187 if (strcmp(name, specials[i]) == 0) {
197 list_rec(Gc *gc, const char *format, va_list args)
200 trace_assert(format);
208 long int p = va_arg(args, long int);
209 return CONS(gc, NUMBER(gc, p),
210 list_rec(gc, format + 1, args));
214 const char* p = va_arg(args, const char*);
215 return CONS(gc, STRING(gc, p),
216 list_rec(gc, format + 1, args));
220 const char* p = va_arg(args, const char*);
221 return CONS(gc, SYMBOL(gc, p),
222 list_rec(gc, format + 1, args));
226 struct Expr p = va_arg(args, struct Expr);
227 return CONS(gc, p, list_rec(gc, format + 1, args));
231 fprintf(stderr, "Wrong format parameter: %c\n", *format);
240 list(Gc *gc, const char *format, ...)
243 va_start(args, format);
244 struct Expr result = list_rec(gc, format, args);
250 struct Expr bool_as_expr(Gc *gc, bool condition)
252 return condition ? T(gc) : NIL(gc);