baum
view actions.c @ 51:7d7abe88e71b
"input" node is now "create" node and can handle more than user input now
author | meillo@marmaro.de |
---|---|
date | Sun, 02 Mar 2008 16:17:18 +0100 |
parents | 0870e261bf28 |
children | 201b4603671a |
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;
67 tp = node->down;
68 while (tp != NULL) {
69 node->value += action(tp);
70 tp = tp->right;
71 }
72 return node->value;
73 }
76 unsigned char action_number(struct Node* node) {
77 if (node->down != NULL) {
78 action(node->down);
79 }
80 return node->value;
81 }
84 unsigned char action_create(struct Node* node) {
85 int input;
87 if (node->down == NULL) {
88 /* user input */
89 printf("input: ");
90 scanf("%d", &input);
91 input = input % 256;
92 } else {
93 /* return value */
94 input = action(node->down);
95 }
97 insertLast(node, newNode("number", (char) input));
98 return 0;
99 }
102 unsigned char action_times(struct Node* node) {
103 unsigned char i;
104 for (i = 0; i < node->value; i++) {
105 insertLast(node, copyTree(node->down));
106 }
107 return 0;
108 }
111 unsigned char action_if(struct Node* node) {
112 int result = 0;
113 char return1;
114 char return2;
116 if (node->down == NULL || node->down->right == NULL) {
117 fprintf(stderr, "error: while requires two sons");
118 exit(1); /* FIXME other error code */
119 }
121 return1 = action(node->down);
122 return2 = action(node->down->right);
124 if (node->value == '!') { /* 33 */
125 if (return1 != return2) {
126 result = 1;
127 }
128 } else if (node->value == '<') { /* 60 */
129 if (return1 < return2) {
130 result = 1;
131 }
132 } else if (node->value == '>') { /* 62 */
133 if (return1 > return2) {
134 result = 1;
135 }
136 } else {
137 if (return1 == return2) {
138 result = 1;
139 }
140 }
141 return result;
142 }
145 unsigned char action_while(struct Node* node) {
146 if (node->down == NULL || node->down->right == NULL) {
147 fprintf(stderr, "error: while requires two sons");
148 exit(1); /* FIXME other error code */
149 }
150 while (action(node->down)) {
151 action(node->down->right);
152 }
153 return 0;
154 }