meillo@2: #include meillo@2: #include meillo@2: #include meillo@2: #include "baum.h" meillo@2: meillo@26: unsigned char action_print(struct Node* node); meillo@26: unsigned char action_sum(struct Node* node); meillo@26: unsigned char action_number(struct Node* node); meillo@26: unsigned char action_input(struct Node* node); meillo@26: unsigned char action_times(struct Node* node); meillo@50: unsigned char action_if(struct Node* node); meillo@50: unsigned char action_while(struct Node* node); meillo@26: meillo@26: meillo@2: meillo@3: unsigned char action(struct Node* node) { meillo@5: if (node == NULL) { meillo@38: if (option_verbose) { meillo@38: fprintf(stderr, "warning: action of non existing node\n"); meillo@38: } meillo@5: return 0; meillo@5: } meillo@5: meillo@2: if (strcmp(node->name, "print") == 0) { meillo@2: return action_print(node); meillo@3: meillo@2: } else if (strcmp(node->name, "sum") == 0) { meillo@2: return action_sum(node); meillo@3: meillo@2: } else if (strcmp(node->name, "number") == 0) { meillo@2: return action_number(node); meillo@3: meillo@3: } else if (strcmp(node->name, "input") == 0) { meillo@3: return action_input(node); meillo@3: meillo@5: } else if (strcmp(node->name, "times") == 0) { meillo@5: return action_times(node); meillo@5: meillo@50: } else if (strcmp(node->name, "if") == 0) { meillo@50: return action_if(node); meillo@50: meillo@50: } else if (strcmp(node->name, "while") == 0) { meillo@50: return action_while(node); meillo@50: meillo@2: } else { meillo@31: fprintf(stderr, "unknown kind of node\n"); meillo@16: exit(4); meillo@2: } meillo@2: } meillo@2: meillo@2: meillo@2: meillo@3: unsigned char action_print(struct Node* node) { meillo@5: unsigned char result; meillo@5: result = action(node->down); meillo@5: if (node->value == 'c') { meillo@5: printf("%c", result); meillo@5: } else { meillo@5: printf("%d", result); meillo@5: } meillo@5: return result; meillo@2: } meillo@2: meillo@2: meillo@3: unsigned char action_sum(struct Node* node) { meillo@2: struct Node* tp; meillo@2: tp = node->down; meillo@2: while (tp != NULL) { meillo@2: node->value += action(tp); meillo@2: tp = tp->right; meillo@2: } meillo@2: return node->value; meillo@2: } meillo@2: meillo@2: meillo@3: unsigned char action_number(struct Node* node) { meillo@38: if (node->down != NULL) { meillo@38: action(node->down); meillo@38: } meillo@2: return node->value; meillo@2: } meillo@2: meillo@3: meillo@3: unsigned char action_input(struct Node* node) { meillo@5: int input; meillo@14: printf("input: "); meillo@5: scanf("%d", &input); meillo@5: input = input % 256; meillo@5: insertLast(node, newNode("number", (char) input)); meillo@3: return 0; meillo@3: } meillo@3: meillo@3: meillo@5: unsigned char action_times(struct Node* node) { meillo@5: unsigned char i; meillo@10: for (i = 0; i < node->value; i++) { meillo@46: insertLast(node, copyTree(node->down)); meillo@5: } meillo@5: return 0; meillo@5: } meillo@50: meillo@50: meillo@50: unsigned char action_if(struct Node* node) { meillo@50: int result = 0; meillo@50: char return1; meillo@50: char return2; meillo@50: meillo@50: if (node->down == NULL || node->down->right == NULL) { meillo@50: fprintf(stderr, "error: while requires two sons"); meillo@50: exit(1); /* FIXME other error code */ meillo@50: } meillo@50: meillo@50: return1 = action(node->down); meillo@50: return2 = action(node->down->right); meillo@50: meillo@50: if (node->value == '!') { /* 33 */ meillo@50: if (return1 != return2) { meillo@50: result = 1; meillo@50: } meillo@50: } else if (node->value == '<') { /* 60 */ meillo@50: if (return1 < return2) { meillo@50: result = 1; meillo@50: } meillo@50: } else if (node->value == '>') { /* 62 */ meillo@50: if (return1 > return2) { meillo@50: result = 1; meillo@50: } meillo@50: } else { meillo@50: if (return1 == return2) { meillo@50: result = 1; meillo@50: } meillo@50: } meillo@50: return result; meillo@50: } meillo@50: meillo@50: meillo@50: unsigned char action_while(struct Node* node) { meillo@50: if (node->down == NULL || node->down->right == NULL) { meillo@50: fprintf(stderr, "error: while requires two sons"); meillo@50: exit(1); /* FIXME other error code */ meillo@50: } meillo@50: while (action(node->down)) { meillo@50: action(node->down->right); meillo@50: } meillo@50: return 0; meillo@50: }