baum
view 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 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "baum.h"
6 unsigned char action_print(struct Node* node);
7 unsigned char action_sum(struct Node* node);
8 unsigned char action_number(struct Node* node);
9 unsigned char action_create(struct Node* node);
10 unsigned char action_times(struct Node* node);
11 unsigned char action_if(struct Node* node);
12 unsigned char action_while(struct Node* node);
16 unsigned char action(struct Node* node) {
17 if (node == NULL) {
18 if (option_verbose) {
19 fprintf(stderr, "warning: action of non existing node\n");
20 }
21 return 0;
22 }
24 if (strcmp(node->name, "print") == 0) {
25 return action_print(node);
27 } else if (strcmp(node->name, "sum") == 0) {
28 return action_sum(node);
30 } else if (strcmp(node->name, "number") == 0) {
31 return action_number(node);
33 } else if (strcmp(node->name, "create") == 0) {
34 return action_create(node);
36 } else if (strcmp(node->name, "times") == 0) {
37 return action_times(node);
39 } else if (strcmp(node->name, "if") == 0) {
40 return action_if(node);
42 } else if (strcmp(node->name, "while") == 0) {
43 return action_while(node);
45 } else {
46 fprintf(stderr, "unknown kind of node\n");
47 exit(4);
48 }
49 }
53 unsigned char action_print(struct Node* node) {
54 unsigned char result;
55 result = action(node->down);
56 if (node->value == 'c') {
57 printf("%c", result);
58 } else {
59 printf("%d", result);
60 }
61 return result;
62 }
65 unsigned char action_sum(struct Node* node) {
66 struct Node* tp;
68 node->value = 0;
69 tp = node->down;
70 while (tp != NULL) {
71 node->value += action(tp);
72 tp = tp->right;
73 }
74 return node->value;
75 }
78 unsigned char action_number(struct Node* node) {
79 if (node->down != NULL) {
80 action(node->down);
81 }
82 return node->value;
83 }
86 unsigned char action_create(struct Node* node) {
87 int input;
89 if (node->down == NULL) {
90 /* user input */
91 printf("input: ");
92 scanf("%d", &input);
93 input = input % 256;
94 } else {
95 /* return value */
96 input = action(node->down);
97 }
98 insertLast(node, newNode("number", (char) input));
99 return 0;
100 }
103 unsigned char action_times(struct Node* node) {
104 unsigned char i;
105 for (i = 0; i < node->value; i++) {
106 insertLast(node, copyTree(node->down));
107 }
108 return 0;
109 }
112 unsigned char action_if(struct Node* node) {
113 int result = 0;
114 char return1;
115 char return2;
117 if (node->down == NULL || node->down->right == NULL) {
118 fprintf(stderr, "error: while requires two sons");
119 exit(7);
120 }
122 return1 = action(node->down);
123 return2 = action(node->down->right);
125 if (node->value == '!') { /* 33 */
126 if (return1 != return2) {
127 result = 1;
128 }
129 } else if (node->value == '<') { /* 60 */
130 if (return1 < return2) {
131 result = 1;
132 }
133 } else if (node->value == '>') { /* 62 */
134 if (return1 > return2) {
135 result = 1;
136 }
137 } else {
138 if (return1 == return2) {
139 result = 1;
140 }
141 }
142 return result;
143 }
146 unsigned char action_while(struct Node* node) {
147 if (node->down == NULL || node->down->right == NULL) {
148 fprintf(stderr, "error: while requires two sons");
149 exit(7);
150 }
151 while (action(node->down)) {
152 action(node->down->right);
153 }
154 return 0;
155 }