~brenns10/funlisp

f6533abff55d8c3214859f83901f8968a4451db0 — Stephen Brennan 3 years ago 398b3f3
Add more warnings, mostly C89
11 files changed, 42 insertions(+), 34 deletions(-)

M Makefile
M src/charbuf.c
M src/hashtable.c
M src/hashtable.h
M src/iter.c
M src/iter.h
M src/lisp.h
M src/main.c
M src/ringbuf.c
M src/types.c
M src/util.c
M Makefile => Makefile +1 -3
@@ 11,9 11,7 @@ FLAGS += -g -DDEBUG
endif

CC=gcc
#FLAGS=-Wall -Wextra -pedantic
FLAGS=
CFLAGS=$(FLAGS) -std=c99 -fPIC -Iinc -c
CFLAGS= -std=c89 -Wall -Wextra -fPIC -Iinc -c
SRCS=$(wildcard src/*.c)
OBJS=$(patsubst src/%.c,obj/%.o,$(SRCS))


M src/charbuf.c => src/charbuf.c +7 -0
@@ 92,6 92,11 @@ void cb_clear(struct charbuf *obj)
	obj->length = 0;
}

/*
 * These functions use C99 va_copy and vsnprintf, remove them for compatibility.
 */
#ifdef CHARBUF_PRINTF

void cb_vprintf(struct charbuf *obj, char *format, va_list va)
{
	va_list v2;


@@ 116,3 121,5 @@ void cb_printf(struct charbuf *obj, char *format, ...)
	cb_vprintf(obj, format, va);
	va_end(va);  /* Have to va_stop() it when you're done using it. */
}

#endif

M src/hashtable.c => src/hashtable.c +5 -5
@@ 32,7 32,7 @@
 */

unsigned int ht_primes[] = {
	31, // 2^5
	31, /* 2^5 */
	61,
	127,
	257,


@@ 59,7 59,7 @@ unsigned int ht_primes[] = {
	536870909,
	1073741827,
	2147483647,
	4294967291 // 2^32
	4294967291 /* 2^32 */
};

int binary_search(unsigned int *array, int len, unsigned int value)


@@ 302,7 302,7 @@ int ht_remove(struct hashtable *table, void *key)

int ht_remove_ptr(struct hashtable *table, void *key)
{
	ht_remove(table, &key);
	return ht_remove(table, &key);
}

void *ht_get(struct hashtable const *table, void *key)


@@ 391,7 391,7 @@ static void *ht_next(struct iterator *iter)
	unsigned int bufidx;
	int8_t mark = HT_EMPTY;

	for (; mark != HT_FULL && iter->state_int < table->allocated; iter->state_int++) {
	for (; mark != HT_FULL && iter->state_int < (int)table->allocated; iter->state_int++) {
		bufidx = convert_idx(table, iter->state_int);
		mark = HTA_MARK(table, bufidx);
	}


@@ 417,7 417,7 @@ static void *ht_next_ptr(struct iterator *iter)
static bool ht_has_next(struct iterator *iter)
{
	struct hashtable *table = iter->ds;
	return iter->index < table->length;
	return iter->index < (int)table->length;
}

struct iterator ht_iter_keys(struct hashtable *table)

M src/hashtable.h => src/hashtable.h +3 -3
@@ 7,8 7,8 @@

#include "iter.h"

#ifndef LIBSTEPHEN_HTA_H
#define LIBSTEPHEN_HTA_H
#ifndef HASHTABLE_H
#define HASHTABLE_H

typedef unsigned int (*hash_t)(void *to_hash);



@@ 120,4 120,4 @@ struct iterator ht_iter_keys_ptr(struct hashtable *table);
struct iterator ht_iter_values(struct hashtable *table);
struct iterator ht_iter_values_ptr(struct hashtable *table);

#endif // LIBSTEPHEN_HTA_H
#endif

M src/iter.c => src/iter.c +4 -1
@@ 10,7 10,10 @@

#include "iter.h"

void iterator_close_noop(struct iterator *iter) {}
void iterator_close_noop(struct iterator *iter)
{
	(void)iter;
}

static bool sv_has_next(struct iterator *iter)
{

M src/iter.h => src/iter.h +1 -1
@@ 10,7 10,7 @@

struct iterator {
	void *ds;        /* the container data structure */
	size_t index;    /* zero-based index for the iterator */
	int index;    /* zero-based index for the iterator */
	int state_int;   /* some state variables that may help */
	void *state_ptr;


M src/lisp.h => src/lisp.h +3 -5
@@ 19,11 19,9 @@
#define GC_MARKED 'b'

#define LISP_VALUE_HEAD             \
	struct {                          \
		struct lisp_type  *type;        \
		struct lisp_value *next;        \
		char mark;                      \
	}
	struct lisp_type  *type;        \
	struct lisp_value *next;        \
	char mark                      \


/* Type declarations. */

M src/main.c => src/main.c +2 -0
@@ 6,6 6,8 @@

int main(int argc, char **argv)
{
	(void)argc;
	(void)argv;
	lisp_runtime rt;
	lisp_init(&rt);
	lisp_scope *scope = (lisp_scope*)lisp_new(&rt, type_scope);

M src/ringbuf.c => src/ringbuf.c +2 -1
@@ 19,11 19,12 @@ void rb_destroy(struct ringbuf *rb)

void rb_grow(struct ringbuf *rb)
{
	int i;
	int oldalloc = rb->nalloc;
	rb->nalloc *= 2;
	rb->data = realloc(rb->data, rb->nalloc * rb->dsize);

	for (int i = 0; i < rb->count; i++) {
	for (i = 0; i < rb->count; i++) {
		int oldindex = (rb->start + i) % oldalloc;
		int newindex = (rb->start + i) % rb->nalloc;
		if (oldindex != newindex) {

M src/types.c => src/types.c +1 -2
@@ 282,7 282,6 @@ static void symbol_print(FILE *f, lisp_value *v);
static lisp_value *symbol_new(void);
static lisp_value *symbol_eval(lisp_runtime*, lisp_scope*, lisp_value*);
static void symbol_free(void *v);
static struct iterator symbol_expand(lisp_value*v);

static lisp_type type_symbol_obj = {
	.type=&type_type_obj,


@@ 397,7 396,7 @@ static lisp_value *integer_new(void)
	return (lisp_value*)integer;
}

// string
/* string */

static void string_print(FILE *f, lisp_value *v);
static lisp_value *string_new(void);

M src/util.c => src/util.c +13 -13
@@ 55,17 55,17 @@ void lisp_scope_replace_or_insert(lisp_scope *scope, lisp_symbol *key, lisp_valu
{
	lisp_scope *s = scope;

	// First go up the chain checking for the name.
	/* First go up the chain checking for the name.*/
	while (s) {
		if (ht_contains_ptr(&s->scope, key)) {
			// If we find it, replace it.
			/* If we find it, replace it. */
			ht_insert_ptr(&s->scope, key, value);
			return;
		}
		s = s->up;
	}

	// If we never find it, insert it in the "lowest" scope.
	/* If we never find it, insert it in the "lowest" scope. */
	ht_insert_ptr(&scope->scope, key, value);
}



@@ 213,7 213,7 @@ static lisp_value *lisp_builtin_cdr(lisp_runtime *rt, lisp_scope *scope,
	if (!lisp_get_args(arglist, "l", &firstarg)) {
		return (lisp_value*) lisp_error_new(rt, "wrong arguments to cdr");
	}
	// save rv because firstarg may be deleted after decref
	/* save rv because firstarg may be deleted after decref */
	return firstarg->right;
}



@@ 283,7 283,7 @@ static lisp_value *lisp_builtin_define(lisp_runtime *rt, lisp_scope *scope,

	lisp_value *evald = lisp_eval(rt, scope, expr);
	lisp_scope_replace_or_insert(scope, s, evald);
	//lisp_scope_bind(scope, s, evald);
	/*lisp_scope_bind(scope, s, evald); */
	return evald;
}



@@ 493,7 493,7 @@ static lisp_list *get_quoted_left_items(lisp_runtime *rt, lisp_list *list_of_lis
{
	lisp_list *left_items = NULL, *rv;
	while (!lisp_nil_p((lisp_value*)list_of_lists)) {
		// Create or advance left_items to the next list.
		/* Create or advance left_items to the next list. */
		if (left_items == NULL) {
			left_items = (lisp_list*) lisp_new(rt, type_list);
			rv = left_items;


@@ 501,11 501,11 @@ static lisp_list *get_quoted_left_items(lisp_runtime *rt, lisp_list *list_of_lis
			left_items->right = lisp_new(rt, type_list);
			left_items = (lisp_list*) left_items->right;
		}
		// Check the next node in the list to make sure it's actually a list.
		/* Check the next node in the list to make sure it's actually a list. */
		if (lisp_nil_p(list_of_lists->left)) {
			return NULL;
		}
		// Get the next node in the list and get the argument.
		/* Get the next node in the list and get the argument. */
		lisp_list *l = (lisp_list*) list_of_lists->left;
		left_items->left = lisp_quote(rt, l->left);
		list_of_lists = (lisp_list*) list_of_lists->right;


@@ 518,7 518,7 @@ static lisp_list *advance_lists(lisp_runtime *rt, lisp_list *list_of_lists)
{
	lisp_list *right_items = NULL, *rv;
	while (!lisp_nil_p((lisp_value*)list_of_lists)) {
		// Create or advance left_items to the next list.
		/* Create or advance left_items to the next list. */
		if (right_items == NULL) {
			right_items = (lisp_list*) lisp_new(rt, type_list);
			rv = right_items;


@@ 526,11 526,11 @@ static lisp_list *advance_lists(lisp_runtime *rt, lisp_list *list_of_lists)
			right_items->right = lisp_new(rt, type_list);
			right_items = (lisp_list*) right_items->right;
		}
		// Check the next node in the list to make sure it's actually a list.
		/* Check the next node in the list to make sure it's actually a list. */
		if (list_of_lists->left->type != type_list) {
			return NULL;
		}
		// Get the next node in the list and get the argument.
		/* Get the next node in the list and get the argument. */
		lisp_list *l = (lisp_list*) list_of_lists->left;
		right_items->left = l->right;
		list_of_lists = (lisp_list*) list_of_lists->right;


@@ 546,7 546,7 @@ static lisp_value *lisp_builtin_map(lisp_runtime *rt, lisp_scope *scope,
	lisp_list *ret = NULL, *args, *rv;
	lisp_list *map_args = (lisp_list *) lisp_eval_list(rt, scope, a);

	// Get the function from the first argument in the list.
	/* Get the function from the first argument in the list. */
	f = map_args->left;
	if (map_args->right->type != type_list) {
		return (lisp_value*) lisp_error_new(rt, "need at least two arguments");


@@ 583,7 583,7 @@ static lisp_value *lisp_builtin_reduce(lisp_runtime *rt, lisp_scope *scope, lisp
		}
		initializer = list->left;
		list = (lisp_list*)list->right;
 } else if (length == 3) {
	} else if (length == 3) {
		if (!lisp_get_args(args, "**l", &callable, &initializer, &list)) {
			return (lisp_value*) lisp_error_new(rt, "reduce: callable, initializer, and list required");
		}