baum

annotate actions.c @ 6:ab87b154a96b

first tries for the read input function
author meillo@marmaro.de
date Fri, 08 Feb 2008 21:44:07 +0100
parents 15d7d6b9766f
children 0e15841ae111
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@2 40 exit(1);
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 /*
meillo@3 76 unsigned char input = (unsigned char) getchar();
meillo@3 77 getchar(); /* catches the newline */
meillo@5 78
meillo@5 79 /* reads a number which is treated as ASCII value */
meillo@5 80 int input;
meillo@5 81 scanf("%d", &input);
meillo@5 82 input = input % 256;
meillo@5 83 insertLast(node, newNode("number", (char) input));
meillo@5 84
meillo@3 85 return 0;
meillo@3 86 }
meillo@3 87
meillo@3 88
meillo@5 89 unsigned char action_times(struct Node* node) {
meillo@5 90 struct Node* tp;
meillo@5 91 unsigned char i;
meillo@5 92 tp = node->down;
meillo@5 93 for (i; i < node->value; i++) {
meillo@5 94 /* deep copy */
meillo@5 95 }
meillo@5 96 return 0;
meillo@5 97 }
meillo@5 98
meillo@5 99
meillo@5 100 unsigned char action_blackhole(struct Node* node) {
meillo@5 101 action(node->down);
meillo@5 102 return 0;
meillo@5 103 }
meillo@5 104
meillo@5 105