meillo@2: #include meillo@2: #include meillo@2: #include meillo@2: #include "baum.h" meillo@2: #include "actions.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@26: unsigned char action_blackhole(struct Node* node); meillo@26: meillo@26: meillo@2: meillo@3: unsigned char action(struct Node* node) { meillo@5: if (node == NULL) { meillo@5: fprintf(stderr, "action of non existing node\n"); meillo@5: return 0; meillo@5: } meillo@5: meillo@26: logit(node->name); meillo@26: 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@5: } else if (strcmp(node->name, "blackhole") == 0) { meillo@5: return action_blackhole(node); meillo@5: 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@2: return node->value; meillo@2: } meillo@2: meillo@3: meillo@3: unsigned char action_input(struct Node* node) { meillo@5: /* reads a number which is treated as ASCII value */ 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@5: meillo@3: return 0; meillo@3: } meillo@3: meillo@3: meillo@5: unsigned char action_times(struct Node* node) { meillo@5: struct Node* tp; meillo@15: struct Node* last; meillo@5: unsigned char i; meillo@5: tp = node->down; meillo@10: for (i = 0; i < node->value; i++) { meillo@30: /* FIXME deep copy */ meillo@15: last = insertLast(node, newNode(tp->name, tp->value)); meillo@15: if (tp->down != NULL) { meillo@15: last->down = newNode(tp->down->name, tp->down->value); meillo@15: } meillo@15: meillo@5: } meillo@5: return 0; meillo@5: } meillo@5: meillo@5: meillo@5: unsigned char action_blackhole(struct Node* node) { meillo@5: action(node->down); meillo@5: return 0; meillo@5: } meillo@5: meillo@5: