Refactor headers, distinguish public and private

This commit is contained in:
2022-11-22 15:50:18 -05:00
parent f19f672d05
commit 13062a5b86
20 changed files with 152 additions and 93 deletions

14
include/uclisp.h Normal file
View File

@@ -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

View File

@@ -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 <assert.h>
#include <stddef.h>

33
src/common.h Normal file
View File

@@ -0,0 +1,33 @@
#ifndef _UCLISP_COMMON_H_
#define _UCLISP_COMMON_H_
#include <stddef.h>
#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

View File

@@ -2,11 +2,9 @@
#include <stddef.h>
#include "uclisp.h"
#include "internal.h"
#include "utility.h"
#include "common.h"
#include "scope.h"
// TODO: remove string.h
#include <string.h>
struct ucl_object *ucl_evaluate_builtin_form(struct ucl_scope *scope, struct ucl_object *list) {

View File

@@ -1,38 +0,0 @@
#ifndef _UCLISP_INTERNAL_H_
#define _UCLISP_INTERNAL_H_
#include "uclisp.h"
#include <stddef.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)
// 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

View File

@@ -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(); \

View File

@@ -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;

View File

@@ -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 <assert.h>
#include <stddef.h>
#include <stdlib.h>
@@ -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)
{

16
src/memory.h Normal file
View File

@@ -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

View File

@@ -1,6 +1,5 @@
#include "uclisp.h"
#include "internal.h"
#include "utility.h"
#include "common.h"
// TODO: remove these
#include <stdbool.h>

9
src/parse.h Normal file
View File

@@ -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

View File

@@ -1,8 +1,7 @@
#include "uclisp.h"
#include "internal.h"
#include "scope.h"
#include "utility.h"
#include "arena.h"
#include "common.h"
#include <assert.h>
#include <string.h>

View File

@@ -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 <assert.h>

42
src/types.h Normal file
View File

@@ -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

View File

@@ -1,14 +1,14 @@
#include "internal.h"
#include "lisp.h"
#include "uclisp.h"
#include "utility.h"
#include <stddef.h>
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#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,

View File

@@ -2,8 +2,7 @@
#define _UCLISP_UTILITY_H_
#include <stdbool.h>
#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

View File

@@ -2,10 +2,10 @@
#include <unity.h>
#include <string.h>
#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"

View File

@@ -2,10 +2,12 @@
#include <unity.h>
#include <string.h>
#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;

View File

@@ -2,12 +2,12 @@
#include <stdlib.h>
#include <string.h>
#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;

View File

@@ -2,12 +2,11 @@
#include <unity.h>
#include <string.h>
#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;