-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode_meta.c
More file actions
74 lines (69 loc) · 1.77 KB
/
Copy pathnode_meta.c
File metadata and controls
74 lines (69 loc) · 1.77 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef _AMALGAMATE_
#include "script.h"
#include "xstr.h"
#include "utils.h"
#include <string.h>
#endif
static void _meta_on_let(struct node *n, struct node *e) {
const char *name = node_value(e->child);
if (name == 0) {
return;
}
struct xstr *xstr = xstr_create_empty();
for (struct node *nn = e->child->next; nn; nn = nn->next) {
if (nn != e->child->next) {
xstr_cat2(xstr, " ", 1);
}
const char *nv = node_value(nn);
if (is_vlist(nv)) {
struct vlist_iter iter;
vlist_iter_init(nv, &iter);
for (int i = 0; vlist_iter_next(&iter); ++i) {
if (i) {
xstr_cat2(xstr, " ", 1);
}
xstr_cat2(xstr, iter.item, iter.len);
}
} else {
xstr_cat(xstr, nv);
}
}
node_env_set(n, name, xstr_ptr(xstr));
xstr_destroy(xstr);
}
static void _meta_on_entry(struct node *n, struct node *e) {
if (e->type != NODE_TYPE_BAG) {
return;
}
const int plen = AK_LLEN("META_");
char name[plen + strlen(e->value) + 1];
char *wp = name;
memcpy(wp, "META_", plen), wp += plen;
memcpy(wp, e->value, sizeof(name) - plen);
for (int i = plen; i < sizeof(name) - 1; ++i) {
name[i] = utils_toupper_ascii(name[i]);
}
struct xstr *xstr = xstr_create_empty();
for (struct node *nn = e->child; nn; nn = nn->next) {
if (nn != e->child) {
xstr_cat2(xstr, " ", 1);
}
const char *nv = node_value(nn);
xstr_cat(xstr, nv);
}
node_env_set(n, name, xstr_ptr(xstr));
xstr_destroy(xstr);
}
static void _meta_init(struct node *n) {
for (struct node *nn = n->child; nn; nn = nn->next) {
if (strcmp(nn->value, "let") == 0) {
_meta_on_let(n, nn);
} else {
_meta_on_entry(n, nn);
}
}
}
int node_meta_setup(struct node *n) {
n->init = _meta_init;
return 0;
}