comparison actions.c @ 50:0870e261bf28

added first implementation of "if" and "while" (but they are not perfect yet)
author meillo@marmaro.de
date Sun, 02 Mar 2008 15:54:35 +0100
parents f9fc4c4f9e3d
children 7d7abe88e71b
comparison
equal deleted inserted replaced
49:00de718c8590 50:0870e261bf28
6 unsigned char action_print(struct Node* node); 6 unsigned char action_print(struct Node* node);
7 unsigned char action_sum(struct Node* node); 7 unsigned char action_sum(struct Node* node);
8 unsigned char action_number(struct Node* node); 8 unsigned char action_number(struct Node* node);
9 unsigned char action_input(struct Node* node); 9 unsigned char action_input(struct Node* node);
10 unsigned char action_times(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);
11 13
12 14
13 15
14 unsigned char action(struct Node* node) { 16 unsigned char action(struct Node* node) {
15 if (node == NULL) { 17 if (node == NULL) {
31 } else if (strcmp(node->name, "input") == 0) { 33 } else if (strcmp(node->name, "input") == 0) {
32 return action_input(node); 34 return action_input(node);
33 35
34 } else if (strcmp(node->name, "times") == 0) { 36 } else if (strcmp(node->name, "times") == 0) {
35 return action_times(node); 37 return action_times(node);
38
39 } else if (strcmp(node->name, "if") == 0) {
40 return action_if(node);
41
42 } else if (strcmp(node->name, "while") == 0) {
43 return action_while(node);
36 44
37 } else { 45 } else {
38 fprintf(stderr, "unknown kind of node\n"); 46 fprintf(stderr, "unknown kind of node\n");
39 exit(4); 47 exit(4);
40 } 48 }
88 for (i = 0; i < node->value; i++) { 96 for (i = 0; i < node->value; i++) {
89 insertLast(node, copyTree(node->down)); 97 insertLast(node, copyTree(node->down));
90 } 98 }
91 return 0; 99 return 0;
92 } 100 }
101
102
103 unsigned char action_if(struct Node* node) {
104 int result = 0;
105 char return1;
106 char return2;
107
108 if (node->down == NULL || node->down->right == NULL) {
109 fprintf(stderr, "error: while requires two sons");
110 exit(1); /* FIXME other error code */
111 }
112
113 return1 = action(node->down);
114 return2 = action(node->down->right);
115
116 if (node->value == '!') { /* 33 */
117 if (return1 != return2) {
118 result = 1;
119 }
120 } else if (node->value == '<') { /* 60 */
121 if (return1 < return2) {
122 result = 1;
123 }
124 } else if (node->value == '>') { /* 62 */
125 if (return1 > return2) {
126 result = 1;
127 }
128 } else {
129 if (return1 == return2) {
130 result = 1;
131 }
132 }
133 return result;
134 }
135
136
137 unsigned char action_while(struct Node* node) {
138 if (node->down == NULL || node->down->right == NULL) {
139 fprintf(stderr, "error: while requires two sons");
140 exit(1); /* FIXME other error code */
141 }
142 while (action(node->down)) {
143 action(node->down->right);
144 }
145 return 0;
146 }