annotate baum.c @ 8:495a56e706dc

input is now read from stdin
author meillo@marmaro.de
date Sat, 09 Feb 2008 12:42:11 +0100
parents 6a6152cf63f7
children c020b0d1cfca
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
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
17
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
18 struct Node* root = 0;
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
19
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
20
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
21 void logit(char* text) {
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
22 fprintf(stderr, "[%s]\n", text);
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
23 }
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
24
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
25
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
26 /* new */
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
27 struct Node* newNode(char* name, unsigned char value) {
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
28 struct Node* node;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
29 node = (struct Node*) malloc(sizeof(struct Node));
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
30 node->name = name;
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
31 node->value = value;
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
32 node->right = 0;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
33 node->down = 0;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
34 return node;
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
35 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
36
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
37
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
38 void setValue(struct Node* node, unsigned char value) {
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
39 node->value = value;
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
40 }
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
41
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
42
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
43 struct Node* nextNode(struct Node* node) {
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
44 return node->right;
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
45 }
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
46
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
47 struct Node* lastNode(struct Node* node) {
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
48 while (node->right != NULL) {
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
49 node = node->right;
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
50 }
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
51 return node;
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 void insertLast(struct Node* node, struct Node* insert) {
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
55 node = lastNode(node);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
56 node->right = insert;
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
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
59 /* delete */
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
60 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
61 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
62 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
63 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
64 }
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
65 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
66 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
67 }
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
68 free(node); node=0;
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
69 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
70 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
71
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
72
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
73 /* print */
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
74 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
75 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
76 fprintf(stderr, "Node: %20s (%d|%c)\n", node->name, node->value, node->value);
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
77 }
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
78 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
79
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
80 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
81 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
82 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
83 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
84 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
85 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
86 } 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
87 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
88 }
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 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
90 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
91 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
92 } 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
93 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
94 }
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
95 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
96 }
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
97
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
98
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
99
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
100 /* traverse */
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
101 void traverse(struct Node* root) {
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
102 /* each node controlls the nodes below itself */
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
103 action(root);
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
104 }
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
105
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
106 /* init */
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
107 void init() {
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
108 /* add some numbers
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
109 root = newNode("print", 'n');
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
110 root->down = newNode("sum", 0);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
111
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
112 root->down->down = newNode("number", 60);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
113
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
114 root->down->down->right = newNode("number", 50);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
115
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
116 root->down->down->right->right = newNode("sum", 0);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
117 root->down->down->right->right->down = newNode("number", 1);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
118 root->down->down->right->right->down->right = newNode("number", 5);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
119 */
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
120
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
121 /* input numbers and add them
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
122 root = newNode("print", 'n');
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
123 root->down = newNode("sum", 0);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
124 root->down->down = newNode("input", 0);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
125 root->down->down->right = newNode("input", 0);
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
126 root->down->down->right->right = newNode("input", 0);
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
127 */
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
128
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
129 /* prints HelloWorld
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
130 */
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
131 struct Node* np = 0;
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
132
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
133 root = newNode("blackhole", 0);
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
134 root->down = newNode("sum", 0);
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
135 root->down->down = newNode("print", 'c');
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
136 np = root->down->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
137 np->down = newNode("number", 'H');
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
138
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
139 np->right = newNode("print", 'c');
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
140 np->right->down = newNode("number", 'e');
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
141 np = np->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
142
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
143 np->right = newNode("print", 'c');
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
144 np->right->down = newNode("number", 'l');
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
145 np = np->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
146
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
147 np->right = newNode("print", 'c');
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
148 np->right->down = newNode("number", 'l');
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
149 np = np->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
150
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
151 np->right = newNode("print", 'c');
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
152 np->right->down = newNode("number", 'o');
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
153 np = np->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
154
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
155 np->right = newNode("print", 'c');
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
156 np->right->down = newNode("number", ' ');
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
157 np = np->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
158
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
159 np->right = newNode("print", 'c');
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
160 np->right->down = newNode("number", 'L');
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
161 np = np->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
162
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
163 np->right = newNode("print", 'c');
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
164 np->right->down = newNode("number", 'y');
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
165 np = np->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
166
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
167 np->right = newNode("print", 'c');
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
168 np->right->down = newNode("number", 'd');
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
169 np = np->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
170
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
171 np->right = newNode("print", 'c');
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
172 np->right->down = newNode("number", 'i');
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
173 np = np->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
174
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
175 np->right = newNode("print", 'c');
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
176 np->right->down = newNode("number", 10);
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
177 np = np->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
178
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
179
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
180 }
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
181
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
182
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
183 /* read input */
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
184 void read_input() {
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
185 int c;
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
186 int indent;
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
187 char name[256];
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
188 int value;
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
189
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
190 indent = 0;
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
191 strcpy(name, "");
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
192 value = 0;
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
193
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
194 while ((c = getchar()) != EOF) {
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
195 if (c == '#') { /* comment */
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
196 while ((c = getchar()) != '\n') {
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
197 }
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
198 }
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
199
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
200 if (c == ' ' || c == '\t') { /* indent if at start of line */
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
201 if (strlen(name) == 0) {
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
202 indent++;
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
203 }
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
204 }
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
205
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
206 if (c == '\n') { /* end of line: create node */
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
207 if (strlen(name) > 0) {
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
208 printf(" %d - %s - %d\n", indent, name, value);
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
209 /* create node */
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
210 }
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
211 indent = 0;
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
212 strcpy(name, "");
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
213 value = 0;
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
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
216 if (c >= 'a' && c <= 'z') { /* name */
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
217 int i = 1;
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
218 name[0] = (char) c;
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
219 while ((c = getchar()) != '(') {
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
220 name[i] = (char) c;
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
221 i++;
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
222 if (i > 255) {
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
223 break;
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
224 }
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
225 }
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
226 name[i] = '\0';
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
227 }
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
228
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
229 if (c == '(') { /* value */
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
230 scanf("%d)", &value);
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
231 }
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
232
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
233 }
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
234
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
235 }
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
236
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
237 /* main */
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
238 int main(int argc, char* argv[]) {
7
6a6152cf63f7 new implementation of the read input function
meillo@marmaro.de
parents: 6
diff changeset
239 unsigned char shell_return = 0;
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
240 /*
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
241 init();
4
24a697f37e7c trivial output improvement
meillo@marmaro.de
parents: 3
diff changeset
242
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
243 printTree(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
244 fprintf(stderr, "\n\n");
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
245
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
246 shell_return = action(root);
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
247
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
248 fprintf(stderr, "\n\n");
3
15d7d6b9766f added input; added nextNode, lastNode, insertLast
meillo@marmaro.de
parents: 2
diff changeset
249 printTree(root);
4
24a697f37e7c trivial output improvement
meillo@marmaro.de
parents: 3
diff changeset
250
1
3da0ff17c8e7 added features (print, sum, number); split in header file
meillo@marmaro.de
parents: 0
diff changeset
251 delete(root);
6
ab87b154a96b first tries for the read input function
meillo@marmaro.de
parents: 5
diff changeset
252 */
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
253
8
495a56e706dc input is now read from stdin
meillo@marmaro.de
parents: 7
diff changeset
254 read_input();
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
255 exit(shell_return);
0
2f71d692d4f9 initial commit
meillo@marmaro.de
parents:
diff changeset
256 }