From 13062a5b8659630e2f47b998a3fe8a22e3d3cb78 Mon Sep 17 00:00:00 2001 From: Max Regan Date: Tue, 22 Nov 2022 15:50:18 -0500 Subject: [PATCH] Refactor headers, distinguish public and private --- include/uclisp.h | 14 ++++++++++++++ src/builtins.c | 6 ++---- src/common.h | 33 +++++++++++++++++++++++++++++++++ src/evaluate.c | 4 +--- src/internal.h | 38 -------------------------------------- src/lisp.h | 3 +-- src/main.c | 9 +++++---- src/memory.c | 6 +++++- src/memory.h | 16 ++++++++++++++++ src/parse.c | 3 +-- src/parse.h | 9 +++++++++ src/scope.c | 3 +-- src/special.c | 5 ++--- src/types.h | 42 ++++++++++++++++++++++++++++++++++++++++++ src/utility.c | 10 +++++----- src/utility.h | 17 +---------------- test/test_e2e.c | 6 +++--- test/test_parse.c | 6 ++++-- test/test_scope.c | 8 ++++---- test/test_utility.c | 7 +++---- 20 files changed, 152 insertions(+), 93 deletions(-) create mode 100644 include/uclisp.h create mode 100644 src/common.h delete mode 100644 src/internal.h create mode 100644 src/memory.h create mode 100644 src/parse.h create mode 100644 src/types.h diff --git a/include/uclisp.h b/include/uclisp.h new file mode 100644 index 0000000..7fe5aba --- /dev/null +++ b/include/uclisp.h @@ -0,0 +1,14 @@ +#ifndef _UCLISP_H_ +#define _UCLISP_H_ + +struct ucl_scope; +struct ucl_object; + +struct ucl_object *ucl_tokenize(const char *source); +struct ucl_object *ucl_parse(const char *sexp); +struct ucl_object *ucl_evaluate(struct ucl_scope *state, struct ucl_object *sexp); + +// TODO: State encapsulation is all wonky here) +void ucl_gc(); + +#endif diff --git a/src/builtins.c b/src/builtins.c index 0fa8774..c4d2bc6 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -1,9 +1,7 @@ #include "uclisp.h" -#include "internal.h" -#include "utility.h" -#include "builtins.h" -#include "scope.h" #include "lisp.h" +#include "builtins.h" +#include "common.h" #include #include diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..0e4fc5c --- /dev/null +++ b/src/common.h @@ -0,0 +1,33 @@ +#ifndef _UCLISP_COMMON_H_ +#define _UCLISP_COMMON_H_ + +#include + +#include "types.h" +#include "memory.h" +#include "utility.h" + +#define ARRAY_SIZE(x) sizeof(x) / sizeof(x[0]) +#define FOREACH_LIST(list, iter_name, item_name) \ + FOREACH_LIST_COND(list, iter_name, item_name, true) + +#define FOREACH_LIST_COND(list, iter_name, item_name, cond) \ + for (struct ucl_object *iter_name = (list), *item_name = iter_name->cell.car; \ + item_name != NULL && iter_name != NULL && (cond); \ + iter_name = iter_name->cell.cdr, item_name = (iter_name == NULL) ? NULL : iter_name->cell.car) + +#define UCL_RET_IF_ERROR(obj) \ + do { \ + if ((obj)->type == UCL_TYPE_ERROR) { \ + return obj; \ + } \ + } while(0) + +#define UCL_COND_OR_RET_ERROR(cond, msg) \ + do { \ + if (!(cond)) { \ + return ucl_error_create(msg); \ + } \ + } while(0) + +#endif diff --git a/src/evaluate.c b/src/evaluate.c index d97376c..b40ce97 100644 --- a/src/evaluate.c +++ b/src/evaluate.c @@ -2,11 +2,9 @@ #include #include "uclisp.h" -#include "internal.h" -#include "utility.h" +#include "common.h" #include "scope.h" -// TODO: remove string.h #include struct ucl_object *ucl_evaluate_builtin_form(struct ucl_scope *scope, struct ucl_object *list) { diff --git a/src/internal.h b/src/internal.h deleted file mode 100644 index 4f70f83..0000000 --- a/src/internal.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef _UCLISP_INTERNAL_H_ -#define _UCLISP_INTERNAL_H_ - -#include "uclisp.h" -#include - -#define ARRAY_SIZE(x) sizeof(x) / sizeof(x[0]) -#define FOREACH_LIST(list, iter_name, item_name) \ - FOREACH_LIST_COND(list, iter_name, item_name, true) - -#define FOREACH_LIST_COND(list, iter_name, item_name, cond) \ - for (struct ucl_object *iter_name = (list), *item_name = iter_name->cell.car; \ - item_name != NULL && iter_name != NULL && (cond); \ - iter_name = iter_name->cell.cdr, item_name = (iter_name == NULL) ? NULL : iter_name->cell.car) - -// TODO: Refactor this struct's location -struct ucl_scope { - struct ucl_object *list; - struct ucl_scope *parent; -}; - -extern struct ucl_arena *scope_arena; - -struct ucl_object *ucl_cell_create(struct ucl_object *car, struct ucl_object *cdr); -struct ucl_object *ucl_int_create(int integer); -struct ucl_object *ucl_symbol_create(const char* symbol); -struct ucl_object *ucl_string_create(const char* string); -struct ucl_object *ucl_error_create(const char* error); -struct ucl_object *ucl_builtin_create(ucl_lisp builtin); -struct ucl_object *ucl_special_create(ucl_lisp special); - -void ucl_object_delete(struct ucl_object *obj); - -// For testing -struct ucl_object *ucl_token_next(const char **curr_src); -struct ucl_object *ucl_parse_token_atom(struct ucl_object *maybe_atom); - -#endif diff --git a/src/lisp.h b/src/lisp.h index f2a279a..7e421b3 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -1,8 +1,7 @@ #ifndef _UCLISP_LISP_H_ #define _UCLISP_LISP_H_ -#include "uclisp.h" -#include "utility.h" +#include "common.h" #define LISP_FUNC_0(func_name, scope_name) \ static struct ucl_object *func_name##_impl(); \ diff --git a/src/main.c b/src/main.c index 8c7f271..c6afd4c 100644 --- a/src/main.c +++ b/src/main.c @@ -8,11 +8,12 @@ #include "uclisp.h" -#include "builtins.h" -#include "internal.h" -#include "special.h" +// TODO: Make anything needed in this file public +#include "common.h" #include "scope.h" -#include "utility.h" +#include "special.h" +#include "builtins.h" +#include "memory.h" int main(int argc, const char **argv) { (void) argc, (void) argv; diff --git a/src/memory.c b/src/memory.c index 3f3ffd9..9a7fdee 100644 --- a/src/memory.c +++ b/src/memory.c @@ -1,7 +1,10 @@ #include "uclisp.h" -#include "internal.h" +#include "common.h" #include "arena.h" +// TODO: Fix weird structuring of arena locations +#include "scope.h" + #include #include #include @@ -20,6 +23,7 @@ static struct ucl_object *ucl_object_alloc(); static struct ucl_arena *object_arena = NULL; +extern struct ucl_arena *scope_arena; struct ucl_object *ucl_cell_create(struct ucl_object *car, struct ucl_object *cdr) { diff --git a/src/memory.h b/src/memory.h new file mode 100644 index 0000000..caf55bf --- /dev/null +++ b/src/memory.h @@ -0,0 +1,16 @@ +#ifndef _UCLISP_MEMORY_H_ +#define _UCLISP_MEMORY_H_ + +#include "types.h" + +struct ucl_object *ucl_cell_create(struct ucl_object *car, struct ucl_object *cdr); +struct ucl_object *ucl_int_create(int integer); +struct ucl_object *ucl_symbol_create(const char* symbol); +struct ucl_object *ucl_string_create(const char* string); +struct ucl_object *ucl_error_create(const char* error); +struct ucl_object *ucl_builtin_create(ucl_lisp builtin); +struct ucl_object *ucl_special_create(ucl_lisp special); + +void ucl_object_delete(struct ucl_object *obj); + +#endif diff --git a/src/parse.c b/src/parse.c index 943ddb5..fa3f527 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1,6 +1,5 @@ #include "uclisp.h" -#include "internal.h" -#include "utility.h" +#include "common.h" // TODO: remove these #include diff --git a/src/parse.h b/src/parse.h new file mode 100644 index 0000000..c8b9aab --- /dev/null +++ b/src/parse.h @@ -0,0 +1,9 @@ +#ifndef _UCLISP_PARSE_H_ +#define _UCLISP_PARSE_H_ + +#include "types.h" + +struct ucl_object *ucl_token_next(const char **curr_src); +struct ucl_object *ucl_parse_token_atom(struct ucl_object *maybe_atom); + +#endif diff --git a/src/scope.c b/src/scope.c index fb6b7dc..1247cfb 100644 --- a/src/scope.c +++ b/src/scope.c @@ -1,8 +1,7 @@ #include "uclisp.h" -#include "internal.h" #include "scope.h" -#include "utility.h" #include "arena.h" +#include "common.h" #include #include diff --git a/src/special.c b/src/special.c index 1e80a5e..65e4c0a 100644 --- a/src/special.c +++ b/src/special.c @@ -1,8 +1,7 @@ +#include "uclisp.h" +#include "common.h" #include "scope.h" #include "lisp.h" -#include "uclisp.h" -#include "utility.h" -#include "internal.h" #include diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..25cb670 --- /dev/null +++ b/src/types.h @@ -0,0 +1,42 @@ +#ifndef _UCLISP_TYPES_H_ +#define _UCLISP_TYPES_H_ + +enum ucl_type { + UCL_TYPE_CELL = 0, + UCL_TYPE_SYMBOL = 1, + UCL_TYPE_INT = 2, + UCL_TYPE_STRING = 3, + UCL_TYPE_ERROR = 4, + UCL_TYPE_BUILTIN = 5, + UCL_TYPE_SPECIAL = 6, // Like builtins, but special forms where arguments are not necessarily evaluated + UCL_TYPE_COUNT = 7, +}; + +struct ucl_cell { + struct ucl_object *car; + struct ucl_object *cdr; +}; + +struct ucl_scope; +typedef struct ucl_object *(*ucl_lisp)(struct ucl_scope* state, struct ucl_object *args); + +struct ucl_object { + union { + struct ucl_cell cell; + const char *symbol; + int integer; + const char *string; + const char *error; + ucl_lisp builtin; + ucl_lisp special; + }; + enum ucl_type type; + char reachable; +}; + +struct ucl_scope { + struct ucl_object *list; + struct ucl_scope *parent; +}; + +#endif diff --git a/src/utility.c b/src/utility.c index 4265089..728ef03 100644 --- a/src/utility.c +++ b/src/utility.c @@ -1,14 +1,14 @@ -#include "internal.h" -#include "lisp.h" -#include "uclisp.h" -#include "utility.h" - #include #include #include #include #include +#include "uclisp.h" +#include "lisp.h" +#include "common.h" + + struct ucl_object *ucl_car(struct ucl_object *list) { UCL_COND_OR_RET_ERROR( list != NULL && list->type == UCL_TYPE_CELL, diff --git a/src/utility.h b/src/utility.h index 89f1c4d..b59ebdd 100644 --- a/src/utility.h +++ b/src/utility.h @@ -2,8 +2,7 @@ #define _UCLISP_UTILITY_H_ #include - -#include "uclisp.h" +#include "types.h" struct ucl_object *ucl_nil_create(); struct ucl_object *ucl_t_create(); @@ -24,18 +23,4 @@ struct ucl_object *ucl_progn(struct ucl_scope *scope, struct ucl_object *forms); void ucl_print_obj(struct ucl_object *obj); -#define UCL_RET_IF_ERROR(obj) \ - do { \ - if ((obj)->type == UCL_TYPE_ERROR) { \ - return obj; \ - } \ - } while(0) - -#define UCL_COND_OR_RET_ERROR(cond, msg) \ - do { \ - if (!(cond)) { \ - return ucl_error_create(msg); \ - } \ - } while(0) - #endif diff --git a/test/test_e2e.c b/test/test_e2e.c index 623ea5a..82858fa 100644 --- a/test/test_e2e.c +++ b/test/test_e2e.c @@ -2,10 +2,10 @@ #include #include -#include "uclisp.h" -#include "internal.h" -#include "utility.h" #include "testing_helpers.h" + +#include "uclisp.h" +#include "common.h" #include "scope.h" #include "builtins.h" #include "special.h" diff --git a/test/test_parse.c b/test/test_parse.c index fc89a4a..66d078c 100644 --- a/test/test_parse.c +++ b/test/test_parse.c @@ -2,10 +2,12 @@ #include #include -#include "uclisp.h" -#include "internal.h" #include "testing_helpers.h" +#include "uclisp.h" +#include "common.h" +#include "parse.h" + /* static struct ucl_parse_result *result; */ static struct ucl_object *response; diff --git a/test/test_scope.c b/test/test_scope.c index a329f8b..bb1b078 100644 --- a/test/test_scope.c +++ b/test/test_scope.c @@ -2,12 +2,12 @@ #include #include -#include "uclisp.h" -#include "internal.h" -#include "scope.h" -#include "utility.h" #include "testing_helpers.h" +#include "uclisp.h" +#include "common.h" +#include "scope.h" + /* static struct ucl_parse_result *result; */ static struct ucl_scope *scope; static struct ucl_object *response; diff --git a/test/test_utility.c b/test/test_utility.c index aa9ad18..e44f241 100644 --- a/test/test_utility.c +++ b/test/test_utility.c @@ -2,12 +2,11 @@ #include #include -#include "uclisp.h" -#include "internal.h" -#include "utility.h" -#include "scope.h" #include "testing_helpers.h" +#include "uclisp.h" +#include "common.h" + static struct ucl_object *input; static struct ucl_object *response;