view actions.c @ 6:ab87b154a96b

first tries for the read input function
author meillo@marmaro.de
date Fri, 08 Feb 2008 21:44:07 +0100
parents c202ccccedb5
children 0e15841ae111
line wrap: on
line source

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


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

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

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

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

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

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

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

	} else {
		fprintf(stderr, "unknown kind of node");
		exit(1);
	}
}



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) {
	return node->value;
}


unsigned char action_input(struct Node* node) {
	/*
	unsigned char input = (unsigned char) getchar();
	getchar();  /* catches the newline */

	/* reads a number which is treated as ASCII value */
	int input;
	scanf("%d", &input);
	input = input % 256;
	insertLast(node, newNode("number", (char) input));

	return 0;
}


unsigned char action_times(struct Node* node) {
	struct Node* tp;
	unsigned char i;
	tp = node->down;
	for (i; i < node->value; i++) {
		/* deep copy */
	}
	return 0;
}


unsigned char action_blackhole(struct Node* node) {
	action(node->down);
	return 0;
}