Avaliacao de expressoes e Atribuicao quase prontas.
[toypasc.git] / base.c
blob3c0a17149bc3ee0db4c4a28d28970ff61cc4c9f7
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "base.h"
6 Type
7 type_get_from_lexeme(const char *lexeme)
9 if (!strcasecmp (lexeme, "Integer"))
10 return INTEGER;
11 else if (!strcasecmp (lexeme, "Boolean"))
12 return BOOLEAN;
13 else if (!strcasecmp (lexeme, "Char"))
14 return CHAR;
15 else
16 return VOID;
19 char *
20 type_get_lexeme(Type type)
22 switch (type) {
23 case INTEGER:
24 return "Integer";
25 case BOOLEAN:
26 return "Boolean";
27 case CHAR:
28 return "Char";
29 default:
30 return "NoneType";
34 void
35 value_print(FILE *file, Value *value, Type type)
37 if (type == INTEGER) {
38 fprintf(file, "%d", value->integer);
39 } else if (type == BOOLEAN) {
40 fprintf(file, "%s", value->boolean ? "true" : "false");
41 } else if (type == CHAR) {
42 fprintf(file, "'%c'", value->character);
46 void
47 value_get(Value *value, Type type, void *val)
49 if (value == NULL) {
50 fprintf(stderr, "base.c: value_get: value == NULL\n");
51 exit(1);
54 if (type == INTEGER) {
55 *((int *) val) = value->integer;
56 } else if (type == BOOLEAN) {
57 *((bool *) val) = value->boolean;
58 } else if (type == CHAR) {
59 *((char *) val) = value->character;
60 } else {
61 fprintf(stderr, "base.c: value_get: unknow type\n");
62 exit(1);
66 void
67 value_set(Value *value, Type type, void *val)
69 if (value == NULL) {
70 fprintf(stderr, "base.c: value_set: value == NULL\n");
71 exit(1);
74 if (type == VOID || val == NULL) {
75 value->integer = 0;
76 } else if (type == INTEGER) {
77 value->integer = *((int *) val);
78 } else if (type == BOOLEAN) {
79 value->boolean = *((bool *) val);
80 } else if (type == CHAR) {
81 value->character = *((char *) val);
82 } else {
83 fprintf(stderr, "base.c: value_set: unknow type\n");
84 exit(1);
88 void
89 value_set_from_int(Value *value, int val)
91 value_set(value, INTEGER, VOID(val));
94 void
95 value_set_from_bool(Value *value, bool val)
97 value_set(value, BOOLEAN, VOID(val));
100 void
101 value_set_from_char(Value *value, char val)
103 value_set(value, CHAR, VOID(val));