baum

annotate actions.c @ 40:5c13e29bc6fd

removed debug output of read_input
author meillo@marmaro.de
date Sat, 01 Mar 2008 20:15:20 +0100
parents b11ac43f3917
children 1ad3d7305e5d
rev   line source
meillo@2 1 #include <stdio.h>
meillo@2 2 #include <stdlib.h>
meillo@2 3 #include <string.h>
meillo@2 4 #include "baum.h"
meillo@2 5 #include "actions.h"
meillo@2 6
meillo@26 7 unsigned char action_print(struct Node* node);
meillo@26 8 unsigned char action_sum(struct Node* node);
meillo@26 9 unsigned char action_number(struct Node* node);
meillo@26 10 unsigned char action_input(struct Node* node);
meillo@26 11 unsigned char action_times(struct Node* node);
meillo@26 12
meillo@26 13
meillo@2 14
meillo@3 15 unsigned char action(struct Node* node) {
meillo@5 16 if (node == NULL) {
meillo@38 17 if (option_verbose) {
meillo@38 18 fprintf(stderr, "warning: action of non existing node\n");
meillo@38 19 }
meillo@5 20 return 0;
meillo@5 21 }
meillo@5 22
meillo@26 23 logit(node->name);
meillo@26 24
meillo@2 25 if (strcmp(node->name, "print") == 0) {
meillo@2 26 return action_print(node);
meillo@3 27
meillo@2 28 } else if (strcmp(node->name, "sum") == 0) {
meillo@2 29 return action_sum(node);
meillo@3 30
meillo@2 31 } else if (strcmp(node->name, "number") == 0) {
meillo@2 32 return action_number(node);
meillo@3 33
meillo@3 34 } else if (strcmp(node->name, "input") == 0) {
meillo@3 35 return action_input(node);
meillo@3 36
meillo@5 37 } else if (strcmp(node->name, "times") == 0) {
meillo@5 38 return action_times(node);
meillo@5 39
meillo@2 40 } else {
meillo@31 41 fprintf(stderr, "unknown kind of node\n");
meillo@16 42 exit(4);
meillo@2 43 }
meillo@2 44 }
meillo@2 45
meillo@2 46
meillo@2 47
meillo@3 48 unsigned char action_print(struct Node* node) {
meillo@5 49 unsigned char result;
meillo@5 50 result = action(node->down);
meillo@5 51 if (node->value == 'c') {
meillo@5 52 printf("%c", result);
meillo@5 53 } else {
meillo@5 54 printf("%d", result);
meillo@5 55 }
meillo@5 56 return result;
meillo@2 57 }
meillo@2 58
meillo@2 59
meillo@3 60 unsigned char action_sum(struct Node* node) {
meillo@2 61 struct Node* tp;
meillo@2 62 tp = node->down;
meillo@2 63 while (tp != NULL) {
meillo@2 64 node->value += action(tp);
meillo@2 65 tp = tp->right;
meillo@2 66 }
meillo@2 67 return node->value;
meillo@2 68 }
meillo@2 69
meillo@2 70
meillo@3 71 unsigned char action_number(struct Node* node) {
meillo@38 72 if (node->down != NULL) {
meillo@38 73 action(node->down);
meillo@38 74 }
meillo@2 75 return node->value;
meillo@2 76 }
meillo@2 77
meillo@3 78
meillo@3 79 unsigned char action_input(struct Node* node) {
meillo@5 80 /* reads a number which is treated as ASCII value */
meillo@5 81 int input;
meillo@14 82 printf("input: ");
meillo@5 83 scanf("%d", &input);
meillo@5 84 input = input % 256;
meillo@5 85 insertLast(node, newNode("number", (char) input));
meillo@5 86
meillo@3 87 return 0;
meillo@3 88 }
meillo@3 89
meillo@3 90
meillo@5 91 unsigned char action_times(struct Node* node) {
meillo@5 92 struct Node* tp;
meillo@15 93 struct Node* last;
meillo@5 94 unsigned char i;
meillo@5 95 tp = node->down;
meillo@10 96 for (i = 0; i < node->value; i++) {
meillo@30 97 /* FIXME deep copy */
meillo@15 98 last = insertLast(node, newNode(tp->name, tp->value));
meillo@15 99 if (tp->down != NULL) {
meillo@15 100 last->down = newNode(tp->down->name, tp->down->value);
meillo@15 101 }
meillo@15 102
meillo@5 103 }
meillo@5 104 return 0;
meillo@5 105 }