Mercurial > baum
annotate baum.c @ 16:b62288419c1c
added README and LICENSE; changed error code of invalid node
author | meillo@marmaro.de |
---|---|
date | Wed, 13 Feb 2008 20:33:39 +0100 |
parents | e2048e569891 |
children | f0856c177403 |
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 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
26 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
|
27 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
28 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
|
29 } |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
30 } |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
31 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
32 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
33 /* new */ |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
34 struct Node* newNode(char* name, unsigned char value) { |
0 | 35 struct Node* node; |
36 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
|
37 strcpy(node->name, name); |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
38 node->value = value; |
0 | 39 node->right = 0; |
40 node->down = 0; | |
41 return node; | |
42 } | |
43 | |
44 | |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
45 void setValue(struct Node* node, unsigned char value) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
46 node->value = value; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
47 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
48 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
49 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
50 struct Node* nextNode(struct Node* node) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
51 return node->right; |
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 |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
54 struct Node* lastNode(struct Node* node) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
55 while (node->right != NULL) { |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
56 node = node->right; |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
57 } |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
58 return node; |
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 |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
13
diff
changeset
|
61 struct Node* insertLast(struct Node* node, struct Node* insert) { |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
62 node = lastNode(node); |
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
63 node->right = insert; |
15
e2048e569891
insertLast returns now new inserted node; very dumb implementation for action_times
meillo@marmaro.de
parents:
13
diff
changeset
|
64 return insert; |
3
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 |
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) { |
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
|
69 if (node != NULL) { |
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
|
70 if (node->down != NULL) { |
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
|
71 delete(node->down); |
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
|
72 } |
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
|
73 if (node->right != NULL) { |
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
|
74 delete(node->right); |
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
|
75 } |
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
|
76 free(node); node=0; |
0 | 77 } |
78 } | |
79 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
80 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
81 /* print */ |
0 | 82 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
|
83 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
|
84 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
|
85 } |
0 | 86 } |
87 | |
88 void printTree(struct Node* 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
|
89 if (root != NULL) { |
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
|
90 printNode(root); |
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
|
91 fprintf(stderr, " down: "); |
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
|
92 if (root->down != NULL) { |
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
|
93 printTree(root->down); |
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
|
94 } else { |
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 fprintf(stderr, "NULL\n"); |
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
|
96 } |
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
|
97 fprintf(stderr, " right: "); |
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
|
98 if (root->right != NULL) { |
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
|
99 printTree(root->right); |
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 } else { |
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
|
101 fprintf(stderr, "NULL\n"); |
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
|
102 } |
0 | 103 } |
104 } | |
105 | |
106 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
107 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
108 /* traverse */ |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
109 void traverse(struct Node* root) { |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
110 /* each node controlls the nodes below itself */ |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
111 action(root); |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
112 } |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
113 |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
114 |
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
|
115 |
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
|
116 |
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 | 118 struct Stackitem* tmp; |
119 struct Stackitem* new; | |
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 | 122 tmp = stack; |
123 stack = new; | |
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 | 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 | 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 | 132 tmp = stack; |
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 | 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 | 144 int indent; |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
145 char name[256]; |
6 | 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 | 151 |
8 | 152 indent = 0; |
153 strcpy(name, ""); | |
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; |
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 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
|
157 last_node = root; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
158 file = fopen(filename, "r"); |
6 | 159 |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
160 while ((c = getc(file)) != EOF) { |
8 | 161 if (c == '#') { /* comment */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
162 while ((c = getc(file)) != '\n') { |
7
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 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
165 |
8 | 166 if (c == ' ' || c == '\t') { /* indent if at start of line */ |
167 if (strlen(name) == 0) { | |
168 indent++; | |
169 } | |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
170 } |
6 | 171 |
8 | 172 if (c == '\n') { /* end of line: create node */ |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
173 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
|
174 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
175 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
|
176 } |
8 | 177 /* 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
|
178 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
|
179 if (indent > last_indent) { /* down */ |
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 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
|
181 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
|
182 } 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
|
183 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
|
184 } else if (indent < last_indent) { /* up */ |
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 /* FIXME what if it goes more than one level up? */ |
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 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
|
187 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
|
188 } |
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_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
|
190 last_node = node; |
6 | 191 } |
192 indent = 0; | |
193 strcpy(name, ""); | |
194 value = 0; | |
195 } | |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
196 |
8 | 197 if (c >= 'a' && c <= 'z') { /* name */ |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
198 int i = 1; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
199 name[0] = (char) c; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
200 while ((c = getc(file)) != '(') { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
201 name[i] = (char) c; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
202 i++; |
8 | 203 if (i > 255) { |
204 break; | |
205 } | |
7
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 name[i] = '\0'; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
208 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
209 |
8 | 210 if (c == '(') { /* value */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
211 fscanf(file, "%d)", &value); |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
212 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
213 |
6 | 214 } |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
215 |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
216 fclose(file); |
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
217 |
6 | 218 } |
219 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
220 /* main */ |
0 | 221 int main(int argc, char* argv[]) { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
222 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
|
223 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
224 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
|
225 if (strcmp(argv[0], "--version") == 0) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
226 printf("baum %s\n\ |
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\ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
229 http://prog.marmaro.de/baum\n", VERSION); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
230 exit(0); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
231 } 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
|
232 printf("\ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
233 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
|
234 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
|
235 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
|
236 baum [-v] <file> (verbosly) run file\n\ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
237 "); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
238 exit(0); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
239 } 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
|
240 option_check = 1; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
241 } 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
|
242 option_verbose = 1; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
243 /* |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
244 } 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
|
245 / * 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
|
246 iDWarn = atoi((++argv)[0]); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
247 argc--; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
248 */ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
249 } else { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
250 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
|
251 exit(127); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
252 } |
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 if (argc != 1) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
256 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
|
257 exit(3); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
258 } |
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 read_input(argv[0]); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
261 |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
262 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
263 printTree(root); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
264 } |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
265 |
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
|
266 shell_return = action(root); |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
267 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
268 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
269 printTree(root); |
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 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
272 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
|
273 exit(shell_return); |
0 | 274 } |