baum

annotate actions.c @ 21:ca56c1b3fb67

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