view actions.c @ 49:00de718c8590

Added tag 0.3 for changeset f9fc4c4f9e3d666dad2bd1efa627fb646334433c
author meillo@marmaro.de
date Sun, 02 Mar 2008 13:35:42 +0100
parents f9fc4c4f9e3d
children 0870e261bf28
line wrap: on
line source

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

unsigned char action_print(struct Node* node);
unsigned char action_sum(struct Node* node);
unsigned char action_number(struct Node* node);
unsigned char action_input(struct Node* node);
unsigned char action_times(struct Node* node);



unsigned char action(struct Node* node) {
	if (node == NULL) {
		if (option_verbose) {
			fprintf(stderr, "warning: action of non existing node\n");
		}
		return 0;
	}

	if (strcmp(node->name, "print") == 0) {
		return action_print(node);

	} else if (strcmp(node->name, "sum") == 0) {
		return action_sum(node);

	} else if (strcmp(node->name, "number") == 0) {
		return action_number(node);

	} else if (strcmp(node->name, "input") == 0) {
		return action_input(node);

	} else if (strcmp(node->name, "times") == 0) {
		return action_times(node);

	} else {
		fprintf(stderr, "unknown kind of node\n");
		exit(4);
	}
}



unsigned char action_print(struct Node* node) {
	unsigned char result;
	result = action(node->down);
	if (node->value == 'c') {
		printf("%c", result);
	} else {
		printf("%d", result);
	}
	return result;
}


unsigned char action_sum(struct Node* node) {
	struct Node* tp;
	tp = node->down;
	while (tp != NULL) {
		node->value += action(tp);
		tp = tp->right;
	}
	return node->value;
}


unsigned char action_number(struct Node* node) {
	if (node->down != NULL) {
		action(node->down);
	}
	return node->value;
}


unsigned char action_input(struct Node* node) {
	int input;
	printf("input: ");
	scanf("%d", &input);
	input = input % 256;
	insertLast(node, newNode("number", (char) input));
	return 0;
}


unsigned char action_times(struct Node* node) {
	unsigned char i;
	for (i = 0; i < node->value; i++) {
		insertLast(node, copyTree(node->down));
	}
	return 0;
}