8 #include "script/expr.h"
11 static char *string_duplicate(const char *str,
14 if (str_end != NULL && str > str_end) {
18 const size_t n = str_end == NULL ? strlen(str) : (size_t) (str_end - str);
19 char *dup_str = malloc(sizeof(char) * (n + 1));
20 if (dup_str == NULL) {
24 memcpy(dup_str, str, n);
30 struct Expr atom_as_expr(struct Atom *atom)
40 struct Expr cons_as_expr(struct Cons *cons)
50 static struct Atom *clone_atom(Gc *gc, struct Atom *atom)
56 return create_number_atom(gc, atom->num);
59 return create_string_atom(gc, atom->str, NULL);
62 return create_symbol_atom(gc, atom->sym, NULL);
68 struct Expr clone_expr(Gc *gc, struct Expr expr)
72 return atom_as_expr(clone_atom(gc, expr.atom));
77 clone_expr(gc, expr.cons->car),
78 clone_expr(gc, expr.cons->cdr)));
86 struct Expr void_expr(void)
95 void print_atom_as_sexpr(struct Atom *atom)
101 printf("%s", atom->sym);
105 printf("%f", atom->num);
109 printf("\"%s\"", atom->str);
114 void print_cons_as_sexpr(struct Cons *head)
118 struct Cons *cons = head;
121 print_expr_as_sexpr(cons->car);
123 while (cons->cdr.type == EXPR_CONS) {
124 cons = cons->cdr.cons;
126 print_expr_as_sexpr(cons->car);
129 if (cons->cdr.atom->type != ATOM_SYMBOL ||
130 strcmp("nil", cons->cdr.atom->sym) != 0) {
132 print_expr_as_sexpr(cons->cdr);
138 void print_expr_as_sexpr(struct Expr expr)
140 /* TODO(#296): print_expr_as_sexpr doesn't support lists */
143 print_atom_as_sexpr(expr.atom);
147 print_cons_as_sexpr(expr.cons);
155 void destroy_expr(struct Expr expr)
159 destroy_atom(expr.atom);
163 destroy_cons(expr.cons);
171 struct Cons *create_cons(Gc *gc, struct Expr car, struct Expr cdr)
173 struct Cons *cons = malloc(sizeof(struct Cons));
181 if (gc_add_expr(gc, cons_as_expr(cons)) < 0) {
189 void destroy_cons(struct Cons *cons)
194 struct Atom *create_number_atom(Gc *gc, float num)
196 struct Atom *atom = malloc(sizeof(struct Atom));
200 atom->type = ATOM_NUMBER;
203 if (gc_add_expr(gc, atom_as_expr(atom)) < 0) {
211 struct Atom *create_string_atom(Gc *gc, const char *str, const char *str_end)
213 struct Atom *atom = malloc(sizeof(struct Atom));
219 atom->type = ATOM_STRING;
220 atom->str = string_duplicate(str, str_end);
222 if (atom->str == NULL) {
226 if (gc_add_expr(gc, atom_as_expr(atom)) < 0) {
234 if (atom->str != NULL) {
243 struct Atom *create_symbol_atom(Gc *gc, const char *sym, const char *sym_end)
245 struct Atom *atom = malloc(sizeof(struct Atom));
251 atom->type = ATOM_SYMBOL;
252 atom->sym = string_duplicate(sym, sym_end);
254 if (atom->sym == NULL) {
258 if (gc_add_expr(gc, atom_as_expr(atom)) < 0) {
266 if (atom->sym != NULL) {
275 void destroy_atom(struct Atom *atom)
277 switch (atom->type) {