baum

annotate baum.c @ 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)
author meillo@marmaro.de
date Fri, 08 Feb 2008 20:45:17 +0100
parents 24a697f37e7c
children ab87b154a96b
rev   line source
meillo@0 1 /*
meillo@0 2 * baum - an esoteric programming language
meillo@0 3 *
meillo@0 4 * (c) markus schnalke <meillo@marmaro.de>
meillo@0 5 * and julian forster
meillo@0 6 *
meillo@0 7 */
meillo@0 8
meillo@0 9
meillo@0 10 #include <stdio.h>
meillo@0 11 #include <stdlib.h>
meillo@1 12 #include <string.h>
meillo@0 13
meillo@1 14 #include "baum.h"
meillo@2 15 #include "actions.h"
meillo@0 16
meillo@0 17
meillo@5 18 struct Node* root = 0;
meillo@0 19
meillo@0 20
meillo@1 21 void logit(char* text) {
meillo@1 22 fprintf(stderr, "[%s]\n", text);
meillo@1 23 }
meillo@1 24
meillo@1 25
meillo@1 26 /* new */
meillo@3 27 struct Node* newNode(char* name, unsigned char value) {
meillo@0 28 struct Node* node;
meillo@0 29 node = (struct Node*) malloc(sizeof(struct Node));
meillo@0 30 node->name = name;
meillo@3 31 node->value = value;
meillo@0 32 node->right = 0;
meillo@0 33 node->down = 0;
meillo@0 34 return node;
meillo@0 35 }
meillo@0 36
meillo@0 37
meillo@3 38 void setValue(struct Node* node, unsigned char value) {
meillo@3 39 node->value = value;
meillo@3 40 }
meillo@3 41
meillo@3 42
meillo@3 43 struct Node* nextNode(struct Node* node) {
meillo@3 44 return node->right;
meillo@3 45 }
meillo@3 46
meillo@3 47 struct Node* lastNode(struct Node* node) {
meillo@3 48 while (node->right != NULL) {
meillo@3 49 node = node->right;
meillo@3 50 }
meillo@3 51 return node;
meillo@3 52 }
meillo@3 53
meillo@3 54 void insertLast(struct Node* node, struct Node* insert) {
meillo@3 55 node = lastNode(node);
meillo@3 56 node->right = insert;
meillo@3 57 }
meillo@3 58
meillo@1 59 /* delete */
meillo@1 60 void delete(struct Node* node) {
meillo@5 61 if (node != NULL) {
meillo@5 62 if (node->down != NULL) {
meillo@5 63 delete(node->down);
meillo@5 64 }
meillo@5 65 if (node->right != NULL) {
meillo@5 66 delete(node->right);
meillo@5 67 }
meillo@5 68 free(node); node=0;
meillo@0 69 }
meillo@0 70 }
meillo@0 71
meillo@1 72
meillo@1 73 /* print */
meillo@0 74 void printNode(struct Node* node) {
meillo@5 75 if (node != NULL) {
meillo@5 76 fprintf(stderr, "Node: %20s (%d|%c)\n", node->name, node->value, node->value);
meillo@5 77 }
meillo@0 78 }
meillo@0 79
meillo@0 80 void printTree(struct Node* root) {
meillo@5 81 if (root != NULL) {
meillo@5 82 printNode(root);
meillo@5 83 fprintf(stderr, " down: ");
meillo@5 84 if (root->down != NULL) {
meillo@5 85 printTree(root->down);
meillo@5 86 } else {
meillo@5 87 fprintf(stderr, "NULL\n");
meillo@5 88 }
meillo@5 89 fprintf(stderr, " right: ");
meillo@5 90 if (root->right != NULL) {
meillo@5 91 printTree(root->right);
meillo@5 92 } else {
meillo@5 93 fprintf(stderr, "NULL\n");
meillo@5 94 }
meillo@0 95 }
meillo@0 96 }
meillo@0 97
meillo@0 98
meillo@1 99
meillo@1 100 /* traverse */
meillo@1 101 void traverse(struct Node* root) {
meillo@1 102 /* each node controlls the nodes below itself */
meillo@1 103 action(root);
meillo@1 104 }
meillo@1 105
meillo@1 106 /* init */
meillo@1 107 void init() {
meillo@3 108 /* add some numbers
meillo@5 109 root = newNode("print", 'n');
meillo@3 110 root->down = newNode("sum", 0);
meillo@3 111
meillo@3 112 root->down->down = newNode("number", 60);
meillo@3 113
meillo@3 114 root->down->down->right = newNode("number", 50);
meillo@3 115
meillo@3 116 root->down->down->right->right = newNode("sum", 0);
meillo@3 117 root->down->down->right->right->down = newNode("number", 1);
meillo@3 118 root->down->down->right->right->down->right = newNode("number", 5);
meillo@3 119 */
meillo@3 120
meillo@5 121 /* input numbers and add them
meillo@5 122 root = newNode("print", 'n');
meillo@3 123 root->down = newNode("sum", 0);
meillo@3 124 root->down->down = newNode("input", 0);
meillo@3 125 root->down->down->right = newNode("input", 0);
meillo@3 126 root->down->down->right->right = newNode("input", 0);
meillo@5 127 */
meillo@5 128
meillo@5 129 /* prints HelloWorld
meillo@5 130 */
meillo@5 131 struct Node* np = 0;
meillo@5 132
meillo@5 133 root = newNode("blackhole", 0);
meillo@5 134 root->down = newNode("sum", 0);
meillo@5 135 root->down->down = newNode("print", 'c');
meillo@5 136 np = root->down->down;
meillo@5 137 np->down = newNode("number", 'H');
meillo@5 138
meillo@5 139 np->right = newNode("print", 'c');
meillo@5 140 np->right->down = newNode("number", 'e');
meillo@5 141 np = np->right;
meillo@5 142
meillo@5 143 np->right = newNode("print", 'c');
meillo@5 144 np->right->down = newNode("number", 'l');
meillo@5 145 np = np->right;
meillo@5 146
meillo@5 147 np->right = newNode("print", 'c');
meillo@5 148 np->right->down = newNode("number", 'l');
meillo@5 149 np = np->right;
meillo@5 150
meillo@5 151 np->right = newNode("print", 'c');
meillo@5 152 np->right->down = newNode("number", 'o');
meillo@5 153 np = np->right;
meillo@5 154
meillo@5 155 np->right = newNode("print", 'c');
meillo@5 156 np->right->down = newNode("number", ' ');
meillo@5 157 np = np->right;
meillo@5 158
meillo@5 159 np->right = newNode("print", 'c');
meillo@5 160 np->right->down = newNode("number", 'L');
meillo@5 161 np = np->right;
meillo@5 162
meillo@5 163 np->right = newNode("print", 'c');
meillo@5 164 np->right->down = newNode("number", 'y');
meillo@5 165 np = np->right;
meillo@5 166
meillo@5 167 np->right = newNode("print", 'c');
meillo@5 168 np->right->down = newNode("number", 'd');
meillo@5 169 np = np->right;
meillo@5 170
meillo@5 171 np->right = newNode("print", 'c');
meillo@5 172 np->right->down = newNode("number", 'i');
meillo@5 173 np = np->right;
meillo@5 174
meillo@5 175 np->right = newNode("print", 'c');
meillo@5 176 np->right->down = newNode("number", 10);
meillo@5 177 np = np->right;
meillo@5 178
meillo@5 179
meillo@1 180 }
meillo@1 181
meillo@1 182
meillo@1 183 /* main */
meillo@0 184 int main(int argc, char* argv[]) {
meillo@5 185 unsigned char shell_return;
meillo@0 186 init();
meillo@4 187
meillo@0 188 printTree(root);
meillo@5 189 fprintf(stderr, "\n\n");
meillo@1 190
meillo@5 191 shell_return = action(root);
meillo@1 192
meillo@5 193 fprintf(stderr, "\n\n");
meillo@3 194 printTree(root);
meillo@4 195
meillo@1 196 delete(root);
meillo@0 197
meillo@5 198 exit(shell_return);
meillo@0 199 }