Mercurial > baum
comparison actions.c @ 5:c202ccccedb5
added checks for null pointer; print echoes as char or number now (depends on value); all logging goes to stderr now; new nodes blackhole and times (not implemented yet)
author | meillo@marmaro.de |
---|---|
date | Fri, 08 Feb 2008 20:45:17 +0100 |
parents | 15d7d6b9766f |
children | 0e15841ae111 |
comparison
equal
deleted
inserted
replaced
4:24a697f37e7c | 5:c202ccccedb5 |
---|---|
4 #include "baum.h" | 4 #include "baum.h" |
5 #include "actions.h" | 5 #include "actions.h" |
6 | 6 |
7 | 7 |
8 unsigned char action(struct Node* node) { | 8 unsigned char action(struct Node* node) { |
9 if (node == NULL) { | |
10 fprintf(stderr, "action of non existing node\n"); | |
11 return 0; | |
12 } | |
13 | |
9 if (strcmp(node->name, "print") == 0) { | 14 if (strcmp(node->name, "print") == 0) { |
10 logit("print-node"); | 15 logit("print-node"); |
11 return action_print(node); | 16 return action_print(node); |
12 | 17 |
13 } else if (strcmp(node->name, "sum") == 0) { | 18 } else if (strcmp(node->name, "sum") == 0) { |
14 logit("sum-node"); | 19 logit("sum-node"); |
15 return action_sum(node); | 20 return action_sum(node); |
16 | 21 |
17 } else if (strcmp(node->name, "printchar") == 0) { | |
18 logit("printchar-node"); | |
19 return action_printchar(node); | |
20 | |
21 } else if (strcmp(node->name, "number") == 0) { | 22 } else if (strcmp(node->name, "number") == 0) { |
22 logit("number-node"); | 23 logit("number-node"); |
23 return action_number(node); | 24 return action_number(node); |
24 | 25 |
25 } else if (strcmp(node->name, "input") == 0) { | 26 } else if (strcmp(node->name, "input") == 0) { |
26 logit("input-node"); | 27 logit("input-node"); |
27 return action_input(node); | 28 return action_input(node); |
29 | |
30 } else if (strcmp(node->name, "times") == 0) { | |
31 logit("times-node"); | |
32 return action_times(node); | |
33 | |
34 } else if (strcmp(node->name, "blackhole") == 0) { | |
35 logit("blackhole-node"); | |
36 return action_blackhole(node); | |
28 | 37 |
29 } else { | 38 } else { |
30 fprintf(stderr, "unknown kind of node"); | 39 fprintf(stderr, "unknown kind of node"); |
31 exit(1); | 40 exit(1); |
32 } | 41 } |
33 } | 42 } |
34 | 43 |
35 | 44 |
36 | 45 |
37 unsigned char action_print(struct Node* node) { | 46 unsigned char action_print(struct Node* node) { |
38 printf("%d\n", action(node->down)); | 47 unsigned char result; |
39 return 0; | 48 result = action(node->down); |
40 } | 49 if (node->value == 'c') { |
41 | 50 printf("%c", result); |
42 | 51 } else { |
43 unsigned char action_printchar(struct Node* node) { | 52 printf("%d", result); |
44 printf("%c\n", action(node->down)); | 53 } |
45 return 0; | 54 return result; |
46 } | 55 } |
47 | 56 |
48 | 57 |
49 unsigned char action_sum(struct Node* node) { | 58 unsigned char action_sum(struct Node* node) { |
50 struct Node* tp; | 59 struct Node* tp; |
61 return node->value; | 70 return node->value; |
62 } | 71 } |
63 | 72 |
64 | 73 |
65 unsigned char action_input(struct Node* node) { | 74 unsigned char action_input(struct Node* node) { |
75 /* | |
66 unsigned char input = (unsigned char) getchar(); | 76 unsigned char input = (unsigned char) getchar(); |
67 getchar(); /* catches the newline */ | 77 getchar(); /* catches the newline */ |
68 insertLast(node, newNode("number", input)); | 78 |
79 /* reads a number which is treated as ASCII value */ | |
80 int input; | |
81 scanf("%d", &input); | |
82 input = input % 256; | |
83 insertLast(node, newNode("number", (char) input)); | |
84 | |
69 return 0; | 85 return 0; |
70 } | 86 } |
71 | 87 |
72 | 88 |
89 unsigned char action_times(struct Node* node) { | |
90 struct Node* tp; | |
91 unsigned char i; | |
92 tp = node->down; | |
93 for (i; i < node->value; i++) { | |
94 /* deep copy */ | |
95 } | |
96 return 0; | |
97 } | |
98 | |
99 | |
100 unsigned char action_blackhole(struct Node* node) { | |
101 action(node->down); | |
102 return 0; | |
103 } | |
104 | |
105 |