annotate baum.c @ 25:6f2c1f9dc08f

Added tag 0.1 for changeset 69a7cf2f0c06a5e0f0609f80c875e2ceb7ffa9c2
author meillo@marmaro.de
date Wed, 13 Feb 2008 22:04:50 +0100
parents e2048e569891
children f0856c177403
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
1 /*
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
2 * baum - an esoteric programming language
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
3 *
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
4 * (c) markus schnalke <meillo@marmaro.de>
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
5 * and julian forster
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
6 *
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
7 */
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
8
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
9
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
10 #include <stdio.h>
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
11 #include <stdlib.h>
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
12 #include <string.h>
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
13
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
14 #include "baum.h"
2
557fa4df2bcd added difference between char and number
meillo@marmaro.de
parents: 1
diff changeset
15 #include "actions.h"
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
16
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
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
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
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
23 struct Stackitem* stack = NULL;
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
24
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
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
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
35 struct Node* node;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
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
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
39 node->right = 0;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
40 node->down = 0;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
41 return node;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
42 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
43
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
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
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
77 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
78 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
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
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
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
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
86 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
87
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
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
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
103 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
104 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
105
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
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
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
118 struct Stackitem* tmp;
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
119 struct Stackitem* new;
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
120 new = (struct Stackitem*) malloc(sizeof(struct Stackitem));
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
121 new->node = node;
10
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
122 tmp = stack;
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
123 stack = new;
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
124 stack->next = tmp;
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
125 }
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
126 struct Node* pull() {
10
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
127 if (stack == NULL) {
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
128 return NULL;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
129 }
10
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
130 struct Stackitem* tmp;
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
131 struct Node* node;
10
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
132 tmp = stack;
0e15841ae111 s/list/stack/g because thats what it is
meillo@marmaro.de
parents: 9
diff changeset
133 stack = stack->next;
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
134 node = tmp->node;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
135 free(tmp); tmp=0;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
136 return node;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
137 }
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
138
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
139
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
140
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
141 /* read input */
12
8e34daa80f64 input is now read from file again (because of input node)
meillo@marmaro.de
parents: 10
diff changeset
142 void read_input(char* filename) {
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
143 int c;
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
144 int indent;
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
145 char name[256];
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
146 int value;
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
147 int last_indent;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
148 struct Node* last_node;
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
149 struct Node* node;
12
8e34daa80f64 input is now read from file again (because of input node)
meillo@marmaro.de
parents: 10
diff changeset
150 FILE* file;
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
151
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
152 indent = 0;
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
153 strcpy(name, "");
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
154 value = 0;
9
c020b0d1cfca read input is now finished in first version; removed init; added push and pull; name is now array instead of pointer
meillo@marmaro.de
parents: 8
diff changeset
155 last_indent = -1;
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
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
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
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
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
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
166 if (c == ' ' || c == '\t') { /* indent if at start of line */
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
167 if (strlen(name) == 0) {
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
168 indent++;
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
169 }
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
170 }
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
171
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
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
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
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
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
191 }
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
192 indent = 0;
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
193 strcpy(name, "");
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
194 value = 0;
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
195 }
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
196
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
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
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
203 if (i > 255) {
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
204 break;
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
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
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
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
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
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
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
218 }
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
219
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
220 /* main */
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
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
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
274 }