7 #include "script/expr.h"
9 struct Expr atom_as_expr(struct Atom *atom)
19 struct Expr cons_as_expr(struct Cons *cons)
29 void print_atom_as_sexpr(struct Atom *atom)
35 printf("%s", atom->sym);
39 printf("%f", atom->num);
43 printf("\"%s\"", atom->str);
48 void print_cons_as_sexpr(struct Cons *cons)
53 print_expr_as_sexpr(cons->car);
55 print_expr_as_sexpr(cons->cdr);
59 void print_expr_as_sexpr(struct Expr expr)
63 print_atom_as_sexpr(expr.atom);
67 print_cons_as_sexpr(expr.cons);
72 struct Expr create_expr_from_str(const char *str)
74 /* TODO(#283): create_expr_from_str is not implemented */
76 return atom_as_expr(0);
79 void destroy_expr(struct Expr expr)
83 destroy_atom(expr.atom);
87 destroy_cons(expr.cons);
92 struct Cons *create_cons(struct Expr car, struct Expr cdr)
94 struct Cons *cons = malloc(sizeof(struct Cons));
105 void destroy_cons(struct Cons *cons)
107 destroy_expr(cons->car);
108 destroy_expr(cons->cdr);
112 struct Atom *create_atom(enum AtomType type, ...)
114 struct Atom *atom = malloc(sizeof(struct Atom));
120 va_start(args, type);
127 const char *arg_str = va_arg(args, const char *);
128 const size_t n = strlen(arg_str);
129 char *str = malloc(sizeof(char) * (n + 1));
136 memcpy(str, arg_str, n + 1);
141 atom->num = (float) va_arg(args, double);
150 void destroy_atom(struct Atom *atom)
152 switch (atom->type) {