-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.cpp
More file actions
76 lines (56 loc) · 1.59 KB
/
type.cpp
File metadata and controls
76 lines (56 loc) · 1.59 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
#include "type.h"
#include <iostream>
///////////////////
// PrimitiveType //
///////////////////
PrimitiveType::PrimitiveType(const std::string& name, int size): name(name), size(size) {}
PrimitiveType::~PrimitiveType() = default;
std::string PrimitiveType::to_string() {
return name;
}
int PrimitiveType::get_size() {
return size;
}
///////////////
// ArrayType //
///////////////
ArrayType::ArrayType(Type* type, int num_elements): type(type), num_elements(num_elements),
size(type->get_size() * num_elements) {}
ArrayType::~ArrayType() = default;
Type* ArrayType::get_type() {
return type;
}
std::string ArrayType::to_string() {
return "ARRAY " + std::to_string(num_elements) + " OF " + type->to_string();
}
int ArrayType::get_size() {
return size;
}
////////////////
// RecordType //
////////////////
RecordField::RecordField(Type* type, const std::string& name): type(type), name(name) {}
RecordType::RecordType(const std::vector<RecordField*>& fields, SymbolTable* symtab): fields(fields),
size(symtab->get_offset()), symtab(symtab) {}
RecordType::~RecordType() = default;
Type* RecordType::get_field(const std::string& name) {
for (const auto field : fields) {
if (name == field->name) return field->type;
}
return nullptr;
}
std::string RecordType::to_string() {
std::string out = "RECORD (";
for (const auto field : fields) {
out += field->type->to_string() + " x ";
}
out.erase(out.length() - 3);
out += ")";
return out;
}
int RecordType::get_size() {
return size;
}
SymbolTable* RecordType::get_symtab() {
return symtab;
}