Mercurial > baum
view actions.c @ 61:73de2151aebd
better code for long strings
author | meillo@marmaro.de |
---|---|
date | Thu, 13 Nov 2008 13:15:11 +0100 |
parents | 6279e5b14d9e |
children | 21ff1783f640 |
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; 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; }