diff --git a/base.c b/base.c index d637a30..4cc18bd 100644 --- a/base.c +++ b/base.c @@ -5,7 +5,7 @@ #include "base.h" #include "symbol_table.h" -struct object * car(struct object * o){ +object * car(object * o){ if (!atom_p(o)) { if (o!=nil) return o->data->car; @@ -15,7 +15,7 @@ struct object * car(struct object * o){ check(false, "argument not a list"); } -struct object * cdr(struct object * o){ +object * cdr(object * o){ if (!atom_p(o)) { if (o!=nil) return o->data->cdr; @@ -25,31 +25,31 @@ struct object * cdr(struct object * o){ check(false, "argument not a list"); } -struct object * atom_p(struct object * o){ +object * atom_p(object * o){ if (o!=nil && o->atom_p) return sym("t"); else return nil; } -struct object * eq(struct object * o1, struct object * o2){ +object * eq(object * o1, object * o2){ if ((atom_p(o1) && atom_p(o2) && o1==o2) || (o1==o2 && o2==nil)) return sym("t"); else return nil; } -struct object * list(int len, ...) { +object * list(int len, ...) { va_list ap; - struct object * start = nil; - struct object * cur = nil; - struct object * o = nil; + object * start = nil; + object * cur = nil; + object * o = nil; int i; start = cons(nil, nil); cur = start; va_start(ap, len); for (i=0;idata->car = o; if (i==len-1) cur->data->cdr = nil; @@ -61,25 +61,25 @@ struct object * list(int len, ...) { return start; } -struct object * cons(struct object * o1, struct object * o2){ - struct object * o = (struct object *) malloc(sizeof(struct object)); +object * cons(object * o1, object * o2){ + object * o = (object *) malloc(sizeof(object)); o->atom_p = false; o->function_p = false; - struct cons_cell * cc = (struct cons_cell *) malloc(sizeof(struct cons_cell)); + cons_cell * cc = (cons_cell *) malloc(sizeof(cons_cell)); o->data = cc; cc->car = o1; cc->cdr = o2; return o; } -struct object * quote(struct object * o){ +object * quote(object * o){ return o; } -static void print_cons(struct object * o); +static void print_cons(object * o); static void print_atom(atom a); -struct object * print(struct object * o){ +object * print(object * o){ if (o==nil) { printf("nil"); return nil; @@ -93,7 +93,7 @@ struct object * print(struct object * o){ return nil; } -static void print_cons(struct object * o){ +static void print_cons(object * o){ /* printf("("); print(car(o)); printf(" . "); @@ -113,18 +113,18 @@ static void print_atom(atom a){ printf("%s", st_id_to_name(a)); } -struct object * null(struct object * o){ +object * null(object * o){ return eq(o, nil); } -struct object * and(struct object * x, struct object * y){ +object * and(object * x, object * y){ if (!null(x) && !null(y)) return sym("t"); else return nil; } -struct object * not(struct object * o){ +object * not(object * o){ if (o == nil) return sym("t"); else @@ -132,35 +132,35 @@ struct object * not(struct object * o){ } -struct object * sym(const char * name){ +object * sym(const char * name){ return st_insert(name); } -struct object * cadr(struct object * o){ +object * cadr(object * o){ return car(cdr(o)); } -struct object * caddr(struct object * o){ +object * caddr(object * o){ return car(cdr(cdr(o))); } -static struct object * cadddr(struct object * o){ +static object * cadddr(object * o){ return car(cdr(cdr(cdr(o)))); } -struct object * caar(struct object * o){ +object * caar(object * o){ return car(car(o)); } -struct object * caddar(struct object * o){ +object * caddar(object * o){ return car(cdr(cdr(car(o)))); } -struct object * cadar(struct object * o){ +object * cadar(object * o){ return car(cdr(car(o))); } -struct object * append(struct object * x, struct object * y){ +object * append(object * x, object * y){ if (null(x)) return y; @@ -168,7 +168,7 @@ struct object * append(struct object * x, struct object * y){ } -struct object * pair(struct object * x, struct object * y) { +object * pair(object * x, object * y) { if (null(x) && null(y)) return nil; @@ -179,7 +179,7 @@ struct object * pair(struct object * x, struct object * y) { exit(1); } -struct object * assoc(struct object * x, struct object * y) { +object * assoc(object * x, object * y) { if (null(x)) { fprintf(stderr, "nil is not a variablename\n"); exit(EXIT_FAILURE); @@ -195,13 +195,13 @@ struct object * assoc(struct object * x, struct object * y) { return assoc(x, cdr(y)); } -static struct object * evcon(struct object * c, struct object *a); -static struct object * evlis(struct object * m, struct object *a); +static object * evcon(object * c, object *a); +static object * evlis(object * m, object *a); -struct object * globals = nil; -struct object * globals_end = nil; +object * globals = nil; +object * globals_end = nil; -struct object * eval(struct object * e, struct object *a){ +object * eval(object * e, object *a){ if (null(e)) return nil; if (atom_p(e)) @@ -238,10 +238,10 @@ struct object * eval(struct object * e, struct object *a){ } if (eq(car(e), sym("defun"))) { - struct object *name = cadr(e); - struct object *params = caddr(e); - struct object *body = cadddr(e); - struct object *pair = cons(list(2, name, list(3, sym("lambda"), params, body)), nil); + object *name = cadr(e); + object *params = caddr(e); + object *body = cadddr(e); + object *pair = cons(list(2, name, list(3, sym("lambda"), params, body)), nil); globals_end->data->cdr = pair; globals_end = pair; return eval(name, a); @@ -267,14 +267,14 @@ struct object * eval(struct object * e, struct object *a){ exit(1); } -struct object * evcon(struct object * c, struct object *a){ +object * evcon(object * c, object *a){ if (eval(caar(c), a)) return eval(cadar(c), a); return evcon(cdr(c), a); } -struct object * evlis(struct object * m, struct object *a){ +object * evlis(object * m, object *a){ if (null(m)) return nil; diff --git a/base.h b/base.h index 5dfb9ce..8ee25a5 100644 --- a/base.h +++ b/base.h @@ -9,45 +9,47 @@ typedef int atom; #define nil NULL -struct object { +typedef struct _cons_cell cons_cell; +typedef struct _object object; + +struct _object { bool atom_p; bool function_p; - struct cons_cell * data; + cons_cell * data; int symbol_index; }; -struct cons_cell { - struct object * car; - struct object * cdr; +struct _cons_cell { + object * car; + object * cdr; }; -extern struct object * globals; -extern struct object * globals_end; - -struct object * car(struct object * o); -struct object * cdr(struct object * o); -struct object * quote(struct object * o); -struct object * atom_p(struct object * o); -struct object * eq(struct object * o1, struct object * o2); -struct object * cons(struct object * o1, struct object * o2); -struct object * print(struct object * o); -struct object * reader(); -struct object * sym(const char * name); -struct object * list(int len, ...); +extern object * globals; +extern object * globals_end; -struct object * null(struct object * o); -struct object * and(struct object * x, struct object * y); -struct object * not(struct object * o); +object * car(object * o); +object * cdr(object * o); +object * quote(object * o); +object * atom_p(object * o); +object * eq(object * o1, object * o2); +object * cons(object * o1, object * o2); +object * print(object * o); +object * reader(); +object * sym(const char * name); +object * list(int len, ...); -struct object * cadr(struct object * o); -struct object * caddr(struct object * o); -struct object * cadar(struct object * o); -struct object * caar(struct object * o); -struct object * caddar(struct object * o); -struct object * append(struct object * x, struct object * y); -struct object * pair(struct object * x, struct object * y); -struct object * assoc(struct object * x, struct object * y); -struct object * eval(struct object * e, struct object * a); +object * null(object * o); +object * and(object * x, object * y); +object * not(object * o); +object * cadr(object * o); +object * caddr(object * o); +object * cadar(object * o); +object * caar(object * o); +object * caddar(object * o); +object * append(object * x, object * y); +object * pair(object * x, object * y); +object * assoc(object * x, object * y); +object * eval(object * e, object * a); #ifdef DEBUG #define LOG(fmt, ...) fprintf(stderr, "%s: " fmt "\n", __PRETTY_FUNCTION__, ##__VA_ARGS__); diff --git a/hashmap.h b/hashmap.h index 9b4b7f6..0a32675 100644 --- a/hashmap.h +++ b/hashmap.h @@ -4,7 +4,7 @@ #include #include -typedef struct _hashmap_entry { +typedef struct { void * key; void * value; uint32_t hash; @@ -13,7 +13,7 @@ typedef struct _hashmap_entry { typedef uint32_t (*hashmap_hash_fn)(const void * key, const size_t size); typedef bool (*hashmap_equals_fn)(const void * key1, const void * key2, const size_t size); -typedef struct _hashmap { +typedef struct { hashmap_entry *entries; double load_factor; size_t size; diff --git a/parser.c b/parser.c index e2ad75c..c1cfc4e 100644 --- a/parser.c +++ b/parser.c @@ -51,24 +51,24 @@ static void match(const char * s){ } } -static struct object * atom_or_list(); +static object * atom_or_list(); -static struct object * list_body(){ +static object * list_body(){ if (is_c()) return nil; return cons(atom_or_list(), list_body()); } -static struct object * parse_list() { +static object * parse_list() { match("("); - struct object * o; + object * o; o = list_body(); match(")"); return o; } -static struct object * parse_atom() { - static struct object * o; +static object * parse_atom() { + static object * o; check_not_nil(_sym, "parse error"); if (strcmp(_sym, "nil")==0) o = nil; @@ -78,7 +78,7 @@ static struct object * parse_atom() { return o; } -static struct object * atom_or_list(){ +static object * atom_or_list(){ if (is_sym()) return parse_atom(); else if (is_o()) @@ -89,19 +89,19 @@ static struct object * atom_or_list(){ } } -static struct object * lisp_program(){ +static object * lisp_program(){ return atom_or_list(); } -static struct object * parse(){ +static object * parse(){ _sym = (char *)lex(); return lisp_program(); } extern FILE * yyin; -struct object * parse_string(char *s){ - struct object *o; +object * parse_string(char *s){ + object *o; const char * fn = "/tmp/tinylisp-123"; FILE * f = fopen(fn, "w"); fwrite(s, strlen(s), 1, f); diff --git a/parser.h b/parser.h index 3b1003c..806fb60 100644 --- a/parser.h +++ b/parser.h @@ -1,8 +1,9 @@ #ifndef __PARSER_H #define __PARSER_H +#include "base.h" enum symbol_types { SYMBOL, OPENING_BRACE, CLOSING_BRACE }; -struct object * parse_string(char *s); +object * parse_string(char *s); #endif diff --git a/symbol_table.c b/symbol_table.c index cb071e5..493d4a3 100644 --- a/symbol_table.c +++ b/symbol_table.c @@ -3,15 +3,15 @@ #include "symbol_table.h" #include "base.h" -static struct symbol_table st; +static symbol_table st; -struct object * st_insert(const char * name){ - struct object * o; +object * st_insert(const char * name){ + object * o; if ((o = st_lookup(name)) != NULL) return o; check(st.size < SYMBOL_TABLE_SIZE, "symbol table size exceeded"); - struct symbol * sym = &st.symbols[st.size]; + symbol * sym = &st.symbols[st.size]; o = &sym->object; o->atom_p = true; o->function_p = false; @@ -24,7 +24,7 @@ struct object * st_insert(const char * name){ return o; } -struct object * st_lookup(const char * name){ +object * st_lookup(const char * name){ int i; for (i=0; i