leao/include/sexp.h

48 lines
1.1 KiB
C

#include <stdbool.h>
/* Datastructures representing s-exprs */
struct sexp_list;
enum sexp_form {
sexp_bool,
sexp_int,
sexp_string,
sexp_symbol,
sexp_list,
};
struct sexp {
enum sexp_form form;
union {
bool bool_lit;
/* Integer literals are not directly converted to numbers */
char *lexeme;
struct sexp_list *list;
};
};
struct sexp_list {
struct sexp *elem;
struct sexp_list *next;
};
/* Helper to create a list by appending at its end in O(1) time */
struct sexp_list_builder {
struct sexp_list *head;
struct sexp_list *last;
};
/* Returns false if allocation fails */
bool sexp_list_append(struct sexp_list_builder *b, struct sexp *elem);
/* If allocation fails, these functions return NULL */
struct sexp *sexp_make_bool(bool v);
struct sexp *sexp_make_int(char *lexeme);
struct sexp *sexp_make_string(char *lexeme);
struct sexp *sexp_make_symbol(char *lexeme);
struct sexp *sexp_make_list(struct sexp_list *list);
void free_sexp_list(struct sexp_list *head);
void free_sexp(struct sexp *s);