Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions impls/c.2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ifndef no_fast
CFLAGS += -flto -O3 -DNDEBUG
LDFLAGS += -flto
endif
ifdef profilie
ifdef profile
CFLAGS += -pg
LDFLAGS += -pg
endif
Expand Down Expand Up @@ -71,6 +71,6 @@ deps:
$(CC) -MM -MF- *.c > $@

clean:
rm -f $(S0+) *.o deps
rm -f $(S0+) *.o deps gmon.out

.PHONY: all clean
13 changes: 13 additions & 0 deletions impls/c.2/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
make -Cimpls/c.2/ clean
make -Cimpls/c.2/ no_fast=1
make test^c.2 HARD=1 REGRESS=1
make test^mal HARD=1 MAL_IMPL=c.2

make -Cimpls/c.2/ clean
make -Cimpls/c.2/
make perf^c.2

make -Cimpls/c.2/ clean
make -Cimpls/c.2/ stepA_mal profile=1
make perf^c.2
(cd impls/c.2/ && gprof stepA_mal | less)
52 changes: 26 additions & 26 deletions impls/c.2/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ MalType mal_nth(list args) {
}
}
else if ((v = is_vector(lst))) {
if (idx < v->count) {
if ((size_t)idx < v->count) {
return v->nth[idx];
}
} else {
Expand Down Expand Up @@ -319,7 +319,7 @@ MalType mal_rest(list args) {
return make_list(NULL);
}
else if ((v = is_vector(lst))) {
for (int i = v->count - 1; 0 < i; i--) {
for (size_t i = v->count; 1 < i--; ) {
result = list_push(result, v->nth[i]);
}
return make_list(result);
Expand All @@ -342,7 +342,7 @@ MalType mal_cons(list args) {
list result = NULL;
vector_t v;
if ((v = is_vector(lst))) {
for (int i = v->count - 1; -1 < i; i--) {
for (size_t i = v->count; i--; ) {
result = list_push(result, v->nth[i]);
}
}
Expand Down Expand Up @@ -400,7 +400,7 @@ MalType mal_count(list args) {
else if (!is_list(val, &mal_list)) {
bad_type("count", MALTYPE_LIST | MALTYPE_NIL | MALTYPE_VECTOR, val);
}
return make_integer(list_count(mal_list));
return make_integer((long)list_count(mal_list));
}

MalType mal_empty_questionmark(list args) {
Expand Down Expand Up @@ -562,7 +562,7 @@ MalType mal_apply(list args) {
// Append the elements of the final sequence,
// efficiently if it is a list.
if (v) {
for(int i = v->count- 1; 0 <= i; i--) {
for (size_t i = v->count; i--; ) {
*lst_last = list_push(*lst_last, v->nth[i]);
}
}
Expand Down Expand Up @@ -632,12 +632,13 @@ MalType mal_keyword(list args) {

MalType mal_vector(list args) {
/* Accepts any number and type of arguments */
int capacity= list_count(args);
size_t capacity = list_count(args);
struct vector* v = vector_new(capacity);
while (args) {
vector_append(&capacity, &v, args->data);
args = args->next;
}
assert(v->count == capacity);
return make_vector(v);
}

Expand Down Expand Up @@ -826,17 +827,18 @@ MalType mal_conj(list args) {
}
else if ((src = is_vector(lst))) {

int capacity = src->count + list_count(rest);
size_t capacity = src->count + list_count(rest);
struct vector* new_vec = vector_new(capacity);

for (int i = 0; i <= src->count - 1; i++) {
for (size_t i = 0; i < src->count; i++) {
vector_append(&capacity, &new_vec, src->nth[i]);
}

while(rest) {
vector_append(&capacity, &new_vec, rest->data);
rest = rest->next;
}
assert(new_vec->count == capacity);
return make_vector(new_vec);
}
else {
Expand All @@ -862,18 +864,18 @@ MalType mal_seq(list args) {
return make_nil();
}
else {
for (int i = strlen(ch) - 1; -1 < i; i--) {
for (size_t i = strlen(ch); i--; ) {
char* new_ch = GC_MALLOC(2);
new_ch[0] = ch[i];
new_ch[1] = 0;
*new_ch = ch[i];
assert(!new_ch[1]);

lst = list_push(lst, make_string(new_ch));
}
return make_list(lst);
}
}
else if ((v = is_vector(val))) {
for (int i = v->count - 1; 0 <= i; i--) {
for (size_t i = v->count; i--; ) {
lst = list_push(lst, v->nth[i]);
}
return lst ? make_list(lst) : make_nil();
Expand Down Expand Up @@ -949,13 +951,15 @@ struct {
{ "double", MALTYPE_FLOAT, &ffi_type_double },
{ "float", MALTYPE_FLOAT, &ffi_type_float },
};
int core_ffi_find(const char *type) {
// Negative if not found
int i = sizeof(core_ffi_translations) / sizeof(*core_ffi_translations) - 1;
while ((0 <= i) && strcmp(core_ffi_translations[i].c_type, type)) {
i--;
size_t core_ffi_find(const char *type) {
for (size_t i = 0;
i < sizeof(core_ffi_translations) / sizeof(*core_ffi_translations);
i++) {
if (!strcmp(core_ffi_translations[i].c_type, type)) {
return i;
}
}
return i;
make_error("'ffi': unknown type '%s'", type);
}
MalType mal_dot(list args) {

Expand All @@ -975,10 +979,8 @@ MalType mal_dot(list args) {
if (!return_type_str) {
bad_type(".", MALTYPE_STRING, a->data);
}
int return_type = core_ffi_find(return_type_str);
if (return_type < 0) {
make_error("'ffi': return type not supported '%s'", return_type_str);
}
size_t return_type = core_ffi_find(return_type_str);
if (mal_error) return NULL;

a = a->next;

Expand All @@ -1000,10 +1002,8 @@ MalType mal_dot(list args) {
if (!val_type) {
bad_type(".", MALTYPE_STRING, a->data);
}
int val_type_index = core_ffi_find(val_type);
if (val_type_index < 0) {
make_error("'ffi': type not recognised '%s'", val_type);
}
size_t val_type_index = core_ffi_find(val_type);
if (mal_error) return NULL;
arg_types[arg_count] = core_ffi_translations[val_type_index].ffit;

a = a->next;
Expand Down
2 changes: 1 addition & 1 deletion impls/c.2/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern MalType mal_error;

#define make_error(...) { \
mal_error = make_string(mal_printf(__VA_ARGS__)); \
return NULL; \
return 0; \
}

#define bad_type(context, mask, form) \
Expand Down
11 changes: 6 additions & 5 deletions impls/c.2/hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct map {
size_t size;
struct bucket {
MalType key;
MalType value;
void* value;
} buckets[];
};

Expand Down Expand Up @@ -102,7 +102,7 @@ size_t search(hashmap map, MalType key) {
return index;
}

void put(struct map* map, MalType key, MalType value) {
void put(struct map* map, MalType key, void* value) {
size_t i = search(map, key);
if (!map->buckets[i].key) {
map->used++;
Expand All @@ -112,7 +112,8 @@ void put(struct map* map, MalType key, MalType value) {
map->buckets[i].value = value;
}

struct map* hashmap_put(struct map* map, MalType key, MalType value) {
struct map* hashmap_put(struct map* map, MalType key, void* value) {
assert(value);
if (map->size <= 2 * (map->used + 1)) {
// Reallocate.
size_t size = map->size * GROW_FACTOR;
Expand All @@ -130,7 +131,7 @@ struct map* hashmap_put(struct map* map, MalType key, MalType value) {
return map;
}

inline MalType hashmap_get(hashmap map, MalType key) {
inline void* hashmap_get(hashmap map, MalType key) {
return map->buckets[search(map, key)].value; // may be null
}

Expand Down Expand Up @@ -168,7 +169,7 @@ inline MalType map_key(hashmap map, map_cursor position) {
return map->buckets[position].key;
}

inline MalType map_val(hashmap map, map_cursor position) {
inline void* map_val(hashmap map, map_cursor position) {
assert(position < map->size);
assert(map->buckets[position].key);
assert(map->buckets[position].value);
Expand Down
7 changes: 4 additions & 3 deletions impls/c.2/hashmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ struct map* map_empty();

struct map* map_copy(hashmap);

struct map* hashmap_put(struct map* map, MalType key, MalType value);
struct map* hashmap_put(struct map* map, MalType key, void* value);
// Value must not be NULL.
// May reallocate.

MalType hashmap_get(hashmap map, MalType key);
void* hashmap_get(hashmap map, MalType key);
// Returns NULL if the map does not contain the key.

void map_dissoc_mutate(struct map* map, MalType key);
Expand All @@ -30,6 +31,6 @@ map_cursor map_iter(hashmap);
bool map_cont(hashmap, map_cursor);
map_cursor map_next(hashmap, map_cursor);
MalType map_key(hashmap, map_cursor);
MalType map_val(hashmap, map_cursor);
void* map_val(hashmap, map_cursor);

#endif
4 changes: 2 additions & 2 deletions impls/c.2/linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ list list_push(list lst, MalType data_ptr) {
return new_head;
}

int list_count(list lst) {
size_t list_count(list lst) {

int counter = 0;
size_t counter = 0;

while(lst) {

Expand Down
2 changes: 1 addition & 1 deletion impls/c.2/linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ struct pair_s {

/* interface */
list list_push(list lst, MalType data_ptr);
int list_count(list lst);
size_t list_count(list lst);

#endif
4 changes: 2 additions & 2 deletions impls/c.2/printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ int print_L(FILE* stream, const struct printf_info *i, const void *const *a) {
int pr_str_vector(FILE* stream, const struct printf_info *i, vector_t v) {
int written = 0;
ADD(fprintf(stream, "["));
for (int j = 0; j < v->count; j++) {
for (size_t j = 0; j < v->count; j++) {
ADD(fprintf(stream,
i->alt ? "%s%#M" : "%s%M",
j ? " " : "",
Expand Down Expand Up @@ -269,7 +269,7 @@ const char* mal_printf(const char* fmt, ...) {
char* buffer = GC_MALLOC(n + 1);

va_start(argptr, fmt);
int again = vsnprintf(buffer, n + 1, fmt, argptr);
int again = vsnprintf(buffer, n+1, fmt, argptr);
assert(n == again);
#ifdef NDEBUG
(void)again;
Expand Down
8 changes: 5 additions & 3 deletions impls/c.2/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

#include <gc.h>

Expand Down Expand Up @@ -95,6 +96,7 @@ const char* read_symbol (Reader reader) {
size_t len = *reader - start;
char* result = GC_MALLOC(len + 1);
strncpy(result, start, len);
assert(!result[len]);
return result;
}

Expand Down Expand Up @@ -177,7 +179,7 @@ MalType read_number(Reader reader) {
// (followed by a digit).
(*reader)++;

int has_decimal_point = 0;
bool has_decimal_point = false;

while(true) {
if(**reader == '.') {
Expand Down Expand Up @@ -255,8 +257,8 @@ MalType read_list(Reader reader) {

MalType read_vector(Reader reader) {
(*reader)++;
int capacity = 10;
struct vector* v = vector_new(10);
size_t capacity = 10;
struct vector* v = vector_new(capacity);
while(true) {
DEBUG("searching ']'");
skip_spaces(reader);
Expand Down
5 changes: 3 additions & 2 deletions impls/c.2/step2_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,14 @@ list evaluate_list(list lst, hashmap env) {
}

MalType evaluate_vector(vector_t lst, hashmap env) {
int capacity = lst->count;
size_t capacity = lst->count;
struct vector* evlst = vector_new(capacity);
for(int i = 0; i <= lst->count - 1; i++) {
for (size_t i = 0; i < capacity; i++) {
MalType new = EVAL(lst->nth[i], env);
if (mal_error) return NULL;
vector_append(&capacity, &evlst, new);
}
assert(evlst->count == capacity);
return make_vector(evlst);
}

Expand Down
Loading