baum

annotate actions.c @ 54:6e46b106c334

updated examples to "create" instead of "input"
author meillo@marmaro.de
date Sun, 02 Mar 2008 16:43:07 +0100
parents 201b4603671a
children 6279e5b14d9e
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
meillo@26 6 unsigned char action_print(struct Node* node);
meillo@26 7 unsigned char action_sum(struct Node* node);
meillo@26 8 unsigned char action_number(struct Node* node);
meillo@51 9 unsigned char action_create(struct Node* node);
meillo@26 10 unsigned char action_times(struct Node* node);
meillo@50 11 unsigned char action_if(struct Node* node);
meillo@50 12 unsigned char action_while(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@38 18 if (option_verbose) {
meillo@38 19 fprintf(stderr, "warning: action of non existing node\n");
meillo@38 20 }
meillo@5 21 return 0;
meillo@5 22 }
meillo@5 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@51 33 } else if (strcmp(node->name, "create") == 0) {
meillo@51 34 return action_create(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@50 39 } else if (strcmp(node->name, "if") == 0) {
meillo@50 40 return action_if(node);
meillo@50 41
meillo@50 42 } else if (strcmp(node->name, "while") == 0) {
meillo@50 43 return action_while(node);
meillo@50 44
meillo@2 45 } else {
meillo@31 46 fprintf(stderr, "unknown kind of node\n");
meillo@16 47 exit(4);
meillo@2 48 }
meillo@2 49 }
meillo@2 50
meillo@2 51
meillo@2 52
meillo@3 53 unsigned char action_print(struct Node* node) {
meillo@5 54 unsigned char result;
meillo@5 55 result = action(node->down);
meillo@5 56 if (node->value == 'c') {
meillo@5 57 printf("%c", result);
meillo@5 58 } else {
meillo@5 59 printf("%d", result);
meillo@5 60 }
meillo@5 61 return result;
meillo@2 62 }
meillo@2 63
meillo@2 64
meillo@3 65 unsigned char action_sum(struct Node* node) {
meillo@2 66 struct Node* tp;
meillo@53 67
meillo@53 68 node->value = 0;
meillo@2 69 tp = node->down;
meillo@2 70 while (tp != NULL) {
meillo@2 71 node->value += action(tp);
meillo@2 72 tp = tp->right;
meillo@2 73 }
meillo@2 74 return node->value;
meillo@2 75 }
meillo@2 76
meillo@2 77
meillo@3 78 unsigned char action_number(struct Node* node) {
meillo@38 79 if (node->down != NULL) {
meillo@38 80 action(node->down);
meillo@38 81 }
meillo@2 82 return node->value;
meillo@2 83 }
meillo@2 84
meillo@3 85
meillo@51 86 unsigned char action_create(struct Node* node) {
meillo@5 87 int input;
meillo@51 88
meillo@51 89 if (node->down == NULL) {
meillo@51 90 /* user input */
meillo@51 91 printf("input: ");
meillo@51 92 scanf("%d", &input);
meillo@51 93 input = input % 256;
meillo@51 94 } else {
meillo@51 95 /* return value */
meillo@51 96 input = action(node->down);
meillo@51 97 }
meillo@5 98 insertLast(node, newNode("number", (char) input));
meillo@3 99 return 0;
meillo@3 100 }
meillo@3 101
meillo@3 102
meillo@5 103 unsigned char action_times(struct Node* node) {
meillo@5 104 unsigned char i;
meillo@10 105 for (i = 0; i < node->value; i++) {
meillo@46 106 insertLast(node, copyTree(node->down));
meillo@5 107 }
meillo@5 108 return 0;
meillo@5 109 }
meillo@50 110
meillo@50 111
meillo@50 112 unsigned char action_if(struct Node* node) {
meillo@50 113 int result = 0;
meillo@50 114 char return1;
meillo@50 115 char return2;
meillo@50 116
meillo@50 117 if (node->down == NULL || node->down->right == NULL) {
meillo@50 118 fprintf(stderr, "error: while requires two sons");
meillo@52 119 exit(7);
meillo@50 120 }
meillo@50 121
meillo@50 122 return1 = action(node->down);
meillo@50 123 return2 = action(node->down->right);
meillo@50 124
meillo@50 125 if (node->value == '!') { /* 33 */
meillo@50 126 if (return1 != return2) {
meillo@50 127 result = 1;
meillo@50 128 }
meillo@50 129 } else if (node->value == '<') { /* 60 */
meillo@50 130 if (return1 < return2) {
meillo@50 131 result = 1;
meillo@50 132 }
meillo@50 133 } else if (node->value == '>') { /* 62 */
meillo@50 134 if (return1 > return2) {
meillo@50 135 result = 1;
meillo@50 136 }
meillo@50 137 } else {
meillo@50 138 if (return1 == return2) {
meillo@50 139 result = 1;
meillo@50 140 }
meillo@50 141 }
meillo@50 142 return result;
meillo@50 143 }
meillo@50 144
meillo@50 145
meillo@50 146 unsigned char action_while(struct Node* node) {
meillo@50 147 if (node->down == NULL || node->down->right == NULL) {
meillo@50 148 fprintf(stderr, "error: while requires two sons");
meillo@52 149 exit(7);
meillo@50 150 }
meillo@50 151 while (action(node->down)) {
meillo@50 152 action(node->down->right);
meillo@50 153 }
meillo@50 154 return 0;
meillo@50 155 }