-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.c
More file actions
107 lines (101 loc) · 2.82 KB
/
expression.c
File metadata and controls
107 lines (101 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "expression.h"
Expression * create(Type type){
Expression * e;
switch(type){
case INT: e = createInt(0); break;
case DOUBLE: e = createDouble(0.0); break;
case CHAR: e = createChar('\0'); break;
case BOOL: e = createBool(FALSE); break;
default :
e = (Expression *) malloc(sizeof(Expression));
e->type = UNKNOWN;
}
return e;
}
Expression * createInt(int value){
Expression* e = (Expression *) malloc(sizeof(Expression));
e->type = INT;
e->value._int = value;
return e;
}
Expression * createDouble(double value){
Expression* e = (Expression *) malloc(sizeof(Expression));
e->type = DOUBLE;
e->value._double = value;
return e;
}
Expression * createChar(char value){
Expression* e = (Expression *) malloc(sizeof(Expression));
e->type = CHAR;
e->value._char = value;
return e;
}
Expression * createBool(Bool value){
Expression *e = (Expression *) malloc(sizeof(Expression));
e->type = BOOL;
e->value._bool = value;
return e;
}
EXIT_CODE getInt(Expression * expression, int* value){
EXIT_CODE code = TYPE_DOESNT_AGREE;
if(expression->type == INT){
*value = expression->value._int;
code = SUCCESS;
}
return code;
}
EXIT_CODE getDouble(Expression * expression, double *value){
EXIT_CODE code = TYPE_DOESNT_AGREE;
if(expression->type == DOUBLE){
*value = expression->value._double;
code = SUCCESS;
}
return code;
}
EXIT_CODE getChar(Expression * expression, char *value){
EXIT_CODE code = TYPE_DOESNT_AGREE;
if(expression->type == CHAR){
*value = expression->value._char;
code = SUCCESS;
}
return code;
}
EXIT_CODE getBool(Expression * expression, Bool *value){
EXIT_CODE code = TYPE_DOESNT_AGREE;
if(expression->type == BOOL){
*value = expression->value._bool;
code = SUCCESS;
}
return code;
}
Type getType(Expression* e){
return e->type;
}
void printExpression(Expression *e){
printf("[");
int i;
double d;
char c;
Bool b;
switch(e->type){
case INT:
getInt(e, &i);
printf("%d : Entero",i);
break;
case DOUBLE:
getDouble(e,&d);
printf("%lf : Decimal",d);
break;
case CHAR:
getChar(e,&c);
printf("%c : Caracter",c);
break;
case BOOL:
getBool(e, &b);
printf("%s : Predicado",b == TRUE ?"Verdadero":"Falso");
break;
default:
printf("Undefined");
}
printf("]\n");
}