~lattis/muon

ref: 49a50d56c57dfdf470b4bcbffc16bc5ec92dcb4e muon/src/functions/array.c -rw-r--r-- 1.4 KiB
49a50d56Stone Tickle support comma seperated option array 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "posix.h"

#include "functions/common.h"
#include "functions/array.h"
#include "lang/interpreter.h"
#include "log.h"

static bool
func_array_length(struct workspace *wk, uint32_t rcvr, uint32_t args_node, uint32_t *obj)
{
	if (!interp_args(wk, args_node, NULL, NULL, NULL)) {
		return false;
	}

	make_obj(wk, obj, obj_number)->dat.num = get_obj(wk, rcvr)->dat.arr.len;
	return true;
}

static bool
func_array_get(struct workspace *wk, uint32_t rcvr, uint32_t args_node, obj *res)
{
	struct args_norm an[] = { { obj_number }, ARG_TYPE_NULL };
	struct args_norm ao[] = { { obj_any }, ARG_TYPE_NULL };
	if (!interp_args(wk, args_node, an, ao, NULL)) {
		return false;
	}

	int64_t i = get_obj(wk, an[0].val)->dat.num;

	if (!bounds_adjust(wk, rcvr, &i)) {
		if (ao[0].set) {
			*res = ao[0].val;
		} else {
			interp_error(wk, an[0].node, "index out of bounds");
			return false;
		}
	} else {
		obj_array_index(wk, rcvr, i, res);
	}

	return true;
}

static bool
func_array_contains(struct workspace *wk, obj rcvr, uint32_t args_node, obj *res)
{
	struct args_norm an[] = { { obj_any }, ARG_TYPE_NULL };
	if (!interp_args(wk, args_node, an, NULL, NULL)) {
		return false;
	}

	make_obj(wk, res, obj_bool)->dat.num = obj_array_in(wk, rcvr, an[0].val);
	return true;
}


const struct func_impl_name impl_tbl_array[] = {
	{ "length", func_array_length },
	{ "get", func_array_get },
	{ "contains", func_array_contains },
	{ NULL, NULL },
};