view actions.c @ 53:036779d5da75

"sum" node clears its value now everytime it gets called
author meillo@marmaro.de
date Sun, 02 Mar 2008 16:32:52 +0100
parents 201b4603671a
children 6279e5b14d9e
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_create(struct Node* node);
unsigned char action_times(struct Node* node);
unsigned char action_if(struct Node* node);
unsigned char action_while(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, "create") == 0) {
		return action_create(node);

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

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

	} else if (strcmp(node->name, "while") == 0) {
		return action_while(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;

	node->value = 0;
	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_create(struct Node* node) {
	int input;

	if (node->down == NULL) {
		/* user input */
		printf("input: ");
		scanf("%d", &input);
		input = input % 256;
	} else {
		/* return value */
		input = action(node->down);
	}
	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;
}


unsigned char action_if(struct Node* node) {
	int result = 0;
	char return1;
	char return2;

	if (node->down == NULL || node->down->right == NULL) {
		fprintf(stderr, "error: while requires two sons");
		exit(7);
	}

	return1 = action(node->down);
	return2 = action(node->down->right);

	if (node->value == '!') { /* 33 */
		if (return1 != return2) {
			result = 1;
		}
	} else if (node->value == '<') { /* 60 */
		if (return1 < return2) {
			result = 1;
		}
	} else if (node->value == '>') { /* 62 */
		if (return1 > return2) {
			result = 1;
		}
	} else {
		if (return1 == return2) {
			result = 1;
		}
	}
	return result;
}


unsigned char action_while(struct Node* node) {
	if (node->down == NULL || node->down->right == NULL) {
		fprintf(stderr, "error: while requires two sons");
		exit(7);
	}
	while (action(node->down)) {
		action(node->down->right);
	}
	return 0;
}