baum
diff 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 |
line diff
1.1 --- a/actions.c Sun Mar 02 13:35:42 2008 +0100 1.2 +++ b/actions.c Sun Mar 02 15:54:35 2008 +0100 1.3 @@ -8,6 +8,8 @@ 1.4 unsigned char action_number(struct Node* node); 1.5 unsigned char action_input(struct Node* node); 1.6 unsigned char action_times(struct Node* node); 1.7 +unsigned char action_if(struct Node* node); 1.8 +unsigned char action_while(struct Node* node); 1.9 1.10 1.11 1.12 @@ -34,6 +36,12 @@ 1.13 } else if (strcmp(node->name, "times") == 0) { 1.14 return action_times(node); 1.15 1.16 + } else if (strcmp(node->name, "if") == 0) { 1.17 + return action_if(node); 1.18 + 1.19 + } else if (strcmp(node->name, "while") == 0) { 1.20 + return action_while(node); 1.21 + 1.22 } else { 1.23 fprintf(stderr, "unknown kind of node\n"); 1.24 exit(4); 1.25 @@ -90,3 +98,49 @@ 1.26 } 1.27 return 0; 1.28 } 1.29 + 1.30 + 1.31 +unsigned char action_if(struct Node* node) { 1.32 + int result = 0; 1.33 + char return1; 1.34 + char return2; 1.35 + 1.36 + if (node->down == NULL || node->down->right == NULL) { 1.37 + fprintf(stderr, "error: while requires two sons"); 1.38 + exit(1); /* FIXME other error code */ 1.39 + } 1.40 + 1.41 + return1 = action(node->down); 1.42 + return2 = action(node->down->right); 1.43 + 1.44 + if (node->value == '!') { /* 33 */ 1.45 + if (return1 != return2) { 1.46 + result = 1; 1.47 + } 1.48 + } else if (node->value == '<') { /* 60 */ 1.49 + if (return1 < return2) { 1.50 + result = 1; 1.51 + } 1.52 + } else if (node->value == '>') { /* 62 */ 1.53 + if (return1 > return2) { 1.54 + result = 1; 1.55 + } 1.56 + } else { 1.57 + if (return1 == return2) { 1.58 + result = 1; 1.59 + } 1.60 + } 1.61 + return result; 1.62 +} 1.63 + 1.64 + 1.65 +unsigned char action_while(struct Node* node) { 1.66 + if (node->down == NULL || node->down->right == NULL) { 1.67 + fprintf(stderr, "error: while requires two sons"); 1.68 + exit(1); /* FIXME other error code */ 1.69 + } 1.70 + while (action(node->down)) { 1.71 + action(node->down->right); 1.72 + } 1.73 + return 0; 1.74 +}