summaryrefslogtreecommitdiff
path: root/uscript/val.c
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-04-17 22:48:52 -0400
committeriamcheeseman <[email protected]>2026-04-17 22:48:52 -0400
commitf328d3b2bea11f6b89bf4b3707205ecd8496b93d (patch)
tree6d3fdb155a7d4c19332f54790b1d6ae89ae0b04f /uscript/val.c
parentde5d3ebdbc674bf8f1e324ee5b43c51af288a286 (diff)
microscript: add maps
Diffstat (limited to 'uscript/val.c')
-rw-r--r--uscript/val.c39
1 files changed, 38 insertions, 1 deletions
diff --git a/uscript/val.c b/uscript/val.c
index 76f1376..9df040a 100644
--- a/uscript/val.c
+++ b/uscript/val.c
@@ -9,6 +9,7 @@
#define STR_NUM_FMT "%g"
#define STR_FUNC_FMT "<func '%s': %p>"
#define STR_ARR_FMT "<arr: %p>"
+#define STR_MAP_FMT "<map: %p>"
static
void init_obj(struct us_val val, struct us_obj *obj)
@@ -46,6 +47,17 @@ struct us_arr *create_arr(void)
return arr;
}
+struct us_map *create_map(void)
+{
+ struct us_map *map = mem_alloc(sizeof(struct us_map));
+ map->e = NULL;
+ map->locked = create_bool(false);
+ map->len = 0;
+ map->cap = 0;
+ init_obj(wrap_map(map), &map->header);
+ return map;
+}
+
struct us_proto *create_proto(struct us_str *name)
{
struct us_proto *proto = mem_alloc(sizeof(struct us_proto));
@@ -100,10 +112,16 @@ void free_val(struct us_val v)
}
case VAL_ARR: {
struct us_arr *arr = get_arr(v);
- da_free(arr->e);
+ mem_free(arr->e);
mem_free(arr);
break;
}
+ case VAL_MAP: {
+ struct us_map *map = get_map(v);
+ mem_free(map->e);
+ mem_free(map);
+ break;
+ }
case VAL_PROTO: {
struct us_proto *proto = get_proto(v);
da_free(proto->bytecode);
@@ -156,6 +174,7 @@ bool vals_eql(struct us_val a, struct us_val b)
memcmp(a_str->chars, b_str->chars, a_str->len) == 0;
}
case VAL_ARR:
+ case VAL_MAP:
case VAL_FUNC:
case VAL_CFUNC:
case VAL_UPVAL:
@@ -214,6 +233,15 @@ char *val_to_str(struct us_val v, int *len_out)
*len_out = len;
return str;
}
+ case VAL_MAP: {
+ const struct us_map *map = get_map(v);
+ int len = snprintf(NULL, 0, STR_MAP_FMT, (void*)map);
+ char *str = mem_alloc(sizeof(char) * (len + 1));
+ snprintf(str, len + 1, STR_MAP_FMT, (void*)map);
+ if (len_out)
+ *len_out = len;
+ return str;
+ }
case VAL_PROTO: {
const struct us_proto *proto = get_proto(v);
int len = snprintf(
@@ -266,3 +294,12 @@ char *val_to_str(struct us_val v, int *len_out)
return NULL;
}
+bool val_as_bool(struct us_val v)
+{
+ if (v.type == VAL_ZILCH)
+ return false;
+ if (v.type == VAL_BOOL)
+ return get_bool(v);
+ return true;
+}
+