commit a9e9e3f7b4914ae1af50907a0b6af4e61ae518d4 from: Omar Polo date: Sat Aug 07 21:13:25 2021 UTC add special instruction `vargs': yields the number of varargs commit - c31f55f534c112abd2319b8b0de79878b39b775d commit + a9e9e3f7b4914ae1af50907a0b6af4e61ae518d4 blob - 2dea90aca8c47690f4e37ac1c1dcdce3cf81d43e blob + e2e994d9ceb6b0d5ddb002d96408cf19daea148d --- script.c +++ script.c @@ -165,6 +165,16 @@ static inline void pushbool(int n) { pushv(n ? &v_true : &v_false); +} + +static inline void +pushnum(int64_t n) +{ + struct value v; + + v.type = V_NUM; + v.v.num = n; + pushv(&v); } static inline struct opstack * @@ -494,7 +504,17 @@ op_sfail(struct op *expr, char *msg) op = newop(OP_SFAIL); op->v.sfail.expr = expr; op->v.sfail.msg = msg; + + return op; +} + +struct op * +op_vargs(void) +{ + struct op *op; + op = newop(OP_VARGS); + return op; } @@ -782,6 +802,9 @@ pp_op(struct op *op) if (op->v.sfail.msg != NULL) printf(": \"%s\"", op->v.sfail.msg); break; + case OP_VARGS: + printf("vargs"); + break; default: printf(" ???[%d] ", op->type); } @@ -975,6 +998,15 @@ eval(struct op *op) return ret; break; + case OP_VARGS: + if ((ret = getvar_raw("...", &t)) == EVAL_OK) { + for (i = 0; t != NULL; t = t->next) + i++; + pushnum(i); + } else + pushnum(0); + break; + default: before_printing(); fprintf(stderr, "invalid op, aborting.\n");