~lbnz/xr0

4670c4f2947ea500d96e35ae5c048ea1d4f1d1c6 — Claude Betz 5 months ago 652e44e chore/simplify-map
checkpoint working on compiling
5 files changed, 74 insertions(+), 33 deletions(-)

M Makefile
M include/generic_map.h
M src/ast/lex.l
M src/state/Makefile
M src/util/generic_map.c
M Makefile => Makefile +52 -1
@@ 40,15 40,34 @@ LEX_OBJ = $(BUILD_DIR)/lex.o
UTIL_OBJ = $(BUILD_DIR)/util.o
GRAM_OBJ = $(BUILD_DIR)/gram.o

AST_FUNCTION_MAP_OBJ = $(BUILD_DIR)/ast_function_map.o
AST_VARIABLE_MAP_OBJ = $(BUILD_DIR)/ast_variable_map.o
AST_TYPE_MAP_OBJ = $(BUILD_DIR)/ast_type_map.o
LONG_MAP_OBJ = $(BUILD_DIR)/long_map.o
OBJECT_MAP_OBJ = $(BUILD_DIR)/object_map.o
VARIABLE_MAP_OBJ = $(BUILD_DIR)/variable_map.o
VALUE_MAP_OBJ = $(BUILD_DIR)/value_map.o
GENERIC_MAP_OBJ = $(BUILD_DIR)/generic_map.o

GRAM_TAB_C = $(BUILD_DIR)/gram.tab.c
GRAM_TAB_H = $(BUILD_DIR)/gram.tab.h
LEX_YY_C = $(BUILD_DIR)/lex.yy.c

MAP_OBJECTS = $(AST_FUNCTION_MAP_OBJ) \
	      $(AST_VARIABLE_MAP_OBJ) \
	      $(AST_TYPE_MAP_OBJ) \
	      $(LONG_MAP_OBJ) \
	      $(OBJECT_MAP_OBJ) \
	      $(VARIABLE_MAP_OBJ) \
	      $(VALUE_MAP_OBJ) \
	      $(GENERIC_MAP_OBJ)

XR0_OBJECTS = $(AST_OBJ) \
	      $(LEX_OBJ) \
	      $(GRAM_OBJ) \
	      $(STATE_OBJ) \
	      $(UTIL_OBJ) \
	      $(MAP_OBJECTS) \
	      $(MATH_OBJ)

STATE_OBJECTS = $(VALUE_OBJ) \


@@ 99,7 118,6 @@ $(HEAP_OBJ): $(STATE_DIR)/heap.c $(BLOCK_OBJ)
$(BLOCK_OBJ): $(STATE_DIR)/block.c $(OBJECT_OBJ)
	@printf 'CC\t$@\n'
	@$(CC) $(CFLAGS) -o $@ -c $(STATE_DIR)/block.c

$(OBJECT_OBJ): $(OBJECT_DIR)/object.c $(VALUE_OBJ)
	@printf 'CC\t$@\n'
	@$(CC) $(CFLAGS) -o $@ -c $(OBJECT_DIR)/object.c


@@ 140,6 158,39 @@ $(GRAM_TAB_C) $(GRAM_TAB_H): $(AST_DIR)/gram.y $(BUILD_DIR)
	@printf 'YACC\t$@\n'
	@$(YACC) -o $(BUILD_DIR)/gram.tab.c -d $(AST_DIR)/gram.y

$(AST_FUNCTION_MAP_OBJ): $(UTIL_DIR)/ast_function_map.c $(BUILD_DIR) $(GENERIC_MAP_OBJ)
	@printf 'CC\t$@\n'
	@$(CC) $(CFLAGS) -o $@ -c $(UTIL_DIR)/ast_function_map.c

$(AST_VARIABLE_MAP_OBJ): $(UTIL_DIR)/ast_variable_map.c $(BUILD_DIR) $(GENERIC_MAP_OBJ)
	@printf 'CC\t$@\n'
	@$(CC) $(CFLAGS) -o $@ -c $(UTIL_DIR)/ast_variable_map.c

$(AST_TYPE_MAP_OBJ): $(UTIL_DIR)/ast_type_map.c $(BUILD_DIR) $(GENERIC_MAP_OBJ)
	@printf 'CC\t$@\n'
	@$(CC) $(CFLAGS) -o $@ -c $(UTIL_DIR)/ast_type_map.c

$(LONG_MAP_OBJ): $(UTIL_DIR)/long_map.c $(BUILD_DIR) $(GENERIC_MAP_OBJ)
	@printf 'CC\t$@\n'
	@$(CC) $(CFLAGS) -o $@ -c $(UTIL_DIR)/long_map.c

$(OBJECT_MAP_OBJ): $(UTIL_DIR)/object_map.c $(BUILD_DIR) $(GENERIC_MAP_OBJ)
	@printf 'CC\t$@\n'
	@$(CC) $(CFLAGS) -o $@ -c $(UTIL_DIR)/object_map.c

$(VARIABLE_MAP_OBJ): $(UTIL_DIR)/variable_map.c $(BUILD_DIR) $(GENERIC_MAP_OBJ)
	@printf 'CC\t$@\n'
	@$(CC) $(CFLAGS) -o $@ -c $(UTIL_DIR)/variable_map.c

$(VALUE_MAP_OBJ): $(UTIL_DIR)/value_map.c $(BUILD_DIR) $(GENERIC_MAP_OBJ)
	@printf 'CC\t$@\n'
	@$(CC) $(CFLAGS) -o $@ -c $(UTIL_DIR)/value_map.c

$(GENERIC_MAP_OBJ): $(UTIL_DIR)/generic_map.c $(BUILD_DIR)
	@printf 'CC\t$@\n'
	@$(CC) $(CFLAGS) -o $@ -E $(UTIL_DIR)/generic_map.c


$(BIN_DIR):
	@mkdir -p $(BIN_DIR)


M include/generic_map.h => include/generic_map.h +6 -10
@@ 1,12 1,8 @@
#ifdef TYPE

#ifndef TYPED
#define TYPED(THING) THING
#endif

struct TYPED(map_entry) {
	char *key;
	const TYPE value;
	TYPE value;
};

struct TYPED(map) {


@@ 18,30 14,30 @@ struct TYPED(map) {
 * Create a new map
 */
struct TYPED(map) *
TYPED(map_create) ();
TYPED(map_create)();

/**
 * Free space allocated for map
 */
void
TYPED(map_destroy) (struct TYPED(map) *map);
TYPED(map_destroy)(struct TYPED(map) *map);

/**
 * Get index
 */
static int
TYPED(map_getindex) (struct TYPED(map) *map, const char *key);
TYPED(map_getindex)(struct TYPED(map) *map, char *key);

/**
 * Get value
 */
TYPE
TYPED(map_get) (struct TYPED(map) *map, const char *key);
TYPED(map_get)(struct TYPED(map) *map, char *key);

/**
 * Set value
 */
void
TYPED(map_set) (struct TYPED(map) *map, const char *key, TYPE val);
TYPED(map_set)(struct TYPED(map) *map, char *key, TYPE val);

#endif

M src/ast/lex.l => src/ast/lex.l +0 -6
@@ 286,12 286,6 @@ count()
	}
}

static void
heaptk_destroy(long *tk)
{
	free(tk);
}

void
install_refresh(long tk)
{

M src/state/Makefile => src/state/Makefile +1 -1
@@ 29,6 29,7 @@ VALUE_OBJ = $(BUILD_DIR)/value.o
BLOCK_OBJ = $(BUILD_DIR)/block.o

XR0_OBJECTS = $(AST_OBJ) $(UTIL_OBJ) $(LEX_OBJ)

OBJECTS = $(XR0_OBJECTS) \
	  $(VALUE_OBJ) \
	  $(LOCATION_OBJ) \


@@ 103,7 104,6 @@ $(GRAM_TAB_C) $(GRAM_TAB_H): $(PARSE_DIR)/gram.y $(BUILD_DIR)
	@printf 'YACC\t$@\n'
	@$(YACC) -o $(BUILD_DIR)/gram.tab.c -d $(PARSE_DIR)/gram.y


$(BUILD_DIR):
	@mkdir -p $(BUILD_DIR)


M src/util/generic_map.c => src/util/generic_map.c +15 -15
@@ 4,32 4,32 @@
#include "string.h"
#include "generic_map.h"

TYPED(map_entry)
TYPED(entry_create) (char *key, const void *val)
struct TYPED(map_entry)
TYPED(entry_create)(char *key, TYPE val)
{
	assert(key);
	return TYPED(map_entry) { (char *) key, val };
	return (struct TYPED(map_entry)) { (char *) key, val };
}

void
TYPED(entry_destroy) (TYPED(map_entry) e) {
TYPED(entry_destroy)(struct TYPED(map_entry) e) {
	free(e.key);
}

struct TYPED(map) *
TYPED(map_create) () {
TYPED(map_create)() {
	return (struct TYPED(map) *) calloc(1, sizeof(struct TYPED(map)));
}

void
TYPED(map_destroy) (struct TYPED(map) *m) {
TYPED(map_destroy)(struct TYPED(map) *m) {
	for (int i = 0; i < m->n; i++) {
		entry_destroy(m->entry[i]);
		TYPED(entry_destroy)(m->entry[i]);
	}
}

static int
TYPED(map_getindex) (struct TYPED(map) *m, const char *key) {
TYPED(map_getindex)(struct TYPED(map) *m, char *key) {
	assert(key != NULL);
	for (int i = 0; i < m->n; i++) {
		if (strcmp(m->entry[i].key, key) == 0) {


@@ 40,24 40,24 @@ TYPED(map_getindex) (struct TYPED(map) *m, const char *key) {
}

TYPE
TYPED(map_get) (struct TYPED(map) *m, const char *key)
TYPED(map_get)(struct TYPED(map) *m, char *key)
{
	int index = map_getindex(m, key);
	int index = TYPED(map_getindex)(m, key);
	if (index != -1) {
		return (void *) m->entry[index].val;
		return (TYPE) m->entry[index].value;
	}
	return NULL;
}

void
TYPED(map_set) (struct TYPED(map) *m, char *key, const void *value)
TYPED(map_set)(struct TYPED(map) *m, char *key, TYPE value)
{
	int index = map_getindex(m, key);
	int index = TYPED(map_getindex)(m, key);
	if (index >= 0) {
		m->entry[index].val = value;
		m->entry[index].value = value;
		return;
	}
	m->entry = realloc(m->entry, sizeof(struct TYPED(map_entry)) * ++m->n);
	m->entry[m->n-1] = TYPED(entry_create) (key, value);
	m->entry[m->n-1] = TYPED(entry_create)(key, value);
	return;
}