-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.cpp
157 lines (139 loc) · 2.98 KB
/
type.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "type.h"
void property::debugProperty()
{
cout << " prop: " << name << " (a " << dtype->name << ")" << endl;
}
void method::debugMethod()
{
if (rtype) {
cout << " function: " << name << " (returns a " << rtype->name << ")";
} else {
cout << " procedure: " << name;
}
vector<type*>::iterator i;
cout << " params:" << endl;
for (i = ptypes.begin() ; i != ptypes.end(); i++) {
type* t = *i;
cout << " " << t->name << endl;
}
}
void type::numberCode()
{
vector<method*>::iterator i;
for (i = methods.begin() ; i != methods.end(); i++) {
method* t = *i;
t->code->number();
}
}
void type::debugCode()
{
vector<method*>::iterator i;
for (i = methods.begin() ; i != methods.end(); i++) {
method* m = *i;
cout << "---- " << m->name << " ----" << endl;
operation* p = m->code;
while(p) {
p->debug(false);
p = p->next;
}
}
}
void type::debugType()
{
cout << " type: " << name;
if (superclass) {
cout << " (extends " << superclass->name << ")";
}
cout << endl;
vector<property*>::iterator pi;
for (pi = props.begin() ; pi != props.end(); pi++) {
property* p = *pi;
p->debugProperty();
}
vector<method*>::iterator i;
for (i = methods.begin() ; i != methods.end(); i++) {
method* t = *i;
t->debugMethod();
}
}
method* type::getMethodByName(string name)
{
vector<method*>::iterator i;
for (i = methods.begin() ; i != methods.end(); i++) {
method* t = *i;
if (t->name == name) return t;
}
return 0;
}
property* type::getPropertyByName(string name)
{
vector<property*>::iterator i;
for (i = props.begin() ; i != props.end(); i++) {
property* t = *i;
if (t->name == name) return t;
}
return 0;
}
typelibrary::typelibrary()
{
// type(string nm, type* sc, bool pt, bool ar, int sz);
addType(new type("##global", 0, true, false, 0));
addType(new type("int", 0, false, false, 1));
addType(new type("float", 0, false, false, 1));
addType(new type("int*", 0, true, false, 1));
addType(new type("float*", 0, true, false, 1));
}
void typelibrary::debugCode()
{
vector<type*>::iterator i;
for (i = types.begin() ; i != types.end(); i++) {
type* t = *i;
t->debugCode();
}
}
void typelibrary::numberCode()
{
vector<type*>::iterator i;
for (i = types.begin() ; i != types.end(); i++) {
type* t = *i;
t->numberCode();
}
}
void typelibrary::debugTypes()
{
cout << "type library:" << endl;
vector<type*>::iterator i;
for (i = types.begin() ; i != types.end(); i++) {
type* t = *i;
t->debugType();
}
}
void typelibrary::addType(type* t)
{
if (!getTypeByName(t->name)) {
types.push_back(t);
} else {
cout << "error: type already added." << endl;
exit(1);
}
}
type* typelibrary::getTypeByName(string tname)
{
vector<type*>::iterator i;
for (i = types.begin() ; i != types.end() ; i++) {
type* t = *i;
if (t->name == tname) {
return t;
}
}
return 0;
}
type* typelibrary::getOrCreateType(string tname)
{
type* t = getTypeByName(tname);
if (!t) {
t = new type(tname, 0, true, false, 1);
addType(t);
}
return t;
}