baum
view actions.c @ 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 |
parents | 6279e5b14d9e |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "baum.h"
6 unsigned char action_print(struct Node* node);
7 unsigned char action_sum(struct Node* node);
8 unsigned char action_number(struct Node* node);
9 unsigned char action_create(struct Node* node);
10 unsigned char action_times(struct Node* node);
11 unsigned char action_if(struct Node* node);
12 unsigned char action_while(struct Node* node);
14 struct action {
15 char* name;
16 unsigned char (*func)(struct Node* node);
17 };
19 struct action actions[] = {
20 {"print", action_print},
21 {"sum", action_sum},
22 {"number", action_number},
23 {"create", action_create},
24 {"times", action_times},
25 {"if", action_if},
26 {"while", action_while},
27 };
31 unsigned char action(struct Node* node) {
32 int i;
34 if (node == NULL) {
35 if (option_verbose) {
36 fprintf(stderr, "warning: action of non existing node\n");
37 }
38 return 0;
39 }
41 for (i = 0; i < (sizeof(actions)/sizeof(actions[0])); i++) {
42 if (strcmp(actions[i].name, node->name) == 0) {
43 return (actions[i].func)(node);
44 }
45 }
47 fprintf(stderr, "unknown kind of node\n");
48 exit(4);
49 }
53 unsigned char action_print(struct Node* node) {
54 unsigned char result;
55 result = action(node->down);
56 if (node->value == 'c') {
57 printf("%c", result);
58 } else {
59 printf("%d", result);
60 }
61 return result;
62 }
65 unsigned char action_sum(struct Node* node) {
66 struct Node* tp;
68 node->value = 0;
69 for (tp = node->down; tp != NULL; tp = tp->right) {
70 node->value += action(tp);
71 }
72 return node->value;
73 }
76 unsigned char action_number(struct Node* node) {
77 if (node->down != NULL) {
78 action(node->down);
79 }
80 return node->value;
81 }
84 unsigned char action_create(struct Node* node) {
85 int input;
87 if (node->down == NULL) {
88 /* user input */
89 printf("input: ");
90 scanf("%d", &input);
91 input = input % 256;
92 } else {
93 /* return value */
94 input = action(node->down);
95 }
96 insertLast(node, newNode("number", (char) input));
97 return 0;
98 }
101 unsigned char action_times(struct Node* node) {
102 unsigned char i;
104 for (i = 0; i < node->value; i++) {
105 insertLast(node, copyTree(node->down));
106 }
107 return 0;
108 }
111 unsigned char action_if(struct Node* node) {
112 int result = 0;
113 char return1;
114 char return2;
116 if (node->down == NULL || node->down->right == NULL) {
117 fprintf(stderr, "error: while requires two sons");
118 exit(7);
119 }
121 return1 = action(node->down);
122 return2 = action(node->down->right);
124 if (node->value == '!') { /* 33 */
125 if (return1 != return2) {
126 result = 1;
127 }
128 } else if (node->value == '<') { /* 60 */
129 if (return1 < return2) {
130 result = 1;
131 }
132 } else if (node->value == '>') { /* 62 */
133 if (return1 > return2) {
134 result = 1;
135 }
136 } else {
137 if (return1 == return2) {
138 result = 1;
139 }
140 }
141 return result;
142 }
145 unsigned char action_while(struct Node* node) {
146 if (node->down == NULL || node->down->right == NULL) {
147 fprintf(stderr, "error: while requires two sons");
148 exit(7);
149 }
150 while (action(node->down)) {
151 action(node->down->right);
152 }
153 return 0;
154 }