From b86d3361836935297c6aedb0318ed8a34e4c52f0 Mon Sep 17 00:00:00 2001 From: Felix Perktold Date: Sun, 18 Jan 2026 14:21:34 +0100 Subject: refactor lisp.h into public lisp_api.h and internal lisp.h --- Makefile | 2 +- lisp.c | 1 + lisp.h | 45 --------------------------------------------- lisp.y | 1 + lisp_api.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 1 + test.c | 1 + 7 files changed, 57 insertions(+), 46 deletions(-) create mode 100644 lisp_api.h diff --git a/Makefile b/Makefile index a04723e..6feb95e 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ OBJS = lisp.o lisp.tab.o lex.yy.o main: main.c $(OBJS) $(CC) $(CFLAGS) -o $@ main.c $(OBJS) $(LIBS) -lisp.o: lisp.c lisp.h lex.yy.o +lisp.o: lisp.c lisp_api.h lisp.h lex.yy.o $(CC) $(CFLAGS) -c lisp.c lisp.tab.c lisp.tab.h: lisp.y diff --git a/lisp.c b/lisp.c index 4e143f1..d5f9600 100644 --- a/lisp.c +++ b/lisp.c @@ -3,6 +3,7 @@ #include #include #include +#include "lisp_api.h" #include "lisp.h" #include "lex.yy.h" #include "lisp.tab.h" diff --git a/lisp.h b/lisp.h index a422c9b..a972ecc 100644 --- a/lisp.h +++ b/lisp.h @@ -4,51 +4,6 @@ typedef struct value value; typedef struct env env; -typedef enum { - VT_INT, - VT_DOUBLE, - VT_SYMBOL, - VT_STRING, - VT_NIL, - VT_PAIR, - VT_LAMBDA, - VT_BUILTIN -} val_type; - -struct value { - val_type type; - union { - int i; - double d; - const char *sym; - const char *str; - struct { - value *car; - value *cdr; - } pair; - struct { - value *params; - value *body; - env *env; - } lambda; - struct { - value *(*fn) (env *, value *); - } builtin; - } as; -}; - -value *make_int(int i); -value *make_double(double d); -value *make_symbol(const char *sym); -value *make_string(const char *str); -value *make_nil(); -value *make_lambda(env *e, value *params, value *body); -value *make_builtin(value *(*fn) (env *, value *)); -value *cons(value *car, value *cdr); -value *car(value *cons); -value *cdr(value *cons); -value *reverse(value *list); - void print_value(value *val); void println_value(value *val); // environments, TODO: make this use hashtable diff --git a/lisp.y b/lisp.y index a4ed142..ec246f3 100644 --- a/lisp.y +++ b/lisp.y @@ -1,6 +1,7 @@ %{ #include #include +#include "lisp_api.h" #include "lisp.h" int yylex(void); diff --git a/lisp_api.h b/lisp_api.h new file mode 100644 index 0000000..70d317b --- /dev/null +++ b/lisp_api.h @@ -0,0 +1,52 @@ +#ifndef LISP_API_H +#define LISP_API_H + +typedef struct value value; +typedef struct env env; + +typedef enum { + VT_INT, + VT_DOUBLE, + VT_SYMBOL, + VT_STRING, + VT_NIL, + VT_PAIR, + VT_LAMBDA, + VT_BUILTIN +} val_type; + +struct value { + val_type type; + union { + int i; + double d; + const char *sym; + const char *str; + struct { + value *car; + value *cdr; + } pair; + struct { + value *params; + value *body; + env *env; + } lambda; + struct { + value *(*fn) (env *, value *); + } builtin; + } as; +}; + +value *make_int(int i); +value *make_double(double d); +value *make_symbol(const char *sym); +value *make_string(const char *str); +value *make_nil(); +value *make_lambda(env *e, value *params, value *body); +value *make_builtin(value *(*fn) (env *, value *)); +value *cons(value *car, value *cdr); +value *car(value *cons); +value *cdr(value *cons); +value *reverse(value *list); + +#endif diff --git a/main.c b/main.c index b675dfa..73adf2d 100644 --- a/main.c +++ b/main.c @@ -3,6 +3,7 @@ #include #include #include +#include "lisp_api.h" #include "lisp.h" #include "lex.yy.h" #include "lisp.tab.h" diff --git a/test.c b/test.c index 95cce3f..5bc1ff4 100644 --- a/test.c +++ b/test.c @@ -1,6 +1,7 @@ #include #include #include +#include "lisp_api.h" #include "lisp.h" #include "lex.yy.h" #include "lisp.tab.h" -- cgit v1.2.3