# HG changeset patch # User meillo@marmaro.de # Date 1214378416 -7200 # Node ID 21ff1783f6406b61db7237e264f9d8fdfbdf5318 # Parent 7adeee76ce3e24c4ab12f16d48ed4eab09a7daed using function pointers now, instead of a large if-else diff -r 7adeee76ce3e -r 21ff1783f640 actions.c --- a/actions.c Sun Mar 16 20:11:02 2008 +0100 +++ b/actions.c Wed Jun 25 09:20:16 2008 +0200 @@ -11,9 +11,26 @@ unsigned char action_if(struct Node* node); unsigned char action_while(struct Node* node); +struct action { + char* name; + unsigned char (*func)(struct Node* node); +}; + +struct action actions[] = { + {"print", action_print}, + {"sum", action_sum}, + {"number", action_number}, + {"create", action_create}, + {"times", action_times}, + {"if", action_if}, + {"while", action_while}, +}; + unsigned char action(struct Node* node) { + int i; + if (node == NULL) { if (option_verbose) { fprintf(stderr, "warning: action of non existing node\n"); @@ -21,31 +38,14 @@ return 0; } - if (strcmp(node->name, "print") == 0) { - return action_print(node); + for (i = 0; i < (sizeof(actions)/sizeof(actions[0])); i++) { + if (strcmp(actions[i].name, node->name) == 0) { + return (actions[i].func)(node); + } + } - } else if (strcmp(node->name, "sum") == 0) { - return action_sum(node); - - } else if (strcmp(node->name, "number") == 0) { - return action_number(node); - - } else if (strcmp(node->name, "create") == 0) { - return action_create(node); - - } else if (strcmp(node->name, "times") == 0) { - return action_times(node); - - } else if (strcmp(node->name, "if") == 0) { - return action_if(node); - - } else if (strcmp(node->name, "while") == 0) { - return action_while(node); - - } else { fprintf(stderr, "unknown kind of node\n"); exit(4); - } }