Mercurial > baum
annotate baum.c @ 62:80df58d240b2 default tip
merge
author | meillo@marmaro.de |
---|---|
date | Thu, 13 Nov 2008 13:19:42 +0100 |
parents | 73de2151aebd b7544f23673b |
children |
rev | line source |
---|---|
0 | 1 /* |
2 * baum - an esoteric programming language | |
3 * | |
4 * (c) markus schnalke <meillo@marmaro.de> | |
5 * and julian forster | |
6 * | |
7 */ | |
8 | |
9 | |
10 #include <stdio.h> | |
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 | 13 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
14 #include "baum.h" |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
15 |
59 | 16 #define VERSION "0.5" |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
17 |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
18 |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
19 void printNode(struct Node* node, int level); |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
20 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
|
21 struct Node* lastNode(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
22 void delete(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
23 |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
24 |
41 | 25 struct Stackitem { |
26 struct Node* node; | |
27 struct Stackitem* next; | |
28 }; | |
29 | |
30 | |
31 | |
32 struct Node* root = 0; | |
33 struct Stackitem* stack = NULL; | |
34 | |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
35 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
36 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
|
37 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
38 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
|
39 } |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
40 } |
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 /* new */ |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
44 struct Node* newNode(char* name, unsigned char value) { |
0 | 45 struct Node* node; |
46 node = (struct Node*) malloc(sizeof(struct Node)); | |
55
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
47 if (node == NULL) { |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
48 perror("unable to allocate memory"); |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
49 exit(10); |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
50 } |
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
|
51 strcpy(node->name, name); |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
52 node->value = value; |
0 | 53 node->right = 0; |
54 node->down = 0; | |
55 return node; | |
56 } | |
57 | |
58 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
59 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
60 struct Node* lastNode(struct Node* node) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
61 while (node->right != NULL) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
62 node = node->right; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
63 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
64 return node; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
65 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
66 |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
13
diff
changeset
|
67 struct Node* insertLast(struct Node* node, struct Node* insert) { |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
68 node = lastNode(node); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
69 node->right = insert; |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
13
diff
changeset
|
70 return insert; |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
71 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
72 |
46 | 73 struct Node* copyTree(struct Node* node) { |
74 if (node == NULL) { | |
75 return NULL; | |
76 } | |
77 struct Node* tmp; | |
78 tmp = newNode(node->name, node->value); | |
79 tmp->down = copyTree(node->down); | |
80 tmp->right = copyTree(node->right); | |
81 return tmp; | |
82 } | |
83 | |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
84 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
85 /* delete */ |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
86 void delete(struct Node* node) { |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
87 if (node == NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
88 return; |
0 | 89 } |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
90 delete(node->down); |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
91 delete(node->right); |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
92 free(node); node=0; |
0 | 93 } |
94 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
95 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
96 /* print */ |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
97 void printNode(struct Node* node, int level) { |
55
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
98 if (node == NULL) { |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
99 return; |
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 } |
55
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
101 while (level-- > 0) { |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
102 fprintf(stderr, "\t"); |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
103 } |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
104 fprintf(stderr, "%s (%d|%c)\n", node->name, node->value, node->value); |
0 | 105 } |
106 | |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
107 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
|
108 if (root == NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
109 return; |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
110 } |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
111 printNode(root, level); |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
112 printTree(root->down, level+1); |
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
113 printTree(root->right, level); |
0 | 114 } |
115 | |
116 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
117 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
118 |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
119 |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
120 /* 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
|
121 void push(struct Node* node) { |
10 | 122 struct Stackitem* tmp; |
123 struct Stackitem* new; | |
124 new = (struct Stackitem*) malloc(sizeof(struct Stackitem)); | |
55
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
125 if (new == NULL) { |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
126 perror("unable to allocate memory"); |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
127 exit(10); |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
128 } |
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
|
129 new->node = node; |
10 | 130 tmp = stack; |
131 stack = new; | |
132 stack->next = tmp; | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
133 } |
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 struct Node* pull() { |
10 | 135 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
|
136 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
|
137 } |
10 | 138 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
|
139 struct Node* node; |
10 | 140 tmp = stack; |
141 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
|
142 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
|
143 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
|
144 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
|
145 } |
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
|
146 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
147 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
148 |
6 | 149 /* read input */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
150 void read_input(char* filename) { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
151 int c; |
8 | 152 int indent; |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
153 char name[256]; |
6 | 154 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
|
155 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
|
156 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
|
157 struct Node* node; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
158 FILE* file; |
6 | 159 |
8 | 160 indent = 0; |
161 strcpy(name, ""); | |
162 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
|
163 last_indent = -1; |
34 | 164 last_node = NULL; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
165 file = fopen(filename, "r"); |
55
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
166 if (file == NULL) { |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
167 perror("unable to open file"); |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
168 exit(10); |
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
169 } |
6 | 170 |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
171 while ((c = getc(file)) != EOF) { |
55
6279e5b14d9e
added error handling for fopen and malloc; code cleanups
meillo@marmaro.de
parents:
54
diff
changeset
|
172 if (c == '#') { /* comment */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
173 while ((c = getc(file)) != '\n') { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
174 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
175 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
176 |
8 | 177 if (c == ' ' || c == '\t') { /* indent if at start of line */ |
178 if (strlen(name) == 0) { | |
179 indent++; | |
180 } | |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
181 } |
6 | 182 |
8 | 183 if (c == '\n') { /* end of line: create node */ |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
184 if (strlen(name) > 0) { |
8 | 185 /* 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
|
186 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
|
187 if (indent > last_indent) { /* down */ |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
188 /* 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
|
189 if (indent > last_indent + 1) { |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
190 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
|
191 exit(5); |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
192 } |
34 | 193 if (last_node == NULL) { |
194 root = node; | |
195 last_node = root; | |
196 } else { | |
197 last_node->down = node; | |
198 } | |
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
|
199 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
|
200 } 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
|
201 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
|
202 } 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
|
203 /* 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
|
204 while (indent < last_indent) { |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
205 last_node = pull(); |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
206 last_indent--; |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
207 } |
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
|
208 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
|
209 } |
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
|
210 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
|
211 last_node = node; |
6 | 212 } |
213 indent = 0; | |
214 strcpy(name, ""); | |
215 value = 0; | |
216 } | |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
217 |
8 | 218 if (c >= 'a' && c <= 'z') { /* name */ |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
219 int i = 1; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
220 name[0] = (char) c; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
221 while ((c = getc(file)) != '(') { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
222 name[i] = (char) c; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
223 i++; |
8 | 224 if (i > 255) { |
33
2e564bf8599c
new error code 6; more readable printTree; cleanups
meillo@marmaro.de
parents:
31
diff
changeset
|
225 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
|
226 exit(6); |
8 | 227 } |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
228 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
229 name[i] = '\0'; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
230 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
231 |
8 | 232 if (c == '(') { /* value */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
233 fscanf(file, "%d)", &value); |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
234 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
235 |
6 | 236 } |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
237 |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
238 /* clear stack */ |
27
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
239 while (stack != NULL) { |
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
240 pull(); |
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
241 } |
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
242 |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
243 fclose(file); |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
244 } |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
245 |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
246 |
6 | 247 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
248 /* main */ |
0 | 249 int main(int argc, char* argv[]) { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
250 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
|
251 |
ff01f0f076e4
option_verbose is now global; stuff about warning when (expected) nodes are not there
meillo@marmaro.de
parents:
37
diff
changeset
|
252 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
|
253 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
254 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
|
255 if (strcmp(argv[0], "--version") == 0) { |
61 | 256 printf("baum %s\n" |
257 "an esoteric programming language\n" | |
258 "by markus schnalke and julian forster\n" | |
259 "http://prog.marmaro.de/baum\n", VERSION); | |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
260 exit(0); |
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], "--help") == 0) { |
61 | 262 printf("baum --version print version information and exit\n" |
263 "baum --help print this output\n" | |
264 "baum [-v] <file> (verbosly) run file\n"); | |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
265 exit(0); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
266 } 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
|
267 option_verbose = 1; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
268 /* |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
269 } 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
|
270 / * 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
|
271 iDWarn = atoi((++argv)[0]); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
272 argc--; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
273 */ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
274 } else { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
275 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
|
276 exit(126); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
277 } |
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 |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
280 if (argc != 1) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
281 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
|
282 exit(3); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
283 } |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
284 |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
285 read_input(argv[0]); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
286 |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
287 if (option_verbose) { |
53
036779d5da75
"sum" node clears its value now everytime it gets called
meillo@marmaro.de
parents:
47
diff
changeset
|
288 fprintf(stderr, "\n\ntree read from input:\n"); |
036779d5da75
"sum" node clears its value now everytime it gets called
meillo@marmaro.de
parents:
47
diff
changeset
|
289 printTree(root, 1); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
290 } |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
291 |
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
|
292 shell_return = action(root); |
34 | 293 fflush(stdout); |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
294 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
295 if (option_verbose) { |
53
036779d5da75
"sum" node clears its value now everytime it gets called
meillo@marmaro.de
parents:
47
diff
changeset
|
296 fprintf(stderr, "\n\nmodified tree after execution:\n"); |
036779d5da75
"sum" node clears its value now everytime it gets called
meillo@marmaro.de
parents:
47
diff
changeset
|
297 printTree(root, 1); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
298 } |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
299 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
300 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
|
301 exit(shell_return); |
0 | 302 } |