view baum.c @ 4:24a697f37e7c

trivial output improvement
author meillo@marmaro.de
date Thu, 07 Feb 2008 18:05:03 +0100
parents 15d7d6b9766f
children c202ccccedb5
line wrap: on
line source

/*
 * baum - an esoteric programming language
 *
 * (c) markus schnalke <meillo@marmaro.de>
 * and julian forster
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "baum.h"
#include "actions.h"


struct Node* root;


void logit(char* text) {
	fprintf(stderr, "[%s]\n", text);
}


/* new */
struct Node* newNode(char* name, unsigned char value) {
	struct Node* node;
	node = (struct Node*) malloc(sizeof(struct Node));
	node->name = name;
	node->value = value;
	node->right = 0;
	node->down = 0;
	return node;
}


void setValue(struct Node* node, unsigned char value) {
	node->value = value;
}


struct Node* nextNode(struct Node* node) {
	return node->right;
}

struct Node* lastNode(struct Node* node) {
	while (node->right != NULL) {
		node = node->right;
	}
	return node;
}

void insertLast(struct Node* node, struct Node* insert) {
	node = lastNode(node);
	node->right = insert;
}

/* delete */
void delete(struct Node* node) {
	if (node->down != NULL) {
		delete(node->down);
	}
	if (node->right != NULL) {
		delete(node->right);
	}
	free(node); node=0;
}


/* print */
void printNode(struct Node* node) {
	printf("Node: %20s (%d|%c)\n", node->name, node->value, node->value);
}

void printTree(struct Node* root) {
	printNode(root);
	printf("  down: ");
	if (root->down != NULL) {
		printTree(root->down);
	} else {
		printf("NULL\n");
	}
	printf("  right: ");
	if (root->right != NULL) {
		printTree(root->right);
	} else {
		printf("NULL\n");
	}
}



/* traverse */
void traverse(struct Node* root) {
	/* each node controlls the nodes below itself */
	action(root);
}

/* init */
void init() {
	/* add some numbers
	root = newNode("print", 0);
	root->down = newNode("sum", 0);

	root->down->down = newNode("number", 60);

	root->down->down->right = newNode("number", 50);

	root->down->down->right->right = newNode("sum", 0);
	root->down->down->right->right->down = newNode("number", 1);
	root->down->down->right->right->down->right = newNode("number", 5);
	*/

	/* input numbers and add them */
	root = newNode("print", 0);
	root->down = newNode("sum", 0);
	root->down->down = newNode("input", 0);
	root->down->down->right = newNode("input", 0);
	root->down->down->right->right = newNode("input", 0);
}


/* main */
int main(int argc, char* argv[]) {
	init();

	printTree(root);
	printf("\n\n");

	action(root);

	printf("\n\n");
	printTree(root);

	delete(root);
	
	return(0);
}