Commit d3e0542b authored by Chris Müller's avatar Chris Müller
Browse files

name refactorings and code movements.

parent f71c6693
......@@ -21,7 +21,7 @@
#include <string.h>
#include <gc.h>
struct cherry_array
struct array
{
uint8_t* data;
size_t alloc;
......@@ -31,12 +31,12 @@ struct cherry_array
struct cherry_array*
cherry_array_new(size_t init_capacity)
struct array*
array_new(size_t init_capacity)
{
assert(init_capacity != 0);
struct cherry_array* array = GC_MALLOC(sizeof(struct cherry_array));
struct array* array = GC_MALLOC(sizeof(struct array));
array->alloc = init_capacity;
array->length = 0;
array->data = GC_MALLOC(sizeof(uint8_t) * array->alloc);
......@@ -47,7 +47,7 @@ cherry_array_new(size_t init_capacity)
size_t
cherry_array_size(struct cherry_array* array)
array_size(struct array* array)
{
assert(array != 0);
......@@ -57,7 +57,7 @@ cherry_array_size(struct cherry_array* array)
size_t
cherry_array_capacity(struct cherry_array* array)
array_capacity(struct array* array)
{
assert(array != 0);
......@@ -66,7 +66,7 @@ cherry_array_capacity(struct cherry_array* array)
size_t
cherry_array_ensure(struct cherry_array* array, size_t min_capacity)
array_ensure(struct array* array, size_t min_capacity)
{
assert(array != 0);
......@@ -81,7 +81,7 @@ cherry_array_ensure(struct cherry_array* array, size_t min_capacity)
size_t
cherry_array_trim(struct cherry_array* array)
array_trim(struct array* array)
{
assert(array != 0);
......@@ -93,11 +93,11 @@ cherry_array_trim(struct cherry_array* array)
void
cherry_array_append(struct cherry_array* array, const_pointer_t data, size_t size)
array_append(struct array* array, const_pointer_t data, size_t size)
{
assert(array != 0);
cherry_array_ensure(array, array->length + size);
array_ensure(array, array->length + size);
memcpy(array->data + array->length, data, size);
......@@ -106,14 +106,14 @@ cherry_array_append(struct cherry_array* array, const_pointer_t data, size_t siz
int
cherry_array_insert(struct cherry_array* array, size_t index, const_pointer_t data, size_t size)
array_insert(struct array* array, size_t index, const_pointer_t data, size_t size)
{
assert(array != 0);
if(index >= array->length)
return FALSE;
cherry_array_ensure(array, array->length + size);
array_ensure(array, array->length + size);
memmove(array->data + index + size, array->data + index, array->length - index);
memcpy(array->data + index, data, size);
......@@ -125,20 +125,20 @@ cherry_array_insert(struct cherry_array* array, size_t index, const_pointer_t da
int
cherry_array_replace(struct cherry_array* array, size_t index, const_pointer_t data, size_t size)
array_replace(struct array* array, size_t index, const_pointer_t data, size_t size)
{
assert(array != 0);
if(index >= array->length && index + size > array->length)
return FALSE;
cherry_array_ensure(array, array->length + size);
array_ensure(array, array->length + size);
memcpy(array->data + index, data, size);
}
int
cherry_array_remove(struct cherry_array* array, size_t index, size_t size)
array_remove(struct array* array, size_t index, size_t size)
{
assert(array != 0);
......@@ -155,7 +155,7 @@ cherry_array_remove(struct cherry_array* array, size_t index, size_t size)
void
cherry_array_clear(struct cherry_array* array)
array_clear(struct array* array)
{
assert(array != 0);
......@@ -165,7 +165,7 @@ cherry_array_clear(struct cherry_array* array)
pointer_t
cherry_array_get(struct cherry_array* array, size_t index)
array_get(struct array* array, size_t index)
{
assert(array != 0);
......@@ -177,7 +177,7 @@ cherry_array_get(struct cherry_array* array, size_t index)
pointer_t
cherry_array_clone(struct cherry_array* array)
array_clone(struct array* array)
{
assert(array != 0);
......@@ -192,7 +192,7 @@ cherry_array_clone(struct cherry_array* array)
}
struct cherry_ptrarray
struct ptrarray
{
pointer_t* data;
size_t alloc;
......@@ -200,12 +200,12 @@ struct cherry_ptrarray
};
struct cherry_ptrarray*
cherry_ptrarray_new(size_t init_capacity)
struct ptrarray*
ptrarray_new(size_t init_capacity)
{
assert(init_capacity != 0);
struct cherry_ptrarray* array = GC_MALLOC(sizeof(struct cherry_ptrarray));
struct ptrarray* array = GC_MALLOC(sizeof(struct ptrarray));
array->alloc = init_capacity;
array->length = 0;
array->data = GC_MALLOC(sizeof(pointer_t) * array->alloc);
......@@ -216,7 +216,7 @@ cherry_ptrarray_new(size_t init_capacity)
size_t
cherry_ptrarray_size(struct cherry_ptrarray* array)
ptrarray_size(struct ptrarray* array)
{
assert(array != 0);
......@@ -225,7 +225,7 @@ cherry_ptrarray_size(struct cherry_ptrarray* array)
size_t
cherry_ptrarray_capacity(struct cherry_ptrarray* array)
ptrarray_capacity(struct ptrarray* array)
{
assert(array != 0);
......@@ -234,7 +234,7 @@ cherry_ptrarray_capacity(struct cherry_ptrarray* array)
size_t
cherry_ptrarray_ensure(struct cherry_ptrarray* array, size_t min_capacity)
ptrarray_ensure(struct ptrarray* array, size_t min_capacity)
{
assert(array != 0);
......@@ -249,7 +249,7 @@ cherry_ptrarray_ensure(struct cherry_ptrarray* array, size_t min_capacity)
size_t
cherry_ptrarray_trim(struct cherry_ptrarray* array)
ptrarray_trim(struct ptrarray* array)
{
assert(array != 0);
......@@ -261,11 +261,11 @@ cherry_ptrarray_trim(struct cherry_ptrarray* array)
void
cherry_ptrarray_append(struct cherry_ptrarray* array, pointer_t ptr)
ptrarray_append(struct ptrarray* array, pointer_t ptr)
{
assert(array != 0);
cherry_ptrarray_ensure(array, array->length + 1);
ptrarray_ensure(array, array->length + 1);
*(array->data + array->length) = ptr;
......@@ -274,14 +274,14 @@ cherry_ptrarray_append(struct cherry_ptrarray* array, pointer_t ptr)
int
cherry_ptrarray_insert(struct cherry_ptrarray* array, size_t index, pointer_t ptr)
ptrarray_insert(struct ptrarray* array, size_t index, pointer_t ptr)
{
assert(array != 0);
if(index >= array->length)
return FALSE;
cherry_ptrarray_ensure(array, array->length + 1);
ptrarray_ensure(array, array->length + 1);
memmove(array->data + index + 1, array->data + index, (array->length - index) * sizeof(pointer_t));
......@@ -294,7 +294,7 @@ cherry_ptrarray_insert(struct cherry_ptrarray* array, size_t index, pointer_t pt
pointer_t
cherry_ptrarray_replace(struct cherry_ptrarray* array, size_t index, pointer_t ptr)
ptrarray_replace(struct ptrarray* array, size_t index, pointer_t ptr)
{
assert(array != 0);
......@@ -309,7 +309,7 @@ cherry_ptrarray_replace(struct cherry_ptrarray* array, size_t index, pointer_t p
}
pointer_t
cherry_ptrarray_remove(struct cherry_ptrarray* array, size_t index)
ptrarray_remove(struct ptrarray* array, size_t index)
{
assert(array != 0);
......@@ -327,7 +327,7 @@ cherry_ptrarray_remove(struct cherry_ptrarray* array, size_t index)
pointer_t
cherry_ptrarray_get(struct cherry_ptrarray* array, size_t index)
ptrarray_get(struct ptrarray* array, size_t index)
{
assert(array != 0);
......@@ -338,7 +338,7 @@ cherry_ptrarray_get(struct cherry_ptrarray* array, size_t index)
}
void
cherry_ptrarray_clear(struct cherry_ptrarray* array)
ptrarray_clear(struct ptrarray* array)
{
assert(array != 0);
array->length = 0;
......
......@@ -53,77 +53,57 @@ get_text(const char* filename, const char* mode) {
static void
cherry_process_file(const char* filename, const byte_t* method, struct cherry_value* arguments)
process_file(const char* filename, const byte_t* method, struct value* arguments)
{
const byte_t* src = get_text(filename, "rb");
struct environment* env = environment();
if(src == 0) {
fprintf(stderr, "cherry: couldn't load %s\n", filename);
exit(EXIT_FAILURE);
}
struct cherry_environment* env = cherry_environment();
struct cherry_context* context = cherry_context(src, filename, SUPRESS_COMMENTS);
struct cherry_value* exp = cherry_read(context);
struct context* c = context(src, filename, SUPRESS_COMMENTS);
cherry_env_push_exception_point(env);
struct value* exp = cherry_read(c);
if(!setjmp(EXCEPTION_JUMP(env))) {
while(exp != 0) {
cherry_eval(env, exp);
exp = cherry_read(context);
}
while(exp != 0) {
cherry_eval(env, exp);
exp = cherry_read(c);
}
if(method) {
struct cherry_value* main = cherry_list_cons(cherry_symbol(method), arguments);
if(method) {
struct value* main = cons(symbol(method), arguments);
cherry_eval(env, main);
}
} else {
fprintf(stderr, "EXCEPTION: ");
cherry_print(stderr, HEAD(EXCEPTION_DATA(env)));
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
cherry_eval(env, main);
}
cherry_env_pop_exception_point(env);
exit(EXIT_SUCCESS);
}
static void
cherry_start_repl(void)
start_repl(void)
{
printf("Welcome to bootstrap-cherry\n\n");
struct cherry_environment* env = cherry_environment();
struct environment* env = environment();
cherry_env_push_exception_point(env);
byte_t* line = (byte_t*) readline("> ");
struct cherry_context* context = cherry_context_repl(line);
struct context* context = context_repl(line);
while(1) {
struct cherry_value* exp = cherry_read(context);
if(!setjmp(EXCEPTION_JUMP(env))) {
cherry_print(stdout, cherry_eval(env, exp));
printf("\n");
} else {
fprintf(stderr, "EXCEPTION: ");
cherry_print(stderr, EXCEPTION_DATA(env));
fprintf(stderr, "\n");
}
struct value* exp = cherry_read(context);
free(line);
cherry_print(stdout, cherry_eval(env, exp));
printf("\n");
free(line);
line = (byte_t*) readline("> ");
cherry_context_repl_set_source(context, line);
context_repl_set_source(context, line);
}
cherry_env_pop_exception_point(env);
}
......@@ -136,13 +116,13 @@ main(int argc, char** argv)
{"main", optional_argument, 0, 'M'}
};
cherry_initialize(0);
initialize(0);
int ch;
const char* filename = 0;
const byte_t* method = 0;
struct cherry_ptrarray* load_path = cherry_ptrarray_new(4);
struct cherry_value* arguments = cherry_emptylist;
struct ptrarray* load_path = ptrarray_new(4);
struct value* arguments = emptylist;
while( (ch = getopt_long(argc, argv, "hI:M:", options, 0)) != -1) {
switch(ch) {
......@@ -150,7 +130,7 @@ main(int argc, char** argv)
print_usage(stdout, argv[0]);
exit(EXIT_SUCCESS);
case 'I':
cherry_ptrarray_append(load_path, optarg);
ptrarray_append(load_path, optarg);
break;
case 'M':
method = optarg;
......@@ -168,13 +148,13 @@ main(int argc, char** argv)
filename = argv[optind++];
while(optind < argc) {
arguments = cherry_list_cons(cherry_string(argv[optind++]), arguments);
arguments = cons(string(argv[optind++]), arguments);
}
if(filename)
cherry_process_file(filename, method, cherry_list_reverse(arguments));
process_file(filename, method, list_reverse(arguments));
else
cherry_start_repl();
start_repl();
return EXIT_SUCCESS;
}
......@@ -23,8 +23,9 @@
#include <setjmp.h>
#include <string.h>
struct cherry_environment;
struct cherry_value;
struct environment;
struct context;
struct value;
#define TRUE 1
#define FALSE 0
......@@ -40,17 +41,17 @@ typedef uint32_t unicode_t;
typedef uint32_t flags_t;
typedef struct cherry_value* (*primitive_t)(struct cherry_environment* env, struct cherry_value* args);
typedef struct value* (*primitive_t)(struct environment* env, struct value* args);
enum cherry_value_type {
enum value_type {
EMPTYLIST, BOOLEAN, DOT,
FIXNUM, FLOAT, STRING, CHAR, PAIR,
TUPLE, SYMBOL, PRIMITIVE, PROCEDURE
};
struct cherry_value {
enum cherry_value_type tag;
struct value {
enum value_type tag;
union {
// trivial
......@@ -64,21 +65,21 @@ struct cherry_value {
// lists
struct {
struct cherry_value* head;
struct cherry_value* tail;
struct value* head;
struct value* tail;
} pair;
// tuples
struct {
struct cherry_value** data;
struct value** data;
size_t size;
} tuple;
// procedures
struct {
struct cherry_value* param;
struct cherry_value* body;
struct cherry_environment* env;
struct value* param;
struct value* body;
struct environment* env;
} proc;
};
};
......@@ -97,9 +98,6 @@ struct cherry_value {
#define IS_PRIMITIVE(value) (value->tag == PRIMITIVE)
#define IS_PROCEDURE(value) (value->tag == PROCEDURE)
#define HEAD(obj) (obj->pair.head)
#define TAIL(obj) (obj->pair.tail)
#define TUPLE_DATA(obj) (obj->tuple.data)
#define TUPLE_SIZE(obj) (obj->tuple.size)
......@@ -107,91 +105,71 @@ struct cherry_value {
#define PROC_BODY(obj) (obj->proc.body)
#define PROC_ENV(obj) (obj->proc.env)
#define cherry_string_size(STR) (strlen(STR) + 1)
#define string_size(STR) (strlen(STR) + 1)
const byte_t* string_dup(const byte_t* str);
const byte_t* cherry_string_dup(const byte_t* str);
struct value* char_from_string(const byte_t* str);
struct value* string_from_string(const byte_t* str);
struct cherry_value* cherry_fixnum_from_string(const byte_t* str, int base);
struct cherry_value* cherry_float_from_string(const byte_t* str);
struct cherry_value* cherry_char_from_string(const byte_t* str);
struct cherry_value* cherry_symbol_from_string(const byte_t* str);
struct cherry_value* cherry_string_from_string(const byte_t* str);
struct value* value_alloc(void);
struct value* symbol(const byte_t* symbol_value);
struct value* fixnum(fixnum_t value);
struct value* floatpoint(float_t float_value);
struct value* string(const byte_t* string_value);
struct value* character(unicode_t char_value);
struct value* primitive(primitive_t fun_value);
struct cherry_value* cherry_value_alloc(void);
struct cherry_value* cherry_symbol(const byte_t* symbol_value);
struct cherry_value* cherry_fixnum(fixnum_t value);
struct cherry_value* cherry_float(float_t float_value);
struct cherry_value* cherry_string(const byte_t* string_value);
struct cherry_value* cherry_char(unicode_t char_value);
struct cherry_value* cherry_primitive(primitive_t fun_value);
struct value* value_dup(struct value* value);
struct cherry_value* cherry_value_dup(struct cherry_value* value);
#define HEAD(obj) (obj->pair.head)
#define TAIL(obj) (obj->pair.tail)
struct cherry_value* cherry_list(struct cherry_value* val, ...);
struct cherry_value* cherry_list_cons(struct cherry_value* head, struct cherry_value* tail);
struct cherry_value* cherry_list_reverse(struct cherry_value* value);
#define list2(a, b) cons(a, cons(b, emptylist))
#define list3(a, b, c) cons(a, cons(b, cons(c, emptylist)))
#define list4(a, b, c, d) cons(a, cons(b, cons(c, cons(d, emptylist))))
struct cherry_value* cherry_tuple_new(size_t size);
struct value* cons(struct value* head, struct value* tail);
struct value* list_reverse(struct value* value);
struct cherry_value* cherry_procedure(struct cherry_environment* env, struct cherry_value* param, struct cherry_value* body);
struct value* tuple_new(size_t size);
struct value* procedure(struct environment* env, struct value* param, struct value* body);
// ----------------------------------------------------------------------------
// Symboltables and Environment
// ----------------------------------------------------------------------------
struct cherry_symbollist;
struct cherry_exception;
struct cherry_symbollist* cherry_symbollist(void);
struct cherry_value* cherry_symbollist_get(struct cherry_symbollist* table, const byte_t* name);
struct symbollist;
#define EXCEPTION_JUMP(env) env->exception_stack->jump
#define EXCEPTION_DATA(env) env->exception_stack->data
struct symbollist* symbollist(void);
struct value* symbollist_get(struct symbollist* table, const byte_t* name);
struct cherry_exception {
jmp_buf jump;
struct cherry_value* data;
struct cherry_exception* next;
struct environment {
struct symbollist* mapping;
};
struct environment* env_push(struct environment* env);
struct environment* env_pop(struct environment* env);
struct value* env_lookup(struct environment* env, struct value* symbol);
int env_add(struct environment* env, struct value* symbol, struct value* value);
struct cherry_environment {
struct cherry_exception* exception_stack;
struct cherry_symbollist* mapping;
};
struct cherry_environment* cherry_env_push(struct cherry_environment* env);
struct cherry_environment* cherry_env_pop(struct cherry_environment* env);
struct cherry_value* cherry_env_lookup(struct cherry_environment* env, struct cherry_value* symbol);
int cherry_env_add(struct cherry_environment* env, struct cherry_value* symbol, struct cherry_value* value);
struct cherry_environment* cherry_env_push_exception_point(struct cherry_environment* env);
struct cherry_environment* cherry_env_pop_exception_point(struct cherry_environment* env);
void cherry_env_raise(struct cherry_environment* env, struct cherry_value* e);
struct cherry_environment* cherry_environment(void);
struct environment* environment(void);
extern struct cherry_symbollist* cherry_global_symbollist;
extern struct cherry_value* cherry_emptylist;
extern struct cherry_value* cherry_true;
extern struct cherry_value* cherry_false;
extern struct cherry_value* cherry_dot;
extern struct cherry_value* cherry_symbol_quote;
extern struct cherry_value* cherry_symbol_define;
extern struct cherry_value* cherry_symbol_let;
extern struct cherry_value* cherry_symbol_lambda;
extern struct cherry_value* cherry_symbol_if;
extern struct cherry_value* cherry_symbol_loop;
extern struct cherry_value* cherry_symbol_begin;
extern struct cherry_value* cherry_symbol_try;
extern struct cherry_value* cherry_symbol_catch;
extern struct symbollist* global_symbollist;
extern struct value* emptylist;
extern struct value* true;
extern struct value* false;
extern struct value* dot;
extern struct value* symbol_quote;
extern struct value* symbol_define;
extern struct value* symbol_let;
extern struct value* symbol_lambda;
extern struct value* symbol_if;
extern struct value* symbol_loop;
extern struct value* symbol_begin;
// ----------------------------------------------------------------------------
// Evaluation
......@@ -213,31 +191,25 @@ extern struct cherry_value* cherry_symbol_catch;
(IS_PAIR(value) && IS_SYMBOL(HEAD(value)) && HEAD(value) == symbol)
#define IS_QUOTE(value) \
IS_TAGGED(value, cherry_symbol_quote)
IS_TAGGED(value, symbol_quote)
#define IS_DEFINE(value) \
IS_TAGGED(value, cherry_symbol_define)
IS_TAGGED(value, symbol_define)
#define IS_LET(value) \
IS_TAGGED(value, cherry_symbol_let)
IS_TAGGED(value, symbol_let)
#define IS_IF(value) \
IS_TAGGED(value, cherry_symbol_if)
IS_TAGGED(value, symbol_if)
#define IS_LAMBDA(value) \
IS_TAGGED(value, cherry_symbol_lambda)
IS_TAGGED(value, symbol_lambda)
#define IS_LOOP(value) \
IS_TAGGED(value, cherry_symbol_loop)
IS_TAGGED(value, symbol_loop)