baum

annotate actions.c @ 50:0870e261bf28

added first implementation of "if" and "while" (but they are not perfect yet)
author meillo@marmaro.de
date Sun, 02 Mar 2008 15:54:35 +0100
parents f9fc4c4f9e3d
children 7d7abe88e71b
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@26 9 unsigned char action_input(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@3 33 } else if (strcmp(node->name, "input") == 0) {
meillo@3 34 return action_input(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@2 67 tp = node->down;
meillo@2 68 while (tp != NULL) {
meillo@2 69 node->value += action(tp);
meillo@2 70 tp = tp->right;
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@3 84 unsigned char action_input(struct Node* node) {
meillo@5 85 int input;
meillo@14 86 printf("input: ");
meillo@5 87 scanf("%d", &input);
meillo@5 88 input = input % 256;
meillo@5 89 insertLast(node, newNode("number", (char) input));
meillo@3 90 return 0;
meillo@3 91 }
meillo@3 92
meillo@3 93
meillo@5 94 unsigned char action_times(struct Node* node) {
meillo@5 95 unsigned char i;
meillo@10 96 for (i = 0; i < node->value; i++) {
meillo@46 97 insertLast(node, copyTree(node->down));
meillo@5 98 }
meillo@5 99 return 0;
meillo@5 100 }
meillo@50 101
meillo@50 102
meillo@50 103 unsigned char action_if(struct Node* node) {
meillo@50 104 int result = 0;
meillo@50 105 char return1;
meillo@50 106 char return2;
meillo@50 107
meillo@50 108 if (node->down == NULL || node->down->right == NULL) {
meillo@50 109 fprintf(stderr, "error: while requires two sons");
meillo@50 110 exit(1); /* FIXME other error code */
meillo@50 111 }
meillo@50 112
meillo@50 113 return1 = action(node->down);
meillo@50 114 return2 = action(node->down->right);
meillo@50 115
meillo@50 116 if (node->value == '!') { /* 33 */
meillo@50 117 if (return1 != return2) {
meillo@50 118 result = 1;
meillo@50 119 }
meillo@50 120 } else if (node->value == '<') { /* 60 */
meillo@50 121 if (return1 < return2) {
meillo@50 122 result = 1;
meillo@50 123 }
meillo@50 124 } else if (node->value == '>') { /* 62 */
meillo@50 125 if (return1 > return2) {
meillo@50 126 result = 1;
meillo@50 127 }
meillo@50 128 } else {
meillo@50 129 if (return1 == return2) {
meillo@50 130 result = 1;
meillo@50 131 }
meillo@50 132 }
meillo@50 133 return result;
meillo@50 134 }
meillo@50 135
meillo@50 136
meillo@50 137 unsigned char action_while(struct Node* node) {
meillo@50 138 if (node->down == NULL || node->down->right == NULL) {
meillo@50 139 fprintf(stderr, "error: while requires two sons");
meillo@50 140 exit(1); /* FIXME other error code */
meillo@50 141 }
meillo@50 142 while (action(node->down)) {
meillo@50 143 action(node->down->right);
meillo@50 144 }
meillo@50 145 return 0;
meillo@50 146 }