Mercurial > baum
annotate baum.c @ 26:f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
author | meillo@marmaro.de |
---|---|
date | Tue, 19 Feb 2008 22:23:37 +0100 |
parents | e2048e569891 |
children | 1c3dd1e88bdf |
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" |
2 | 15 #include "actions.h" |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
16 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
17 #define VERSION "0.1" |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
18 |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
19 int option_check = 0; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
20 int option_verbose = 0; |
0 | 21 |
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
|
22 struct Node* root = 0; |
10 | 23 struct Stackitem* stack = NULL; |
0 | 24 |
25 | |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
26 void printNode(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
27 void printTree(struct Node* root); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
28 struct Node* lastNode(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
29 void delete(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
30 |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
31 |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
32 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
33 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
|
34 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
35 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
|
36 } |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
37 } |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
38 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
39 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
40 /* new */ |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
41 struct Node* newNode(char* name, unsigned char value) { |
0 | 42 struct Node* node; |
43 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
|
44 strcpy(node->name, name); |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
45 node->value = value; |
0 | 46 node->right = 0; |
47 node->down = 0; | |
48 return node; | |
49 } | |
50 | |
51 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
52 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
53 struct Node* lastNode(struct Node* node) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
54 while (node->right != NULL) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
55 node = node->right; |
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 return node; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
58 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
59 |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
13
diff
changeset
|
60 struct Node* insertLast(struct Node* node, struct Node* insert) { |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
61 node = lastNode(node); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
62 node->right = insert; |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
13
diff
changeset
|
63 return insert; |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
64 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
65 |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
66 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
67 /* delete */ |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
68 void delete(struct Node* node) { |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
69 if (node == NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
70 return; |
0 | 71 } |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
72 if (node->down != NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
73 delete(node->down); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
74 } |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
75 if (node->right != NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
76 delete(node->right); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
77 } |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
78 free(node); node=0; |
0 | 79 } |
80 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
81 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
82 /* print */ |
0 | 83 void printNode(struct Node* node) { |
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
|
84 if (node != 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
|
85 fprintf(stderr, "Node: %10s (%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
|
86 } |
0 | 87 } |
88 | |
89 void printTree(struct Node* root) { | |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
90 if (root == NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
91 return; |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
92 } |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
93 |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
94 printNode(root); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
95 fprintf(stderr, " down: "); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
96 if (root->down != NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
97 printTree(root->down); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
98 } else { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
99 fprintf(stderr, "NULL\n"); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
100 } |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
101 fprintf(stderr, " right: "); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
102 if (root->right != NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
103 printTree(root->right); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
104 } else { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
105 fprintf(stderr, "NULL\n"); |
0 | 106 } |
107 } | |
108 | |
109 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
110 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
111 |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
112 |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
113 /* read tree stack */ |
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
|
114 void push(struct Node* node) { |
10 | 115 struct Stackitem* tmp; |
116 struct Stackitem* new; | |
117 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
|
118 new->node = node; |
10 | 119 tmp = stack; |
120 stack = new; | |
121 stack->next = tmp; | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
122 } |
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
|
123 struct Node* pull() { |
10 | 124 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
|
125 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
|
126 } |
10 | 127 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
|
128 struct Node* node; |
10 | 129 tmp = stack; |
130 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
|
131 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
|
132 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
|
133 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
|
134 } |
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 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
136 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
137 |
6 | 138 /* read input */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
139 void read_input(char* filename) { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
140 int c; |
8 | 141 int indent; |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
142 char name[256]; |
6 | 143 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
|
144 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
|
145 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
|
146 struct Node* node; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
147 FILE* file; |
6 | 148 |
8 | 149 indent = 0; |
150 strcpy(name, ""); | |
151 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
|
152 last_indent = -1; |
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
|
153 root = newNode("blackhole", 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
|
154 last_node = root; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
155 file = fopen(filename, "r"); |
6 | 156 |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
157 while ((c = getc(file)) != EOF) { |
8 | 158 if (c == '#') { /* comment */ |
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)) != '\n') { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
160 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
161 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
162 |
8 | 163 if (c == ' ' || c == '\t') { /* indent if at start of line */ |
164 if (strlen(name) == 0) { | |
165 indent++; | |
166 } | |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
167 } |
6 | 168 |
8 | 169 if (c == '\n') { /* end of line: create node */ |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
170 if (strlen(name) > 0) { |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
171 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
172 fprintf(stderr, " %d - %s - %d\n", indent, name, value); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
173 } |
8 | 174 /* 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
|
175 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
|
176 if (indent > last_indent) { /* down */ |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
177 /* FIXME if it goes more than one level down -> error */ |
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
|
178 last_node->down = 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
|
179 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
|
180 } 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
|
181 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
|
182 } else if (indent < last_indent) { /* up */ |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
183 /* FIXME handle if it goes more than one level up */ |
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
|
184 last_node = pull(); |
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
|
185 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
|
186 } |
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 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
|
188 last_node = node; |
6 | 189 } |
190 indent = 0; | |
191 strcpy(name, ""); | |
192 value = 0; | |
193 } | |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
194 |
8 | 195 if (c >= 'a' && c <= 'z') { /* name */ |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
196 int i = 1; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
197 name[0] = (char) c; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
198 while ((c = getc(file)) != '(') { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
199 name[i] = (char) c; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
200 i++; |
8 | 201 if (i > 255) { |
202 break; | |
203 } | |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
204 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
205 name[i] = '\0'; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
206 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
207 |
8 | 208 if (c == '(') { /* value */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
209 fscanf(file, "%d)", &value); |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
210 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
211 |
6 | 212 } |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
213 |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
214 fclose(file); |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
215 } |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
216 |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
217 |
6 | 218 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
219 /* main */ |
0 | 220 int main(int argc, char* argv[]) { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
221 unsigned char shell_return = 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
|
222 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
223 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
|
224 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
|
225 printf("\ |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
226 baum %s\n\ |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
227 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
|
228 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
|
229 http://prog.marmaro.de/baum\n\ |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
230 ", VERSION); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
231 exit(0); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
232 } 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
|
233 printf("\ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
234 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
|
235 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
|
236 baum [-v] -c <file> (verbosly) check file and return 1 if invalid\n\ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
237 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
|
238 "); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
239 exit(0); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
240 } else if (strcmp(argv[0], "-c") == 0) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
241 option_check = 1; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
242 } 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
|
243 option_verbose = 1; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
244 /* |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
245 } 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
|
246 / * 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
|
247 iDWarn = atoi((++argv)[0]); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
248 argc--; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
249 */ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
250 } else { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
251 fprintf(stderr, "unknown option: %s\n", argv[0]); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
252 exit(127); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
253 } |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
254 } |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
255 |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
256 if (argc != 1) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
257 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
|
258 exit(3); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
259 } |
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 read_input(argv[0]); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
262 |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
263 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
264 printTree(root); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
265 } |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
266 |
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
|
267 shell_return = action(root); |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
268 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
269 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
270 printTree(root); |
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 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
273 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
|
274 exit(shell_return); |
0 | 275 } |