Mercurial > baum
annotate baum.c @ 30:cd979b979610
fixed multiple (un)indentions in read_input; some better comments
author | meillo@marmaro.de |
---|---|
date | Fri, 22 Feb 2008 14:47:47 +0100 |
parents | 88a51653db83 |
children | 4e60d96265f0 |
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 |
29
88a51653db83
removed binary from dist tarball; new debian package
meillo@marmaro.de
parents:
27
diff
changeset
|
17 #define VERSION "0.2" |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
18 |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
19 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
20 int option_check = 0; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
21 int option_verbose = 0; |
0 | 22 |
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
|
23 struct Node* root = 0; |
10 | 24 struct Stackitem* stack = NULL; |
0 | 25 |
26 | |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
27 void printNode(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
28 void printTree(struct Node* root); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
29 struct Node* lastNode(struct Node* node); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
30 void delete(struct Node* node); |
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 |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
33 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
34 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
|
35 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
36 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
|
37 } |
1
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 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
41 /* new */ |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
42 struct Node* newNode(char* name, unsigned char value) { |
0 | 43 struct Node* node; |
44 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
|
45 strcpy(node->name, name); |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
46 node->value = value; |
0 | 47 node->right = 0; |
48 node->down = 0; | |
49 return node; | |
50 } | |
51 | |
52 | |
3
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 |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
67 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
68 /* delete */ |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
69 void delete(struct Node* node) { |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
70 if (node == NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
71 return; |
0 | 72 } |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
73 if (node->down != NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
74 delete(node->down); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
75 } |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
76 if (node->right != NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
77 delete(node->right); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
78 } |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
79 free(node); node=0; |
0 | 80 } |
81 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
82 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
83 /* print */ |
0 | 84 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
|
85 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
|
86 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
|
87 } |
0 | 88 } |
89 | |
90 void printTree(struct Node* root) { | |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
91 if (root == NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
92 return; |
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 |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
95 printNode(root); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
96 fprintf(stderr, " down: "); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
97 if (root->down != NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
98 printTree(root->down); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
99 } else { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
100 fprintf(stderr, "NULL\n"); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
101 } |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
102 fprintf(stderr, " right: "); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
103 if (root->right != NULL) { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
104 printTree(root->right); |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
105 } else { |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
106 fprintf(stderr, "NULL\n"); |
0 | 107 } |
108 } | |
109 | |
110 | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
111 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
112 |
3
15d7d6b9766f
added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents:
2
diff
changeset
|
113 |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
114 /* 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
|
115 void push(struct Node* node) { |
10 | 116 struct Stackitem* tmp; |
117 struct Stackitem* new; | |
118 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
|
119 new->node = node; |
10 | 120 tmp = stack; |
121 stack = new; | |
122 stack->next = tmp; | |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
123 } |
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
|
124 struct Node* pull() { |
10 | 125 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
|
126 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
|
127 } |
10 | 128 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
|
129 struct Node* node; |
10 | 130 tmp = stack; |
131 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
|
132 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
|
133 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
|
134 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
|
135 } |
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 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
137 |
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
138 |
6 | 139 /* read input */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
140 void read_input(char* filename) { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
141 int c; |
8 | 142 int indent; |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
143 char name[256]; |
6 | 144 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
|
145 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
|
146 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
|
147 struct Node* node; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
148 FILE* file; |
6 | 149 |
8 | 150 indent = 0; |
151 strcpy(name, ""); | |
152 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
|
153 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
|
154 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
|
155 last_node = root; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
156 file = fopen(filename, "r"); |
6 | 157 |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
158 while ((c = getc(file)) != EOF) { |
8 | 159 if (c == '#') { /* comment */ |
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)) != '\n') { |
7
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 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
163 |
8 | 164 if (c == ' ' || c == '\t') { /* indent if at start of line */ |
165 if (strlen(name) == 0) { | |
166 indent++; | |
167 } | |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
168 } |
6 | 169 |
8 | 170 if (c == '\n') { /* end of line: create node */ |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
171 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
|
172 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
173 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
|
174 } |
8 | 175 /* 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
|
176 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
|
177 if (indent > last_indent) { /* down */ |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
178 /* if it goes more than one level down -> error */ |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
179 if (indent - last_indent > 1) { |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
180 fprintf(stderr, "error: Indention over more than one level. Only indent by one!\n"); |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
181 exit(50); |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
182 } |
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
|
183 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
|
184 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
|
185 } 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
|
186 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
|
187 } 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
|
188 /* 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
|
189 while (indent < last_indent) { |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
190 last_node = pull(); |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
191 last_indent--; |
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
192 } |
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
|
193 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
|
194 } |
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
|
195 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
|
196 last_node = node; |
6 | 197 } |
198 indent = 0; | |
199 strcpy(name, ""); | |
200 value = 0; | |
201 } | |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
202 |
8 | 203 if (c >= 'a' && c <= 'z') { /* name */ |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
204 int i = 1; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
205 name[0] = (char) c; |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
206 while ((c = getc(file)) != '(') { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
207 name[i] = (char) c; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
208 i++; |
8 | 209 if (i > 255) { |
210 break; | |
211 } | |
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 name[i] = '\0'; |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
214 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
215 |
8 | 216 if (c == '(') { /* value */ |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
217 fscanf(file, "%d)", &value); |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
218 } |
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
219 |
6 | 220 } |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
221 |
30
cd979b979610
fixed multiple (un)indentions in read_input; some better comments
meillo@marmaro.de
parents:
29
diff
changeset
|
222 /* clear stack */ |
27
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
223 while (stack != NULL) { |
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
224 pull(); |
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
225 } |
1c3dd1e88bdf
empty stack at the end (thanks valgrind to show me this)
meillo@marmaro.de
parents:
26
diff
changeset
|
226 |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
227 fclose(file); |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
228 } |
12
8e34daa80f64
input is now read from file again (because of input node)
meillo@marmaro.de
parents:
10
diff
changeset
|
229 |
26
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
230 |
6 | 231 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
232 /* main */ |
0 | 233 int main(int argc, char* argv[]) { |
7
6a6152cf63f7
new implementation of the read input function
meillo@marmaro.de
parents:
6
diff
changeset
|
234 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
|
235 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
236 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
|
237 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
|
238 printf("\ |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
239 baum %s\n\ |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
240 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
|
241 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
|
242 http://prog.marmaro.de/baum\n\ |
f0856c177403
removed obsolete stuff; only relevant stuff is extern now; refactoring
meillo@marmaro.de
parents:
15
diff
changeset
|
243 ", VERSION); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
244 exit(0); |
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], "--help") == 0) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
246 printf("\ |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
247 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
|
248 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
|
249 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
|
250 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
|
251 "); |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
252 exit(0); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
253 } 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
|
254 option_check = 1; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
255 } 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
|
256 option_verbose = 1; |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
257 /* |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
258 } else if (strcmp(argv[0], "-W") == 0) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
259 / * 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
|
260 iDWarn = atoi((++argv)[0]); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
261 argc--; |
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 } else { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
264 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
|
265 exit(127); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
266 } |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
267 } |
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 if (argc != 1) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
270 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
|
271 exit(3); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
272 } |
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 read_input(argv[0]); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
275 |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
276 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
277 printTree(root); |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
278 } |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
279 |
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
|
280 shell_return = action(root); |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
281 |
13
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
282 if (option_verbose) { |
bf660b45bba9
added commandline options (-v, -c, file); logging only if -v is set
meillo@marmaro.de
parents:
12
diff
changeset
|
283 printTree(root); |
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 |
1
3da0ff17c8e7
added features (print, sum, number); split in header file
meillo@marmaro.de
parents:
0
diff
changeset
|
286 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
|
287 exit(shell_return); |
0 | 288 } |