baum
view 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 |
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_input(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, "input") == 0) {
34 return action_input(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_input(struct Node* node) {
85 int input;
86 printf("input: ");
87 scanf("%d", &input);
88 input = input % 256;
89 insertLast(node, newNode("number", (char) input));
90 return 0;
91 }
94 unsigned char action_times(struct Node* node) {
95 unsigned char i;
96 for (i = 0; i < node->value; i++) {
97 insertLast(node, copyTree(node->down));
98 }
99 return 0;
100 }
103 unsigned char action_if(struct Node* node) {
104 int result = 0;
105 char return1;
106 char return2;
108 if (node->down == NULL || node->down->right == NULL) {
109 fprintf(stderr, "error: while requires two sons");
110 exit(1); /* FIXME other error code */
111 }
113 return1 = action(node->down);
114 return2 = action(node->down->right);
116 if (node->value == '!') { /* 33 */
117 if (return1 != return2) {
118 result = 1;
119 }
120 } else if (node->value == '<') { /* 60 */
121 if (return1 < return2) {
122 result = 1;
123 }
124 } else if (node->value == '>') { /* 62 */
125 if (return1 > return2) {
126 result = 1;
127 }
128 } else {
129 if (return1 == return2) {
130 result = 1;
131 }
132 }
133 return result;
134 }
137 unsigned char action_while(struct Node* node) {
138 if (node->down == NULL || node->down->right == NULL) {
139 fprintf(stderr, "error: while requires two sons");
140 exit(1); /* FIXME other error code */
141 }
142 while (action(node->down)) {
143 action(node->down->right);
144 }
145 return 0;
146 }