annotate baum.c @ 46:22305a6e128d

added deep copy and fixed so node times
author meillo@marmaro.de
date Sun, 02 Mar 2008 10:29:55 +0100
parents 0b82169d4129
children c31b5bb6d493
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
1 /*
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
2 * baum - an esoteric programming language
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
3 *
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
4 * (c) markus schnalke <meillo@marmaro.de>
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
5 * and julian forster
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
6 *
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
7 */
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
8
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
9
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
10 #include <stdio.h>
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
11 #include <stdlib.h>
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
12 #include <string.h>
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
13
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
14 #include "baum.h"
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents: 1
diff changeset
15 #include "actions.h"
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
16
37
29172b6e802a switched to next version number, updated man page
meillo@marmaro.de
parents: 34
diff changeset
17 #define VERSION "0.3"
13
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
18
30
cd979b979610 fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents: 29
diff changeset
19
33
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
20 void printNode(struct Node* node, int level);
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
21 void printTree(struct Node* root, int level);
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
22 struct Node* lastNode(struct Node* node);
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
23 void delete(struct Node* node);
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
24
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
25
41
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
26 struct Stackitem {
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
27 struct Node* node;
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
28 struct Stackitem* next;
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
29 };
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
30
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
31
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
32
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
33 struct Node* root = 0;
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
34 struct Stackitem* stack = NULL;
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
35
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
36
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
37 void logit(char* text) {
13
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
38 if (option_verbose) {
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
39 fprintf(stderr, "[%s]\n", text);
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
40 }
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
41 }
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
42
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
43
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
44 /* new */
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
45 struct Node* newNode(char* name, unsigned char value) {
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
46 struct Node* node;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
47 node = (struct Node*) malloc(sizeof(struct Node));
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
48 strcpy(node->name, name);
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
49 node->value = value;
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
50 node->right = 0;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
51 node->down = 0;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
52 return node;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
53 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
54
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
55
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
56
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
57 struct Node* lastNode(struct Node* node) {
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
58 while (node->right != NULL) {
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
59 node = node->right;
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
60 }
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
61 return node;
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
62 }
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
63
15
e2048e569891 insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents: 13
diff changeset
64 struct Node* insertLast(struct Node* node, struct Node* insert) {
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
65 node = lastNode(node);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
66 node->right = insert;
15
e2048e569891 insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents: 13
diff changeset
67 return insert;
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
68 }
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
69
46
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
70 struct Node* copyTree(struct Node* node) {
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
71 if (node == NULL) {
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
72 return NULL;
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
73 }
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
74 struct Node* tmp;
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
75 tmp = newNode(node->name, node->value);
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
76 tmp->down = copyTree(node->down);
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
77 tmp->right = copyTree(node->right);
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
78 return tmp;
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
79 }
22305a6e128d added deep copy and fixed so node times
meillo@marmaro.de
parents: 45
diff changeset
80
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
81
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
82 /* delete */
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
83 void delete(struct Node* node) {
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
84 if (node == NULL) {
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
85 return;
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
86 }
33
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
87 delete(node->down);
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
88 delete(node->right);
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
89 free(node); node=0;
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
90 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
91
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
92
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
93 /* print */
33
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
94 void printNode(struct Node* node, int level) {
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)
meillo@marmaro.de
parents: 4
diff changeset
95 if (node != NULL) {
33
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
96 while (level-- > 0) {
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
97 fprintf(stderr, "\t");
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
98 }
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
99 fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value);
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)
meillo@marmaro.de
parents: 4
diff changeset
100 }
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
101 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
102
33
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
103 void printTree(struct Node* root, int level) {
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
104 if (root == NULL) {
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
105 return;
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
106 }
33
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
107 printNode(root, level);
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
108 printTree(root->down, level+1);
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
109 printTree(root->right, level);
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
110 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
111
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
112
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
113
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
114
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
115
30
cd979b979610 fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents: 29
diff changeset
116 /* stack for read_input */
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
117 void push(struct Node* node) {
10
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
118 struct Stackitem* tmp;
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
119 struct Stackitem* new;
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
120 new = (struct Stackitem*) malloc(sizeof(struct Stackitem));
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
121 new->node = node;
10
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
122 tmp = stack;
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
123 stack = new;
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
124 stack->next = tmp;
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
125 }
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
126 struct Node* pull() {
10
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
127 if (stack == NULL) {
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
128 return NULL;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
129 }
10
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
130 struct Stackitem* tmp;
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
131 struct Node* node;
10
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
132 tmp = stack;
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
133 stack = stack->next;
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
134 node = tmp->node;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
135 free(tmp); tmp=0;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
136 return node;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
137 }
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
138
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
139
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
140
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
141 /* read input */
12
8e34daa80f64 input is now read from file again (because of input node)
meillo@marmaro.de
parents: 10
diff changeset
142 void read_input(char* filename) {
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
143 int c;
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
144 int indent;
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
145 char name[256];
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
146 int value;
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
147 int last_indent;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
148 struct Node* last_node;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
149 struct Node* node;
12
8e34daa80f64 input is now read from file again (because of input node)
meillo@marmaro.de
parents: 10
diff changeset
150 FILE* file;
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
151
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
152 indent = 0;
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
153 strcpy(name, "");
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
154 value = 0;
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
155 last_indent = -1;
34
e986c6abed2b eliminated generic blackhole node at root
meillo@marmaro.de
parents: 33
diff changeset
156 last_node = NULL;
12
8e34daa80f64 input is now read from file again (because of input node)
meillo@marmaro.de
parents: 10
diff changeset
157 file = fopen(filename, "r");
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
158
12
8e34daa80f64 input is now read from file again (because of input node)
meillo@marmaro.de
parents: 10
diff changeset
159 while ((c = getc(file)) != EOF) {
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
160 if (c == '#') { /* comment */
12
8e34daa80f64 input is now read from file again (because of input node)
meillo@marmaro.de
parents: 10
diff changeset
161 while ((c = getc(file)) != '\n') {
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
162 }
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
163 }
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
164
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
165 if (c == ' ' || c == '\t') { /* indent if at start of line */
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
166 if (strlen(name) == 0) {
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
167 indent++;
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
168 }
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
169 }
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
170
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
171 if (c == '\n') { /* end of line: create node */
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
172 if (strlen(name) > 0) {
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
173 /* create node */
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
174 node = newNode((char*) name, value);
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
175 if (indent > last_indent) { /* down */
30
cd979b979610 fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents: 29
diff changeset
176 /* if it goes more than one level down -> error */
33
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
177 if (indent > last_indent + 1) {
30
cd979b979610 fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents: 29
diff changeset
178 fprintf(stderr, "error: Indention over more than one level. Only indent by one!\n");
31
4e60d96265f0 removed -c option completely; updated man page; new error code 5
meillo@marmaro.de
parents: 30
diff changeset
179 exit(5);
30
cd979b979610 fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents: 29
diff changeset
180 }
34
e986c6abed2b eliminated generic blackhole node at root
meillo@marmaro.de
parents: 33
diff changeset
181 if (last_node == NULL) {
e986c6abed2b eliminated generic blackhole node at root
meillo@marmaro.de
parents: 33
diff changeset
182 root = node;
e986c6abed2b eliminated generic blackhole node at root
meillo@marmaro.de
parents: 33
diff changeset
183 last_node = root;
e986c6abed2b eliminated generic blackhole node at root
meillo@marmaro.de
parents: 33
diff changeset
184 } else {
e986c6abed2b eliminated generic blackhole node at root
meillo@marmaro.de
parents: 33
diff changeset
185 last_node->down = node;
e986c6abed2b eliminated generic blackhole node at root
meillo@marmaro.de
parents: 33
diff changeset
186 }
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
187 push(last_node);
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
188 } else if (indent == last_indent) { /* right */
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
189 last_node->right = node;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
190 } else if (indent < last_indent) { /* up */
30
cd979b979610 fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents: 29
diff changeset
191 /* handle if it goes more than one level up */
cd979b979610 fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents: 29
diff changeset
192 while (indent < last_indent) {
cd979b979610 fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents: 29
diff changeset
193 last_node = pull();
cd979b979610 fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents: 29
diff changeset
194 last_indent--;
cd979b979610 fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents: 29
diff changeset
195 }
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
196 last_node->right = node;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
197 }
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
198 last_indent = indent;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
199 last_node = node;
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
200 }
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
201 indent = 0;
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
202 strcpy(name, "");
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
203 value = 0;
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
204 }
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
205
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
206 if (c >= 'a' && c <= 'z') { /* name */
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
207 int i = 1;
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
208 name[0] = (char) c;
12
8e34daa80f64 input is now read from file again (because of input node)
meillo@marmaro.de
parents: 10
diff changeset
209 while ((c = getc(file)) != '(') {
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
210 name[i] = (char) c;
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
211 i++;
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
212 if (i > 255) {
33
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
213 fprintf(stderr, "error: node name too long, or no value given\n");
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
214 exit(6);
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
215 }
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
216 }
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
217 name[i] = '\0';
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
218 }
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
219
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
220 if (c == '(') { /* value */
12
8e34daa80f64 input is now read from file again (because of input node)
meillo@marmaro.de
parents: 10
diff changeset
221 fscanf(file, "%d)", &value);
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
222 }
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
223
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
224 }
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
225
30
cd979b979610 fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents: 29
diff changeset
226 /* clear stack */
27
1c3dd1e88bdf empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents: 26
diff changeset
227 while (stack != NULL) {
1c3dd1e88bdf empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents: 26
diff changeset
228 pull();
1c3dd1e88bdf empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents: 26
diff changeset
229 }
1c3dd1e88bdf empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents: 26
diff changeset
230
12
8e34daa80f64 input is now read from file again (because of input node)
meillo@marmaro.de
parents: 10
diff changeset
231 fclose(file);
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
232 }
12
8e34daa80f64 input is now read from file again (because of input node)
meillo@marmaro.de
parents: 10
diff changeset
233
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
234
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
235
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
236 /* main */
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
237 int main(int argc, char* argv[]) {
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
238 unsigned char shell_return = 0;
38
ff01f0f076e4 option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents: 37
diff changeset
239
ff01f0f076e4 option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents: 37
diff changeset
240 option_verbose = 0;
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
241
13
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
242 while (--argc > 0 && (*++argv)[0] == '-') {
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
243 if (strcmp(argv[0], "--version") == 0) {
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
244 printf("\
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
245 baum %s\n\
13
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
246 an esoteric programming language\n\
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
247 by markus schnalke and julian forster\n\
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
248 http://prog.marmaro.de/baum\n\
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
249 ", VERSION);
13
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
250 exit(0);
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
251 } else if (strcmp(argv[0], "--help") == 0) {
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
252 printf("\
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
253 baum --version print version information and exit\n\
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
254 baum --help print this output\n\
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
255 baum [-v] <file> (verbosly) run file\n\
26
f0856c177403 removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents: 15
diff changeset
256 ");
13
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
257 exit(0);
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
258 } else if (strcmp(argv[0], "-v") == 0) {
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
259 option_verbose = 1;
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
260 /*
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
261 } else if (strcmp(argv[0], "-W") == 0) {
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
262 / * TODO: catch if no value given * /
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
263 iDWarn = atoi((++argv)[0]);
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
264 argc--;
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
265 */
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
266 } else {
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
267 fprintf(stderr, "unknown option: %s\n", argv[0]);
42
233ac9bea4f1 switched exit code 127 to 126 (because shell returns 127 if command not found)
meillo@marmaro.de
parents: 41
diff changeset
268 exit(126);
13
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
269 }
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
270 }
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
271
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
272 if (argc != 1) {
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
273 fprintf(stderr, "%d source files given, please specify one.\n", argc);
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
274 exit(3);
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
275 }
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
276
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
277 read_input(argv[0]);
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
278
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
279 if (option_verbose) {
41
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
280 fprintf(stderr, "\n\ntree read from input\n(should be the same without comments and empty lines)\n\n");
33
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
281 printTree(root, 0);
13
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
282 }
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
283
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)
meillo@marmaro.de
parents: 4
diff changeset
284 shell_return = action(root);
34
e986c6abed2b eliminated generic blackhole node at root
meillo@marmaro.de
parents: 33
diff changeset
285 fflush(stdout);
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
286
13
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
287 if (option_verbose) {
41
1ad3d7305e5d reduced output; cleanups
meillo@marmaro.de
parents: 40
diff changeset
288 fprintf(stderr, "\n\ntree after execution of the program\n\n");
33
2e564bf8599c new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents: 31
diff changeset
289 printTree(root, 0);
13
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
290 }
bf660b45bba9 added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents: 12
diff changeset
291
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
292 delete(root);
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)
meillo@marmaro.de
parents: 4
diff changeset
293 exit(shell_return);
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
294 }