baum

annotate actions.c @ 36:b11ac43f3917

removed node blackhole and substituted by node number now executing its child
author meillo@marmaro.de
date Sat, 01 Mar 2008 17:51:25 +0100
parents 4e60d96265f0
children ff01f0f076e4
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@5 17 fprintf(stderr, "action of non existing node\n");
meillo@5 18 return 0;
meillo@5 19 }
meillo@5 20
meillo@26 21 logit(node->name);
meillo@26 22
meillo@2 23 if (strcmp(node->name, "print") == 0) {
meillo@2 24 return action_print(node);
meillo@3 25
meillo@2 26 } else if (strcmp(node->name, "sum") == 0) {
meillo@2 27 return action_sum(node);
meillo@3 28
meillo@2 29 } else if (strcmp(node->name, "number") == 0) {
meillo@2 30 return action_number(node);
meillo@3 31
meillo@3 32 } else if (strcmp(node->name, "input") == 0) {
meillo@3 33 return action_input(node);
meillo@3 34
meillo@5 35 } else if (strcmp(node->name, "times") == 0) {
meillo@5 36 return action_times(node);
meillo@5 37
meillo@2 38 } else {
meillo@31 39 fprintf(stderr, "unknown kind of node\n");
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@36 70 action(node->down);
meillo@2 71 return node->value;
meillo@2 72 }
meillo@2 73
meillo@3 74
meillo@3 75 unsigned char action_input(struct Node* node) {
meillo@5 76 /* reads a number which is treated as ASCII value */
meillo@5 77 int input;
meillo@14 78 printf("input: ");
meillo@5 79 scanf("%d", &input);
meillo@5 80 input = input % 256;
meillo@5 81 insertLast(node, newNode("number", (char) input));
meillo@5 82
meillo@3 83 return 0;
meillo@3 84 }
meillo@3 85
meillo@3 86
meillo@5 87 unsigned char action_times(struct Node* node) {
meillo@5 88 struct Node* tp;
meillo@15 89 struct Node* last;
meillo@5 90 unsigned char i;
meillo@5 91 tp = node->down;
meillo@10 92 for (i = 0; i < node->value; i++) {
meillo@30 93 /* FIXME deep copy */
meillo@15 94 last = insertLast(node, newNode(tp->name, tp->value));
meillo@15 95 if (tp->down != NULL) {
meillo@15 96 last->down = newNode(tp->down->name, tp->down->value);
meillo@15 97 }
meillo@15 98
meillo@5 99 }
meillo@5 100 return 0;
meillo@5 101 }