From d45a5b21de22438c004e1db96a8f154da09cdc0e Mon Sep 17 00:00:00 2001 From: Oskari Timperi Date: Wed, 14 May 2014 22:27:34 +0300 Subject: Initial commit - tokenizing, parsing and basic eval support - arithmetic (+, -, *, /) - quote - atom for checking if the arg is an atom (i.e. not a list) - eq for checking equality - > for checking order - if - some unit testing - simple repl - mem management needs improvement :-) --- atom.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 atom.h (limited to 'atom.h') diff --git a/atom.h b/atom.h new file mode 100644 index 0000000..3b716fa --- /dev/null +++ b/atom.h @@ -0,0 +1,57 @@ +#ifndef ATOM_H +#define ATOM_H + +#define LIST_GET_ATOM(LIST) ((struct atom *) (LIST)->data) + +#define ATOM_TYPE(LIST) ((LIST_GET_ATOM(LIST))->type) +#define IS_INT(LIST) ((ATOM_TYPE(LIST)) == ATOM_INT) +#define IS_STR(LIST) ((ATOM_TYPE(LIST)) == ATOM_STR) +#define IS_SYM(LIST) ((ATOM_TYPE(LIST)) == ATOM_SYMBOL) +#define IS_LIST(LIST) ((ATOM_TYPE(LIST)) == ATOM_LIST) + +#define IS_TRUE(LIST) (LIST_GET_ATOM(LIST) == &true_atom) +#define IS_FALSE(LIST) (LIST_GET_ATOM(LIST) == &false_atom) +#define IS_NIL(LIST) (LIST_GET_ATOM(LIST) == &nil_atom) + +#define CAR(LIST) LIST + +#define CDR(LIST) ((LIST) != NULL ? (LIST)->next : NULL) +#define CDDR(LIST) CDR(CDR(LIST)) + +enum +{ + ATOM_NIL, + ATOM_INT, + ATOM_STR, + ATOM_SYMBOL, + ATOM_LIST +}; + +struct atom +{ + union + { + long l; + struct + { + char *str; + int len; + } str; + struct list *list; + }; + char type; +}; + +struct list; + +struct atom *atom_new(char type); +struct atom *atom_new_int(long l); +struct atom *atom_new_str(const char *str, int len); +struct atom *atom_new_sym(const char *sym, int len); +struct atom *atom_new_list(struct list *list); + +extern struct atom true_atom; +extern struct atom false_atom; +extern struct atom nil_atom; + +#endif -- cgit v1.2.3