baum

annotate actions.c @ 59:b7544f23673b

switched to version 0.5
author meillo@marmaro.de
date Wed, 25 Jun 2008 11:09:28 +0200
parents 6279e5b14d9e
children
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@57 14 struct action {
meillo@57 15 char* name;
meillo@57 16 unsigned char (*func)(struct Node* node);
meillo@57 17 };
meillo@57 18
meillo@57 19 struct action actions[] = {
meillo@57 20 {"print", action_print},
meillo@57 21 {"sum", action_sum},
meillo@57 22 {"number", action_number},
meillo@57 23 {"create", action_create},
meillo@57 24 {"times", action_times},
meillo@57 25 {"if", action_if},
meillo@57 26 {"while", action_while},
meillo@57 27 };
meillo@57 28
meillo@26 29
meillo@2 30
meillo@3 31 unsigned char action(struct Node* node) {
meillo@57 32 int i;
meillo@57 33
meillo@5 34 if (node == NULL) {
meillo@38 35 if (option_verbose) {
meillo@38 36 fprintf(stderr, "warning: action of non existing node\n");
meillo@38 37 }
meillo@5 38 return 0;
meillo@5 39 }
meillo@5 40
meillo@57 41 for (i = 0; i < (sizeof(actions)/sizeof(actions[0])); i++) {
meillo@57 42 if (strcmp(actions[i].name, node->name) == 0) {
meillo@57 43 return (actions[i].func)(node);
meillo@57 44 }
meillo@57 45 }
meillo@3 46
meillo@31 47 fprintf(stderr, "unknown kind of node\n");
meillo@16 48 exit(4);
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@55 69 for (tp = node->down; tp != NULL; tp = tp->right) {
meillo@2 70 node->value += action(tp);
meillo@2 71 }
meillo@2 72 return node->value;
meillo@2 73 }
meillo@2 74
meillo@2 75
meillo@3 76 unsigned char action_number(struct Node* node) {
meillo@38 77 if (node->down != NULL) {
meillo@38 78 action(node->down);
meillo@38 79 }
meillo@2 80 return node->value;
meillo@2 81 }
meillo@2 82
meillo@3 83
meillo@51 84 unsigned char action_create(struct Node* node) {
meillo@5 85 int input;
meillo@51 86
meillo@51 87 if (node->down == NULL) {
meillo@51 88 /* user input */
meillo@51 89 printf("input: ");
meillo@51 90 scanf("%d", &input);
meillo@51 91 input = input % 256;
meillo@51 92 } else {
meillo@51 93 /* return value */
meillo@51 94 input = action(node->down);
meillo@51 95 }
meillo@5 96 insertLast(node, newNode("number", (char) input));
meillo@3 97 return 0;
meillo@3 98 }
meillo@3 99
meillo@3 100
meillo@5 101 unsigned char action_times(struct Node* node) {
meillo@5 102 unsigned char i;
meillo@55 103
meillo@10 104 for (i = 0; i < node->value; i++) {
meillo@46 105 insertLast(node, copyTree(node->down));
meillo@5 106 }
meillo@5 107 return 0;
meillo@5 108 }
meillo@50 109
meillo@50 110
meillo@50 111 unsigned char action_if(struct Node* node) {
meillo@50 112 int result = 0;
meillo@50 113 char return1;
meillo@50 114 char return2;
meillo@50 115
meillo@50 116 if (node->down == NULL || node->down->right == NULL) {
meillo@50 117 fprintf(stderr, "error: while requires two sons");
meillo@52 118 exit(7);
meillo@50 119 }
meillo@50 120
meillo@50 121 return1 = action(node->down);
meillo@50 122 return2 = action(node->down->right);
meillo@50 123
meillo@50 124 if (node->value == '!') { /* 33 */
meillo@50 125 if (return1 != return2) {
meillo@50 126 result = 1;
meillo@50 127 }
meillo@50 128 } else if (node->value == '<') { /* 60 */
meillo@50 129 if (return1 < return2) {
meillo@50 130 result = 1;
meillo@50 131 }
meillo@50 132 } else if (node->value == '>') { /* 62 */
meillo@50 133 if (return1 > return2) {
meillo@50 134 result = 1;
meillo@50 135 }
meillo@50 136 } else {
meillo@50 137 if (return1 == return2) {
meillo@50 138 result = 1;
meillo@50 139 }
meillo@50 140 }
meillo@50 141 return result;
meillo@50 142 }
meillo@50 143
meillo@50 144
meillo@50 145 unsigned char action_while(struct Node* node) {
meillo@50 146 if (node->down == NULL || node->down->right == NULL) {
meillo@50 147 fprintf(stderr, "error: while requires two sons");
meillo@52 148 exit(7);
meillo@50 149 }
meillo@50 150 while (action(node->down)) {
meillo@50 151 action(node->down->right);
meillo@50 152 }
meillo@50 153 return 0;
meillo@50 154 }