baum

diff baum.c @ 0:2f71d692d4f9

initial commit
author meillo@marmaro.de
date Thu, 07 Feb 2008 12:51:54 +0100
parents
children 3da0ff17c8e7
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/baum.c	Thu Feb 07 12:51:54 2008 +0100
     1.3 @@ -0,0 +1,70 @@
     1.4 +/*
     1.5 + * baum - an esoteric programming language
     1.6 + *
     1.7 + * (c) markus schnalke <meillo@marmaro.de>
     1.8 + * and julian forster
     1.9 + *
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#include <stdio.h>
    1.14 +#include <stdlib.h>
    1.15 +
    1.16 +
    1.17 +struct Node {
    1.18 +	char* name;
    1.19 +	char value;
    1.20 +	struct Node* down;
    1.21 +	struct Node* right;
    1.22 +};
    1.23 +
    1.24 +struct Node* root;
    1.25 +
    1.26 +
    1.27 +struct Node* newNode(char* name) {
    1.28 +	struct Node* node;
    1.29 +	node = (struct Node*) malloc(sizeof(struct Node));
    1.30 +	node->name = name;
    1.31 +	node->value = 0;
    1.32 +	node->right = 0;
    1.33 +	node->down = 0;
    1.34 +	return node;
    1.35 +}
    1.36 +
    1.37 +void init() {
    1.38 +	root = newNode("print");
    1.39 +	root->down = newNode("sum");
    1.40 +}
    1.41 +
    1.42 +void cleanup(struct Node* node) {
    1.43 +	if (node->down != NULL) {
    1.44 +		cleanup(node->down);
    1.45 +	}
    1.46 +	if (node->right != NULL) {
    1.47 +		cleanup(node->right);
    1.48 +	}
    1.49 +	free(node); node=0;
    1.50 +}
    1.51 +
    1.52 +void printNode(struct Node* node) {
    1.53 +	printf("Node: %20s (%c)\n", node->name, node->value);
    1.54 +}
    1.55 +
    1.56 +void printTree(struct Node* root) {
    1.57 +	printNode(root);
    1.58 +	if (root->down != NULL) {
    1.59 +		printTree(root->down);
    1.60 +	}
    1.61 +	if (root->right != NULL) {
    1.62 +		printTree(root->right);
    1.63 +	}
    1.64 +}
    1.65 +
    1.66 +
    1.67 +int main(int argc, char* argv[]) {
    1.68 +	init();
    1.69 +	printTree(root);
    1.70 +	cleanup(root);
    1.71 +	
    1.72 +	return(0);
    1.73 +}