Skip to content

Commit e899b5a

Browse files
committed
* macros
1 parent 69a4678 commit e899b5a

File tree

8 files changed

+130
-51
lines changed

8 files changed

+130
-51
lines changed

dist/autark.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define CONFIG_H
33

44
#define META_VERSION "0.9.0"
5-
#define META_REVISION "170d75f"
5+
#define META_REVISION "69a4678"
66

77
#endif
88
#define _AMALGAMATE_
@@ -1106,6 +1106,8 @@ void node_reset(struct node *n);
11061106

11071107
const char* node_value(struct node *n);
11081108

1109+
#define NODE_VISIT_CHILD_SKIP INT_MAX
1110+
11091111
int node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct node*, int, void*));
11101112

11111113
void node_module_setup(struct node *n, unsigned flags);
@@ -6519,10 +6521,12 @@ struct _call {
65196521
struct ulist nodes; ///< Nodes stack. struct node*
65206522
struct node *args[MACRO_MAX_ARGS_NUM];
65216523
int nn_idx;
6524+
int arg_idx;
65226525
};
65236526

65246527
static int _call_macro_visit(struct node *n, int lvl, void *d) {
65256528
struct _call *call = d;
6529+
int ret = 0;
65266530

65276531
if (call->mn == n /*skip macro itself */ || call->mn->child == n /* skip macro name */) {
65286532
return 0;
@@ -6534,15 +6538,24 @@ static int _call_macro_visit(struct node *n, int lvl, void *d) {
65346538
}
65356539

65366540
// Macro arg
6537-
if (n->value[0] == '&' && n->value[1] == '\0' && n->child && !n->child->next) {
6541+
if (n->value[0] == '&' && n->value[1] == '\0') {
6542+
int idx;
65386543
int rc = 0;
6539-
int idx = utils_strtol(n->child->value, 10, &rc);
6540-
if (rc || idx < 1 || idx >= MACRO_MAX_ARGS_NUM) {
6541-
node_fatal(rc, n, "Invalid macro arg index: %d", idx);
6542-
return 0;
6544+
6545+
if (n->child) {
6546+
idx = utils_strtol(n->child->value, 10, &rc);
6547+
if (rc || idx < 1 || idx >= MACRO_MAX_ARGS_NUM) {
6548+
node_fatal(rc, n, "Invalid macro arg index: %d", idx);
6549+
return 0;
6550+
}
6551+
//n->child = 0;
6552+
ret = INT_MAX;
6553+
idx--;
6554+
} else {
6555+
call->arg_idx++;
6556+
idx = call->arg_idx;
65436557
}
6544-
n->child = 0; // Do not traverse arg index
6545-
idx--;
6558+
call->arg_idx = idx;
65466559
n = call->args[idx];
65476560
if (!n) {
65486561
node_fatal(rc, call->n, "Call argument: %d for macro: %s is not set", (idx + 1), call->key);
@@ -6568,7 +6581,7 @@ static int _call_macro_visit(struct node *n, int lvl, void *d) {
65686581
c->next = nn;
65696582
}
65706583
ulist_push(&call->nodes, &nn);
6571-
return 0;
6584+
return ret;
65726585
}
65736586

65746587
static void _call_remove(struct node *n) {
@@ -6594,6 +6607,7 @@ static void _call_init(struct node *n) {
65946607
}
65956608
struct _call *call = xcalloc(1, sizeof(*call));
65966609
call->nn_idx = -1;
6610+
call->arg_idx = -1;
65976611
call->key = key;
65986612
call->mn = mn;
65996613
call->n = n;
@@ -6615,10 +6629,7 @@ static void _call_init(struct node *n) {
66156629

66166630
ulist_push(&call->nodes, &n->parent);
66176631
_call_remove(n);
6618-
int rc = node_visit(mn, 1, call, _call_macro_visit);
6619-
if (rc) {
6620-
node_fatal(rc, n, 0);
6621-
}
6632+
node_visit(mn, 1, call, _call_macro_visit);
66226633
for (int i = call->nn_idx; i < n->ctx->nodes.num; ++i) {
66236634
struct node *nn = *(struct node**) ulist_get(&n->ctx->nodes, i);
66246635
node_bind(nn);
@@ -8528,13 +8539,15 @@ static void _finish(struct _yycontext *yy) {
85288539

85298540
static int _node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct node*, int, void*)) {
85308541
int ret = visitor(n, lvl, ctx);
8531-
if (ret) {
8542+
if (ret && ret != NODE_VISIT_CHILD_SKIP) {
85328543
return ret;
85338544
}
8534-
for (struct node *c = n->child; c; c = c->next) {
8535-
ret = _node_visit(c, lvl + 1, ctx, visitor);
8536-
if (ret) {
8537-
return ret;
8545+
if (ret != NODE_VISIT_CHILD_SKIP) {
8546+
for (struct node *c = n->child; c; c = c->next) {
8547+
ret = _node_visit(c, lvl + 1, ctx, visitor);
8548+
if (ret) {
8549+
return ret;
8550+
}
85388551
}
85398552
}
85408553
return visitor(n, -lvl, ctx);

dist/build.sh

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# https://github.com/Softmotions/autark
77

88
META_VERSION=0.9.0
9-
META_REVISION=170d75f
9+
META_REVISION=69a4678
1010
cd "$(cd "$(dirname "$0")"; pwd -P)"
1111

1212
prev_arg=""
@@ -62,7 +62,7 @@ cat <<'a292effa503b' > ${AUTARK_HOME}/autark.c
6262
#ifndef CONFIG_H
6363
#define CONFIG_H
6464
#define META_VERSION "0.9.0"
65-
#define META_REVISION "170d75f"
65+
#define META_REVISION "69a4678"
6666
#endif
6767
#define _AMALGAMATE_
6868
#define _XOPEN_SOURCE 700
@@ -873,6 +873,7 @@ void node_product_add(struct node*, const char *prod, char pathbuf[PATH_MAX]);
873873
void node_product_add_raw(struct node*, const char *prod);
874874
void node_reset(struct node *n);
875875
const char* node_value(struct node *n);
876+
#define NODE_VISIT_CHILD_SKIP INT_MAX
876877
int node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct node*, int, void*));
877878
void node_module_setup(struct node *n, unsigned flags);
878879
void node_init(struct node *n);
@@ -5681,9 +5682,11 @@ struct _call {
56815682
struct ulist nodes; ///< Nodes stack. struct node*
56825683
struct node *args[MACRO_MAX_ARGS_NUM];
56835684
int nn_idx;
5685+
int arg_idx;
56845686
};
56855687
static int _call_macro_visit(struct node *n, int lvl, void *d) {
56865688
struct _call *call = d;
5689+
int ret = 0;
56875690
if (call->mn == n /*skip macro itself */ || call->mn->child == n /* skip macro name */) {
56885691
return 0;
56895692
}
@@ -5692,15 +5695,23 @@ static int _call_macro_visit(struct node *n, int lvl, void *d) {
56925695
return 0;
56935696
}
56945697
// Macro arg
5695-
if (n->value[0] == '&' && n->value[1] == '\0' && n->child && !n->child->next) {
5698+
if (n->value[0] == '&' && n->value[1] == '\0') {
5699+
int idx;
56965700
int rc = 0;
5697-
int idx = utils_strtol(n->child->value, 10, &rc);
5698-
if (rc || idx < 1 || idx >= MACRO_MAX_ARGS_NUM) {
5699-
node_fatal(rc, n, "Invalid macro arg index: %d", idx);
5700-
return 0;
5701+
if (n->child) {
5702+
idx = utils_strtol(n->child->value, 10, &rc);
5703+
if (rc || idx < 1 || idx >= MACRO_MAX_ARGS_NUM) {
5704+
node_fatal(rc, n, "Invalid macro arg index: %d", idx);
5705+
return 0;
5706+
}
5707+
//n->child = 0;
5708+
ret = INT_MAX;
5709+
idx--;
5710+
} else {
5711+
call->arg_idx++;
5712+
idx = call->arg_idx;
57015713
}
5702-
n->child = 0; // Do not traverse arg index
5703-
idx--;
5714+
call->arg_idx = idx;
57045715
n = call->args[idx];
57055716
if (!n) {
57065717
node_fatal(rc, call->n, "Call argument: %d for macro: %s is not set", (idx + 1), call->key);
@@ -5724,7 +5735,7 @@ static int _call_macro_visit(struct node *n, int lvl, void *d) {
57245735
c->next = nn;
57255736
}
57265737
ulist_push(&call->nodes, &nn);
5727-
return 0;
5738+
return ret;
57285739
}
57295740
static void _call_remove(struct node *n) {
57305741
struct node *prev = node_find_prev_sibling(n);
@@ -5748,6 +5759,7 @@ static void _call_init(struct node *n) {
57485759
}
57495760
struct _call *call = xcalloc(1, sizeof(*call));
57505761
call->nn_idx = -1;
5762+
call->arg_idx = -1;
57515763
call->key = key;
57525764
call->mn = mn;
57535765
call->n = n;
@@ -5767,10 +5779,7 @@ static void _call_init(struct node *n) {
57675779
}
57685780
ulist_push(&call->nodes, &n->parent);
57695781
_call_remove(n);
5770-
int rc = node_visit(mn, 1, call, _call_macro_visit);
5771-
if (rc) {
5772-
node_fatal(rc, n, 0);
5773-
}
5782+
node_visit(mn, 1, call, _call_macro_visit);
57745783
for (int i = call->nn_idx; i < n->ctx->nodes.num; ++i) {
57755784
struct node *nn = *(struct node**) ulist_get(&n->ctx->nodes, i);
57765785
node_bind(nn);
@@ -7530,13 +7539,15 @@ static void _finish(struct _yycontext *yy) {
75307539
}
75317540
static int _node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct node*, int, void*)) {
75327541
int ret = visitor(n, lvl, ctx);
7533-
if (ret) {
7542+
if (ret && ret != NODE_VISIT_CHILD_SKIP) {
75347543
return ret;
75357544
}
7536-
for (struct node *c = n->child; c; c = c->next) {
7537-
ret = _node_visit(c, lvl + 1, ctx, visitor);
7538-
if (ret) {
7539-
return ret;
7545+
if (ret != NODE_VISIT_CHILD_SKIP) {
7546+
for (struct node *c = n->child; c; c = c->next) {
7547+
ret = _node_visit(c, lvl + 1, ctx, visitor);
7548+
if (ret) {
7549+
return ret;
7550+
}
75407551
}
75417552
}
75427553
return visitor(n, -lvl, ctx);

node_call.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct _call {
2121

2222
static int _call_macro_visit(struct node *n, int lvl, void *d) {
2323
struct _call *call = d;
24+
int ret = 0;
2425

2526
if (call->mn == n /*skip macro itself */ || call->mn->child == n /* skip macro name */) {
2627
return 0;
@@ -33,16 +34,17 @@ static int _call_macro_visit(struct node *n, int lvl, void *d) {
3334

3435
// Macro arg
3536
if (n->value[0] == '&' && n->value[1] == '\0') {
36-
int rc = 0;
3737
int idx;
38+
int rc = 0;
3839

3940
if (n->child) {
4041
idx = utils_strtol(n->child->value, 10, &rc);
4142
if (rc || idx < 1 || idx >= MACRO_MAX_ARGS_NUM) {
4243
node_fatal(rc, n, "Invalid macro arg index: %d", idx);
4344
return 0;
4445
}
45-
n->child = 0;
46+
//n->child = 0;
47+
ret = INT_MAX;
4648
idx--;
4749
} else {
4850
call->arg_idx++;
@@ -74,7 +76,7 @@ static int _call_macro_visit(struct node *n, int lvl, void *d) {
7476
c->next = nn;
7577
}
7678
ulist_push(&call->nodes, &nn);
77-
return 0;
79+
return ret;
7880
}
7981

8082
static void _call_remove(struct node *n) {
@@ -122,10 +124,7 @@ static void _call_init(struct node *n) {
122124

123125
ulist_push(&call->nodes, &n->parent);
124126
_call_remove(n);
125-
int rc = node_visit(mn, 1, call, _call_macro_visit);
126-
if (rc) {
127-
node_fatal(rc, n, 0);
128-
}
127+
node_visit(mn, 1, call, _call_macro_visit);
129128
for (int i = call->nn_idx; i < n->ctx->nodes.num; ++i) {
130129
struct node *nn = *(struct node**) ulist_get(&n->ctx->nodes, i);
131130
node_bind(nn);

script.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,15 @@ static void _finish(struct _yycontext *yy) {
310310

311311
static int _node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct node*, int, void*)) {
312312
int ret = visitor(n, lvl, ctx);
313-
if (ret) {
313+
if (ret && ret != NODE_VISIT_CHILD_SKIP) {
314314
return ret;
315315
}
316-
for (struct node *c = n->child; c; c = c->next) {
317-
ret = _node_visit(c, lvl + 1, ctx, visitor);
318-
if (ret) {
319-
return ret;
316+
if (ret != NODE_VISIT_CHILD_SKIP) {
317+
for (struct node *c = n->child; c; c = c->next) {
318+
ret = _node_visit(c, lvl + 1, ctx, visitor);
319+
if (ret) {
320+
return ret;
321+
}
320322
}
321323
}
322324
return visitor(n, -lvl, ctx);

script.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ void node_reset(struct node *n);
136136

137137
const char* node_value(struct node *n);
138138

139+
#define NODE_VISIT_CHILD_SKIP INT_MAX
140+
139141
int node_visit(struct node *n, int lvl, void *ctx, int (*visitor)(struct node*, int, void*));
140142

141143
void node_module_setup(struct node *n, unsigned flags);

tests/data/test10/Autark

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
macro {
22
M_ECHO
3+
set {
4+
JOIN
5+
^{&{1} ' ' &{2}}
6+
}
37
echo { &{2} }
48
echo {
59
init {
610
&{1}
711
}
812
}
13+
echo { JOIN ${JOIN} }
914
}
1015

1116
call {
1217
M_ECHO
1318
'foo bar'
1419
baz
1520
}
21+
22+
call {
23+
M_ECHO
24+
'baz gaz'
25+
last
26+
}

tests/data/test10/Autark.dump

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
Autark:0x100 {
2+
set:0x4 {
3+
JOIN:0x1
4+
^:0x8 {
5+
foo bar:0x1
6+
:0x1
7+
baz:0x1
8+
}
9+
}
210
echo:0x200000 {
311
baz:0x1
412
}
@@ -7,4 +15,32 @@ Autark:0x100 {
715
foo bar:0x1
816
}
917
}
18+
echo:0x200000 {
19+
JOIN:0x1
20+
$:0x2 {
21+
JOIN:0x1
22+
}
23+
}
24+
set:0x4 {
25+
JOIN:0x1
26+
^:0x8 {
27+
baz gaz:0x1
28+
:0x1
29+
last:0x1
30+
}
31+
}
32+
echo:0x200000 {
33+
last:0x1
34+
}
35+
echo:0x200000 {
36+
init:0x200 {
37+
baz gaz:0x1
38+
}
39+
}
40+
echo:0x200000 {
41+
JOIN:0x1
42+
$:0x2 {
43+
JOIN:0x1
44+
}
45+
}
1046
}

0 commit comments

Comments
 (0)