Skip to content

Commit

Permalink
Merge branch 'master' of github.com:NicoNex/tau
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoNex committed Nov 28, 2023
2 parents f178748 + 72bd6d9 commit bd1e325
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 27 deletions.
13 changes: 10 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ifneq ($(origin CC), undefined)
endif
endif

.PHONY: all tau libffi debug install profile run
.PHONY: all tau libffi install profile run

all: libffi tau

Expand All @@ -35,12 +35,19 @@ tau:
CC=$(CC) CGO_CFLAGS="$(CFLAGS)" CGO_LDFLAGS="$(LDFLAGS)" go build -o $(DIR)/tau

libffi:
cd libffi && \
if [ ! -d libffi ] || [ $$(ls -1q libffi | wc -l) -eq 0 ]; then \
git submodule init; \
git submodule update --recursive; \
fi

CC=$(CC) cd libffi && \
ACLOCAL_PATH=$(ACLOCAL_PATH) autoreconf -i && \
./configure --prefix=$(DIR)/internal/obj/libffi --disable-shared --enable-static && \
make install CC=$(CC)

debug: CGO_CFLAGS='-DDEBUG' all
debug:
cd cmd/tau && \
CC=$(CC) CGO_CFLAGS="$(CFLAGS) -DDEBUG -DGC_DEBUG" CGO_LDFLAGS="$(LDFLAGS)" go build -o $(DIR)/tau

install: all
mv tau /usr/bin
Expand Down
32 changes: 31 additions & 1 deletion internal/obj/builtins.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,35 @@ static struct object slice_b(struct object *args, size_t len) {
}
}

static struct object keys_b(struct object *args, size_t len) {
if (len != 1) {
return errorf("keys: wrong number of arguments, expected 1, got %lu", len);
} else if (args[0].type != obj_map) {
return errorf("keys: argument must be a map, got %s instead", otype_str(args[0].type));
}
return map_keys(args[0]);
}

static struct object delete_b(struct object *args, size_t len) {
if (len != 2) {
return errorf("delete: wrong number of arguments, expected 2, got %lu", len);
} else if (args[0].type != obj_map) {
return errorf("delete: first argument must be a map, got %s instead", otype_str(args[0].type));
}

switch (args[1].type) {
case obj_boolean:
case obj_integer:
case obj_float:
case obj_string:
case obj_error:
map_delete(args[0], args[1]);
return null_obj;
default:
return errorf("delete: second argument must be one of boolean integer float string error, got %s instead", otype_str(args[1].type));
}
}

// TODO: eventually add the obj_integer case like in Python.
static struct object bytes_b(struct object *args, size_t len) {
if (len != 1) {
Expand Down Expand Up @@ -552,6 +581,7 @@ const builtin builtins[NUM_BUILTINS] = {
oct_b,
bin_b,
slice_b,
NULL, // open
keys_b,
delete_b,
bytes_b
};
34 changes: 15 additions & 19 deletions internal/obj/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ void mark_list_obj(struct object l) {
}
}

struct object make_list(size_t cap) {
struct list *l = malloc(sizeof(struct list));
l->list = calloc(cap, sizeof(struct object));
l->len = 0;
l->cap = cap;
l->m_parent = NULL;

return (struct object) {
.data.list = l,
.type = obj_list,
.marked = MARKPTR()
};
}

struct object new_list_obj(struct object *list, size_t len) {
struct list *l = malloc(sizeof(struct list));
l->list = list;
Expand All @@ -58,7 +72,7 @@ struct object new_list_obj(struct object *list, size_t len) {
return (struct object) {
.data.list = l,
.type = obj_list,
.marked = MARKPTR(),
.marked = MARKPTR()
};
}

Expand Down Expand Up @@ -90,24 +104,6 @@ struct object new_list_slice(struct object *list, size_t len, uint32_t *m_parent
};
}

inline struct list new_list(size_t cap) {
return (struct list) {
.list = malloc(sizeof(struct object) * cap),
.cap = cap,
.len = 0
};
}

inline void list_insert(struct list *l, struct object o, size_t idx) {
if (idx >= l->cap) {
if (l->cap == 0) l->cap = 1;
while (l->cap <= idx) l->cap *= 2;
l->list = realloc(l->list, sizeof(struct object) * l->cap);
}
l->list[idx] = o;
l->len++;
}

inline struct list list_copy(struct list l) {
struct list ret = {
.list = malloc(sizeof(struct object) * l.cap),
Expand Down
61 changes: 60 additions & 1 deletion internal/obj/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,69 @@ static inline void _map_set(struct map_node **n, struct key_hash k, struct map_p
}
}

static inline void _map_dispose(struct map_node *n) {
static inline void map_set_node(struct map_node **root, struct map_node **cur, struct map_node *n) {
if (*cur == NULL) {
*cur = n;
return;
}

int cmp = memcmp(&(*cur)->key, &n->key, sizeof(struct key_hash));
if (cmp == 0) {
struct map_node *l = (*cur)->l;
struct map_node *r = (*cur)->r;

free(*cur);
*cur = n;
if (l != NULL) map_set_node(root, root, l);
if (r != NULL) map_set_node(root, root, r);
} else if (cmp < 0) {
map_set_node(root, &(*cur)->l, n);
} else {
map_set_node(root, &(*cur)->r, n);
}
}

static inline void _map_delete(struct map_node **root, struct map_node **n, struct key_hash k) {
if (*n != NULL) {
struct map_node *node = *n;
int cmp = memcmp(&k, &node->key, sizeof(struct key_hash));

if (cmp == 0) {
*n = NULL;
if (node->l) map_set_node(root, root, node->l);
if (node->r) map_set_node(root, root, node->r);
free(node);
} else if (cmp < 0) {
_map_delete(root, &(*n)->l, k);
} else {
_map_delete(root, &(*n)->r, k);
}
}
}

static inline void _map_dispose(struct map_node * restrict n) {
if (n != NULL) {
if (n->l != NULL) _map_dispose(n->l);
if (n->r != NULL) _map_dispose(n->r);
free(n);
}
}

static inline void _map_keys(struct map_node * restrict n, struct list *list) {
if (n != NULL) {
list->list[list->len++] = n->val.key;
_map_keys(n->l, list);
_map_keys(n->r, list);
}
}

struct object map_keys(struct object map) {
struct object list = make_list(map.data.map->len);

_map_keys(map.data.map->root, list.data.list);
return list;
}

struct map_pair map_get(struct object map, struct object o) {
return _map_get(map.data.map->root, hash(o));
}
Expand All @@ -115,6 +170,10 @@ struct map_pair map_set(struct object map, struct object k, struct object v) {
return p;
}

void map_delete(struct object map, struct object key) {
_map_delete(&map.data.map->root, &map.data.map->root, hash(key));
}

void dispose_map_obj(struct object map) {
_map_dispose(map.data.map->root);
free(map.data.map);
Expand Down
3 changes: 2 additions & 1 deletion internal/obj/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ var (
"oct",
"bin",
"slice",
"open",
"keys",
"delete",
"bytes",
}

Expand Down
5 changes: 3 additions & 2 deletions internal/obj/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,13 @@ char *error_str(struct object o);
void dispose_error_obj(struct object o);

// List object.
struct object make_list(size_t cap);
struct object new_list_obj(struct object *list, size_t len);
struct object new_list_obj_data(struct object *list, size_t len, size_t cap);
struct object new_list_slice(struct object *list, size_t len, uint32_t *m_parent);
char *list_str(struct object o);
void mark_list_obj(struct object l);
void dispose_list_obj(struct object o);
struct list new_list(size_t cap);
void list_insert(struct list *l, struct object o, size_t idx);
struct list list_copy(struct list l);

// Pipe object.
Expand All @@ -189,7 +188,9 @@ struct map_pair map_get(struct object map, struct object k);
struct map_pair map_set(struct object map, struct object k, struct object v);
void mark_map_obj(struct object m);
char *map_str(struct object map);
void map_delete(struct object map, struct object key);
void dispose_map_obj(struct object map);
struct object map_keys(struct object map);

// Object object.
struct object new_object();
Expand Down
5 changes: 5 additions & 0 deletions internal/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func (vm VM) Free() {
C.vm_dispose(vm)
}

func (vm VM) LastPoppedStackObj() obj.Object {
o := C.vm_last_popped_stack_elem(vm)
return *(*obj.Object)(unsafe.Pointer(&o))
}

func cobj(o obj.Object) C.struct_object {
return *(*C.struct_object)(unsafe.Pointer(&o))
}
Expand Down

0 comments on commit bd1e325

Please sign in to comment.