Mercurial > baum
view actions.c @ 3:15d7d6b9766f
added input; added nextNode, lastNode, insertLast
author | meillo@marmaro.de |
---|---|
date | Thu, 07 Feb 2008 16:15:07 +0100 |
parents | 557fa4df2bcd |
children | c202ccccedb5 |
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 (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, "printchar") == 0) { logit("printchar-node"); return action_printchar(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 { fprintf(stderr, "unknown kind of node"); exit(1); } } unsigned char action_print(struct Node* node) { printf("%d\n", action(node->down)); return 0; } unsigned char action_printchar(struct Node* node) { printf("%c\n", action(node->down)); return 0; } 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 */ insertLast(node, newNode("number", input)); return 0; }