I try to write a wrapper for printf in C++. Of course i found va_list
but dont know how its applicable to me because
the wrapper will not be called directly. I will show later on.
I parse a script that includes a function with an unknown number of arguments like
ASTNode node(Token(PRINT, PRINT));
consume(PRINT);
consume(LPAREN);
node.make_child(variable()); // <-- formatstring (node.child[int])
while(current_token._type() != RPAREN) {
consume(COMMA);
node.make_child(variable()); // <-- the values to replace in formatstring (node.child[int++])
i++;
}
consume(RPAREN);
return node;
The first will be the formatstring and the others will be the values to replace in the format string so the function where i execute it will look like
if(node._token()._type() == PRINT) {
Token formatstring = visit(*node.child[0]);
Token temp;
int i = 1;
while(i < node.child.size()) {
visit(*node.child[i++]); // <-- the values to replace in formatstring
}
}
and doesnt take any "real" parameter. How can i build a dynamic parameter array using va_list or another method?
Thank you
Edit It seems my question is unclear to someone..
printf is called like printf(formatstring, param1, param2, param3...)
and i want to do build the parameters passed after the first parameter (formatstring) in a loop like
// Pseudocode
out("printf(");
out($myformatstring);
int i = 1;
while(i<parameter_count) {
out(parameter[i++]);
out(",");
}
out(")");