static int count(struct cstate *g, Renode *node, int depth)
{
	int min, max, n;
	if (!node) return 0;
	if (++depth > REG_MAXREC) die(g, "stack overflow");
	switch (node->type) {
	default: return 1;
	case P_CAT: return count(g, node->x, depth) + count(g, node->y, depth);
	case P_ALT: return count(g, node->x, depth) + count(g, node->y, depth) + 2;
	case P_REP:
		min = node->m;
		max = node->n;
		if (min == max) n = count(g, node->x, depth) * min;
		else if (max < REPINF) n = count(g, node->x, depth) * max + (max - min);
		else n = count(g, node->x, depth) * (min + 1) + 2;
		if (n < 0 || n > REG_MAXPROG) die(g, "program too large");
		return n;
	case P_PAR: return count(g, node->x, depth) + 2;
	case P_PLA: return count(g, node->x, depth) + 2;
	case P_NLA: return count(g, node->x, depth) + 2;
	}
}
static int count(struct cstate *g, Renode *node, int depth)
{
	int min, max, n;
	if (!node) return 0;
	if (++depth > REG_MAXREC) die(g, "stack overflow");
	switch (node->type) {
	default: return 1;
	case P_CAT: return count(g, node->x, depth) + count(g, node->y, depth);
	case P_ALT: return count(g, node->x, depth) + count(g, node->y, depth) + 2;
	case P_REP:
		min = node->m;
		max = node->n;
		if (min == max) n = count(g, node->x, depth) * min;
		else if (max < REPINF) n = count(g, node->x, depth) * max + (max - min);
		else n = count(g, node->x, depth) * (min + 1) + 2;
		if (n < 0 || n > REG_MAXPROG) die(g, "program too large");
		return n;
	case P_PAR: return count(g, node->x, depth) + 2;
	case P_PLA: return count(g, node->x, depth) + 2;
	case P_NLA: return count(g, node->x, depth) + 2;
	}
}