aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPreston Pan <preston@nullring.xyz>2024-01-09 19:55:24 -0800
committerPreston Pan <preston@nullring.xyz>2024-01-09 19:55:24 -0800
commitbb26e7e9581f507772521ebb447f897a8f68ad57 (patch)
tree655aec6c85930f5f34c5a0f930217b21cc0c6aab
parent707cf3115f060fe385a253b08badd701d3caea42 (diff)
add sleep; fix bugs
-rw-r--r--include/builtins.h2
-rw-r--r--include/stem.h22
-rw-r--r--src/builtins.c28
-rw-r--r--src/stem.c72
4 files changed, 77 insertions, 47 deletions
diff --git a/include/builtins.h b/include/builtins.h
index 148fba7..89fb26d 100644
--- a/include/builtins.h
+++ b/include/builtins.h
@@ -49,6 +49,8 @@ void ssize(value_t *v);
void qstack(value_t *v);
void vat(value_t *v);
void stemfwrite(value_t *v);
+void stemsleep(value_t *v);
+void add_objs();
void add_funcs();
#endif // BUILTINS_H_
diff --git a/include/stem.h b/include/stem.h
index 2643d36..e8a8587 100644
--- a/include/stem.h
+++ b/include/stem.h
@@ -76,6 +76,12 @@ custom_t *init_custom(void (*)(void *), void (*)(void *), void *(*)(void *));
void custom_free(void *);
+void add_func(ht_t *h, void (*func)(value_t *), char *key);
+
+void add_obj(ht_t *h, ht_t *h2, void (*printfunc)(void *),
+ void (*freefunc)(void *), void *(*copyfunc)(void *),
+ void (*createfunc)(void *), char *key);
+
parser_t *init_parser(char *source);
void parser_reset(parser_t *p, char *source);
@@ -84,13 +90,25 @@ void parser_move(parser_t *p);
void parser_skip_whitespace(parser_t *p);
+value_t *parse_string(parser_t *p);
+
+value_t *parse_quote(parser_t *p);
+
+value_t *parse_word(parser_t *p);
+
+value_t *parse_num(parser_t *p);
+
+void parser_error(parser_t *p);
+
value_t *parser_get_next(parser_t *p);
node_t *init_node(string_t *key, void *v);
+void node_free(node_t *n, void (*freefunc)(void *));
+
sll_t *init_sll();
-void sll_add(sll_t *l, string_t *key, void *v);
+void sll_add(sll_t *l, string_t *key, void *v, void (*freefunc)(void *));
void *sll_get(sll_t *l, string_t *key);
@@ -98,7 +116,7 @@ void sll_free(sll_t *l, void (*freefunc)(void *));
ht_t *init_ht(size_t size);
-void ht_add(ht_t *h, string_t *key, void *v);
+void ht_add(ht_t *h, string_t *key, void *v, void (*freefunc)(void *));
void *ht_get(ht_t *h, string_t *key);
diff --git a/src/builtins.c b/src/builtins.c
index df958f4..f801028 100644
--- a/src/builtins.c
+++ b/src/builtins.c
@@ -6,6 +6,7 @@
#include <stdlib.h>
#include <stem.h>
#include <string.h>
+#include <unistd.h>
#define MAX 1000
#define JUSTDO(a) \
@@ -190,7 +191,7 @@ void stemfunc(value_t *v) {
eval_error();
return;
}
- ht_add(WORD_TABLE, string_copy(v1->str_word), v2);
+ ht_add(WORD_TABLE, string_copy(v1->str_word), v2, value_free);
value_free(v1);
}
@@ -766,6 +767,7 @@ void clib(value_t *v) {
} else {
(*af)();
(*aobjs)();
+ dlclose(handle);
value_free(v1);
}
}
@@ -1074,18 +1076,19 @@ void stemfwrite(value_t *v) {
fclose(fp);
}
-void add_func(ht_t *h, void (*func)(value_t *), char *key) {
- string_t *s = init_string(key);
- ht_add(h, s, func);
-}
-
-void add_obj(ht_t *h, ht_t *h2, void (*printfunc)(void *),
- void (*freefunc)(void *), void *(*copyfunc)(void *),
- void (*createfunc)(void *), char *key) {
+void stemsleep(value_t *v) {
+ value_t *v1 = array_pop(STACK);
+ if (v1 == NULL) {
+ eval_error();
+ return;
+ }
+ if (v1->type != VINT && v1->type != VFLOAT) {
+ array_append(STACK, v1);
+ eval_error();
+ }
- custom_t *c = init_custom(printfunc, freefunc, copyfunc);
- ht_add(h, init_string(key), c);
- ht_add(h2, init_string(key), createfunc);
+ sleep(v1->int_float);
+ value_free(v1);
}
void add_objs() {}
@@ -1137,4 +1140,5 @@ void add_funcs() {
add_func(FLIT, isdef, "isdef");
add_func(FLIT, dsc, "dsc");
add_func(FLIT, clib, "clib");
+ add_func(FLIT, stemsleep, "sleep");
}
diff --git a/src/stem.c b/src/stem.c
index f090940..d2204f9 100644
--- a/src/stem.c
+++ b/src/stem.c
@@ -20,9 +20,13 @@ void func_free(void *f) {}
array_t *init_array(size_t size) {
array_t *a = calloc(1, sizeof(array_t));
+ if (!a)
+ die("calloc on array");
a->size = 0;
a->capacity = size;
a->items = calloc(a->capacity, sizeof(value_t *));
+ if (!a->items)
+ die("calloc on a->items");
return a;
}
@@ -60,9 +64,13 @@ void array_free(array_t *a) {
array_t *array_copy(array_t *a) {
array_t *b = calloc(1, sizeof(array_t));
+ if (!b)
+ die("calloc on array");
b->size = a->size;
b->capacity = a->capacity;
b->items = calloc(b->capacity, sizeof(value_t *));
+ if (!b->items)
+ die("calloc on b->items");
for (int i = 0; i < a->size; i++) {
b->items[i] = value_copy(a->items[i]);
}
@@ -71,6 +79,8 @@ array_t *array_copy(array_t *a) {
value_t *init_value(int type) {
value_t *v = calloc(1, sizeof(value_t));
+ if (!v)
+ die("calloc on value");
v->type = type;
v->escaped = false;
return v;
@@ -112,6 +122,8 @@ void value_free(void *vtmp) {
custom_t *init_custom(void (*printfunc)(void *), void (*freefunc)(void *),
void *(*copyfunc)(void *)) {
custom_t *c = calloc(1, sizeof(custom_t));
+ if (!c)
+ die("calloc on custom");
c->printfunc = printfunc;
c->freefunc = freefunc;
c->copyfunc = copyfunc;
@@ -120,8 +132,23 @@ custom_t *init_custom(void (*printfunc)(void *), void (*freefunc)(void *),
void custom_free(void *c) { free(c); }
+void add_func(ht_t *h, void (*func)(value_t *), char *key) {
+ ht_add(h, init_string(key), func, value_free);
+}
+
+void add_obj(ht_t *h, ht_t *h2, void (*printfunc)(void *),
+ void (*freefunc)(void *), void *(*copyfunc)(void *),
+ void (*createfunc)(void *), char *key) {
+
+ custom_t *c = init_custom(printfunc, freefunc, copyfunc);
+ ht_add(h, init_string(key), c, custom_free);
+ ht_add(h2, init_string(key), createfunc, value_free);
+}
+
parser_t *init_parser(char *source) {
parser_t *p = calloc(1, sizeof(parser_t));
+ if (!p)
+ die("calloc on parser");
p->i = 0;
p->source = source;
p->c = source[0];
@@ -263,6 +290,8 @@ value_t *parser_get_next(parser_t *p) {
node_t *init_node(string_t *key, void *value) {
node_t *n = calloc(1, sizeof(node_t));
+ if (!n)
+ die("calloc on node");
n->key = key;
n->value = value;
n->next = NULL;
@@ -277,12 +306,14 @@ void node_free(node_t *n, void (*freefunc)(void *)) {
sll_t *init_sll() {
sll_t *l = calloc(1, sizeof(sll_t));
+ if (!l)
+ die("calloc on linked list");
l->size = 0;
l->head = NULL;
return l;
}
-void sll_add(sll_t *l, string_t *s, void *v) {
+void sll_add(sll_t *l, string_t *s, void *v, void (*freefunc)(void *)) {
if (l->head == NULL) {
node_t *n = init_node(s, v);
l->head = n;
@@ -292,7 +323,7 @@ void sll_add(sll_t *l, string_t *s, void *v) {
node_t *cur = l->head;
while (cur->next != NULL) {
if (strcmp(s->value, cur->key->value) == 0) {
- value_free(cur->value);
+ freefunc(cur->value);
string_free(s);
cur->value = v;
return;
@@ -309,31 +340,6 @@ void sll_add(sll_t *l, string_t *s, void *v) {
cur->next = n;
}
-void sll_add_func(sll_t *l, string_t *s, void *v) {
- if (l->head == NULL) {
- node_t *n = init_node(s, v);
- l->head = n;
- l->size++;
- return;
- }
- node_t *cur = l->head;
- while (cur->next != NULL) {
- if (strcmp(s->value, cur->key->value) == 0) {
- string_free(s);
- cur->value = v;
- return;
- }
- cur = cur->next;
- }
- if (strcmp(s->value, cur->key->value) == 0) {
- string_free(s);
- cur->value = v;
- return;
- }
- node_t *n = init_node(s, v);
- cur->next = n;
-}
-
void *sll_get(sll_t *l, string_t *k) {
if (l->head == NULL)
return NULL;
@@ -359,20 +365,20 @@ void sll_free(sll_t *l, void (*func)(void *)) {
ht_t *init_ht(size_t size) {
ht_t *h = calloc(1, sizeof(ht_t));
+ if (!h)
+ die("calloc on hash table");
h->size = size;
h->buckets = calloc(h->size, sizeof(sll_t *));
+ if (!h->buckets)
+ die("calloc on hash table array");
for (int i = 0; i < size; i++) {
h->buckets[i] = init_sll();
}
return h;
}
-void ht_add(ht_t *h, string_t *key, void *v) {
- sll_add(h->buckets[hash(h, key->value)], key, v);
-}
-
-void ht_add_func(ht_t *h, string_t *key, void *v) {
- sll_add_func(h->buckets[hash(h, key->value)], key, v);
+void ht_add(ht_t *h, string_t *key, void *v, void (*freefunc)(void *)) {
+ sll_add(h->buckets[hash(h, key->value)], key, v, freefunc);
}
void *ht_get(ht_t *h, string_t *key) {