-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.c
104 lines (97 loc) · 5.84 KB
/
types.c
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
#include <stdio.h>
#include "ast.h"
#include "types.h"
const Unif SUM[6][6] = {
/* int */ /* float */ /* char* */ /* bool */ /* void */ /* array */
{{INT_TYPE, NONE, NONE}, {REAL_TYPE, I2F, NONE}, {ERROR, NONE, NONE}, {INT_TYPE, NONE, B2I}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // int
{{REAL_TYPE, NONE, I2F}, {REAL_TYPE, NONE, NONE}, {ERROR, NONE, NONE}, {REAL_TYPE, NONE, B2F}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // float
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // char*
{{INT_TYPE, B2I, NONE}, {REAL_TYPE, B2F, NONE}, {ERROR, NONE, NONE}, {INT_TYPE, B2I, B2I}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // bool
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // void
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}} // array
};
const Unif MUL[7][7] = {
/* int */ /* float */ /* char* */ /* bool */ /* void */ /* array */
{{INT_TYPE, NONE, NONE}, {REAL_TYPE, I2F, NONE}, {ERROR, NONE, NONE}, {INT_TYPE, NONE, B2I}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // int
{{REAL_TYPE, NONE, I2F}, {REAL_TYPE, NONE, NONE}, {ERROR, NONE, NONE}, {REAL_TYPE, NONE, B2F}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // float
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // char*
{{INT_TYPE, B2I, NONE}, {REAL_TYPE, B2F, NONE}, {ERROR, NONE, NONE}, {INT_TYPE, B2I, B2I}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // bool
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // void
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}} // array
};
const Unif OP[7][7] = {
/* int */ /* float */ /* char* */ /* bool */ /* void */ /* array */
{{BOOL_TYPE, NONE, NONE}, {BOOL_TYPE, I2F, NONE}, {ERROR, NONE, NONE}, {BOOL_TYPE, I2B, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // int
{{BOOL_TYPE, NONE, I2F}, {BOOL_TYPE, NONE, NONE}, {ERROR, NONE, NONE}, {BOOL_TYPE, F2B, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // float
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // char*
{{BOOL_TYPE, NONE, I2B}, {BOOL_TYPE, NONE, F2B}, {ERROR, NONE, NONE}, {BOOL_TYPE, NONE, NONE},{ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // bool
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // void
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}} // array
};
const Unif ANDOR[7][7] = {
/* int */ /* float */ /* char* */ /* bool */ /* void */ /* array */
{{BOOL_TYPE, I2B, I2B}, {BOOL_TYPE, I2B, F2B}, {ERROR, NONE, NONE}, {BOOL_TYPE, I2B, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // int
{{BOOL_TYPE, F2B, I2B}, {BOOL_TYPE, F2B, F2B}, {ERROR, NONE, NONE}, {BOOL_TYPE, F2B, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // float
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // char*
{{BOOL_TYPE, NONE, I2B}, {BOOL_TYPE, NONE, F2B}, {ERROR, NONE, NONE}, {BOOL_TYPE, NONE, NONE},{ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // bool
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // void
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}} // array
};
const Unif ASSIGN_TABLE[7][7] = {
/* int */ /* float */ /* char* */ /* bool */ /* void */ /* array */
{{INT_TYPE, NONE, NONE}, {INT_TYPE, NONE, F2I}, {ERROR, NONE, NONE}, {INT_TYPE, NONE, B2I}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // int
{{REAL_TYPE, NONE, I2F}, {REAL_TYPE, NONE, NONE}, {ERROR, NONE, NONE}, {REAL_TYPE, NONE, B2F}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // float
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {STR_TYPE, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // char*
{{BOOL_TYPE, NONE, I2B}, {BOOL_TYPE, NONE, F2B}, {ERROR, NONE, NONE}, {BOOL_TYPE, NONE, NONE},{ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // bool
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}}, // void
{{ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}, {ERROR, NONE, NONE}} // array
};
static const char *TYPE_STRING[] = {
"int",
"float",
"char*",
"bool",
"void",
"array",
"error"
};
static const int TYPE_VALUE[] = {
0,
1,
2,
3,
4,
5,
6
};
static const char *TYPE_LLVM[] = {
"i32",
"float",
"i8*",
"i1",
"void"
};
const char* get_text(Type type) {
return TYPE_STRING[type];
}
const char* get_llvm_type(Type type) {
return TYPE_LLVM[type];
}
const int get_value(Type type) {
return TYPE_VALUE[type];
}
Unif sum(Type type1, Type type2) {
return SUM[get_value(type1)][get_value(type2)];
}
Unif mul(Type type1, Type type2) {
return MUL[get_value(type1)][get_value(type2)];
}
Unif op(Type type1, Type type2) {
return OP[get_value(type1)][get_value(type2)];
}
Unif logic(Type type1, Type type2) {
return ANDOR[get_value(type1)][get_value(type2)];
}
Unif assign(Type type1, Type type2) {
return ASSIGN_TABLE[get_value(type1)][get_value(type2)];
}