baum

annotate actions.c @ 29:88a51653db83

removed binary from dist tarball; new debian package
author meillo@marmaro.de
date Fri, 22 Feb 2008 13:55:57 +0100
parents b62288419c1c
children cd979b979610
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 unsigned char action_blackhole(struct Node* node);
meillo@26 13
meillo@26 14
meillo@2 15
meillo@3 16 unsigned char action(struct Node* node) {
meillo@5 17 if (node == NULL) {
meillo@5 18 fprintf(stderr, "action of non existing node\n");
meillo@5 19 return 0;
meillo@5 20 }
meillo@5 21
meillo@26 22 logit(node->name);
meillo@26 23
meillo@2 24 if (strcmp(node->name, "print") == 0) {
meillo@2 25 return action_print(node);
meillo@3 26
meillo@2 27 } else if (strcmp(node->name, "sum") == 0) {
meillo@2 28 return action_sum(node);
meillo@3 29
meillo@2 30 } else if (strcmp(node->name, "number") == 0) {
meillo@2 31 return action_number(node);
meillo@3 32
meillo@3 33 } else if (strcmp(node->name, "input") == 0) {
meillo@3 34 return action_input(node);
meillo@3 35
meillo@5 36 } else if (strcmp(node->name, "times") == 0) {
meillo@5 37 return action_times(node);
meillo@5 38
meillo@5 39 } else if (strcmp(node->name, "blackhole") == 0) {
meillo@5 40 return action_blackhole(node);
meillo@5 41
meillo@2 42 } else {
meillo@2 43 fprintf(stderr, "unknown kind of node");
meillo@16 44 exit(4);
meillo@2 45 }
meillo@2 46 }
meillo@2 47
meillo@2 48
meillo@2 49
meillo@3 50 unsigned char action_print(struct Node* node) {
meillo@5 51 unsigned char result;
meillo@5 52 result = action(node->down);
meillo@5 53 if (node->value == 'c') {
meillo@5 54 printf("%c", result);
meillo@5 55 } else {
meillo@5 56 printf("%d", result);
meillo@5 57 }
meillo@5 58 return result;
meillo@2 59 }
meillo@2 60
meillo@2 61
meillo@3 62 unsigned char action_sum(struct Node* node) {
meillo@2 63 struct Node* tp;
meillo@2 64 tp = node->down;
meillo@2 65 while (tp != NULL) {
meillo@2 66 node->value += action(tp);
meillo@2 67 tp = tp->right;
meillo@2 68 }
meillo@2 69 return node->value;
meillo@2 70 }
meillo@2 71
meillo@2 72
meillo@3 73 unsigned char action_number(struct Node* node) {
meillo@2 74 return node->value;
meillo@2 75 }
meillo@2 76
meillo@3 77
meillo@3 78 unsigned char action_input(struct Node* node) {
meillo@5 79 /* reads a number which is treated as ASCII value */
meillo@5 80 int input;
meillo@14 81 printf("input: ");
meillo@5 82 scanf("%d", &input);
meillo@5 83 input = input % 256;
meillo@5 84 insertLast(node, newNode("number", (char) input));
meillo@5 85
meillo@3 86 return 0;
meillo@3 87 }
meillo@3 88
meillo@3 89
meillo@5 90 unsigned char action_times(struct Node* node) {
meillo@5 91 struct Node* tp;
meillo@15 92 struct Node* last;
meillo@5 93 unsigned char i;
meillo@5 94 tp = node->down;
meillo@10 95 for (i = 0; i < node->value; i++) {
meillo@5 96 /* deep copy */
meillo@15 97 last = insertLast(node, newNode(tp->name, tp->value));
meillo@15 98 if (tp->down != NULL) {
meillo@15 99 last->down = newNode(tp->down->name, tp->down->value);
meillo@15 100 }
meillo@15 101
meillo@5 102 }
meillo@5 103 return 0;
meillo@5 104 }
meillo@5 105
meillo@5 106
meillo@5 107 unsigned char action_blackhole(struct Node* node) {
meillo@5 108 action(node->down);
meillo@5 109 return 0;
meillo@5 110 }
meillo@5 111
meillo@5 112