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@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@51
|
33 } else if (strcmp(node->name, "create") == 0) {
|
meillo@51
|
34 return action_create(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@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@51
|
96
|
meillo@5
|
97 insertLast(node, newNode("number", (char) input));
|
meillo@3
|
98 return 0;
|
meillo@3
|
99 }
|
meillo@3
|
100
|
meillo@3
|
101
|
meillo@5
|
102 unsigned char action_times(struct Node* node) {
|
meillo@5
|
103 unsigned char i;
|
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 }
|