changeset 57:21ff1783f640

using function pointers now, instead of a large if-else
author meillo@marmaro.de
date Wed, 25 Jun 2008 09:20:16 +0200 (2008-06-25)
parents 7adeee76ce3e
children f5f06d6f62b3
files actions.c
diffstat 1 files changed, 22 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- 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);
-
-	} else if (strcmp(node->name, "sum") == 0) {
-		return action_sum(node);
-
-	} else if (strcmp(node->name, "number") == 0) {
-		return action_number(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, "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);
-	}
 }