Mercurial > baum
view actions.c @ 58:f5f06d6f62b3
made Makefile much simpler (installs now to /usr/local)
author | meillo@marmaro.de |
---|---|
date | Wed, 25 Jun 2008 11:09:14 +0200 |
parents | 21ff1783f640 |
children |
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); struct action { char* name; unsigned char (*func)(struct Node* node); }; struct action actions[] = { {"print", action_print}, {"sum", action_sum}, {"number", action_number}, {"create", action_create}, {"times", action_times}, {"if", action_if}, {"while", action_while}, }; unsigned char action(struct Node* node) { int i; if (node == NULL) { if (option_verbose) { fprintf(stderr, "warning: action of non existing node\n"); } return 0; } for (i = 0; i < (sizeof(actions)/sizeof(actions[0])); i++) { if (strcmp(actions[i].name, node->name) == 0) { return (actions[i].func)(node); } } 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; for (tp = node->down; tp != NULL; tp = tp->right) { node->value += action(tp); } 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; }