Suppose I'm writing a simple parser. It has a dispatcher, which calls the corresponding parsing functions depending on the type of the input expression.
def dispatcher(expression):
m = pattern1.match(expression):
if m is not None:
handle_type1(expression, m)
# ... other types
My question is, is there anyway to combine the matching and checking for None
? I mean, something like the following C code:
void dispatcher(char *expression)
{
if ((m = pattern1.match(expression)) != NULL) {
// ... handle expression type 1
}
else if ((m = pattern2.match(expression)) != NULL) {
// ... handle expression type 2
}
// ... other cases
}
Thanks!