-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathyabFunctions.cpp
More file actions
302 lines (285 loc) · 6.91 KB
/
yabFunctions.cpp
File metadata and controls
302 lines (285 loc) · 6.91 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
** Yab2Cpp
**
** Transpiler by Samuel D. Crow
**
** Based on Yab
**
*/
#include "yab2cpp.h"
/* static initializers */
unordered_map<string, unique_ptr<fn> > fn::functions;
unsigned int fn::nextID;
unsigned int callEnumerator=0;
/* function definitions */
void fn::dumpFunctionIDs()
{
varNames << "Function IDs\n";
auto i=functions.begin();
if (i==functions.end())
{
varNames << "no named functions\n";
return;
}
do
{
varNames << "Function " << i->first
<< " has ID f" << i->second.get()->getID() << "\n";
++i;
} while (i!=functions.end());
}
void fn::generateOnNSub(expression *e, unsigned int skip)
{
label *retLabel=new label();
output_cpp << "callStack=new subroutine(" << retLabel->getID() << ");\n";
label::generateOnNTo(e, skip);
retLabel->generate();
}
void fn::generateGosub(label *sub)
{
label *retLabel=new label();
output_cpp << "callStack=new subroutine(" << retLabel->getID() << ");\n";
sub->generateJumpTo();
retLabel->generate();
}
fn *fn::getSub(string &name)
{
auto iter=fn::functions.find(name);
if(iter==fn::functions.end()) return nullptr;
return iter->second.get();
}
variableType *fn::getLocalVar(string &name)
{
auto iter=this->parameters.find(name);
if (iter!=parameters.end()) return iter->second;
auto i=locals.find(name);
if (i!=locals.end()) return i->second.get();
i=statics.find(name);
if (i!=statics.end()) return i->second.get();
return nullptr;
}
void fn::addParameter(string &name, enum TYPES t)
{
variableType *v=new variableType(S_PARAMETER, name, t, this);
v->generateBox(S_PARAMETER);
this->parameters[name]=v;
this->params.push_back(v);
}
/* TODO needs to be broken into smaller pieces */
operands *fn::generateCall(string &name, list<operands *>¶mList)
{
++callEnumerator;
auto v=params.begin();
operands *current;
label *retAddr=new label();
fn *g=fn::getSub(name);
if (g==nullptr)
{
error(E_SUBROUTINE_NOT_FOUND);
}
if (paramList.size() > this->getNumParams())
{
error(E_TOO_MANY_PARAMETERS);
}
heap_h << "struct f" << g->getID()
<< " *sub" << callEnumerator << ";\n";
output_cpp << "sub" << callEnumerator
<< "= new f" << g->getID()
<< "(" << retAddr->getID() << ");\n"
<< "callStack = sub" << callEnumerator << ";\n";
/* TODO Make parameter processing a separate function */
while(paramList.size()>0)
{
current=paramList.front();
paramList.pop_front();
if(current->getSimpleVarType()!=(*v)->getType())
{
cerr << "assigning " << TYPENAMES[current->getType()]
<< " to " << TYPENAMES[(*v)->getType()] << endl;
error(E_TYPE_MISMATCH);
}
cerr << "assigning to parameter in sub" << callEnumerator <<"\n";
(*v)->assignment(new expression(current), true);
++v;
}
/* pad remaining unassigned variables with empty values */
while (v!=params.end())
{
switch ((*v)->getType())
{
case T_FLOATVAR:
(*v)->assignment(new expression(new constOp("0.0", T_FLOAT)));
break;
case T_INTVAR:
(*v)->assignment(new expression(new constOp("0", T_INT)));
break;
case T_STRINGVAR:
(*v)->assignment(new expression(new constOp("", T_STRING)));
default:
error(E_INTERNAL);
}
++v;
}
g->startAddr->generateJumpTo();
retAddr->generate();
return g->rc;
}
/* typeless return for gosub family */
void fn::generateReturn()
{
switch (this->getType())
{
case T_UNKNOWNFUNC:
output_cpp << "state=f" << this->getID()
<< "::close();\nbreak;\n";
break;
case T_GOSUB:
output_cpp << "state=subroutine::close();\nbreak;\n";
break;
default:
error(E_RETURN_CODE_OMITTED);
}
}
void fn::generateReturn(expression *expr)
{
logger("evaluating expression");
this->rc=expr->evaluate();
logger("expression evaluated");
this->kind=rc->getSimpleVarType();
logger("generating return");
output_cpp << "state=f" << this->getID()
<< "::close();\nbreak;\n";
switch (this->getType())
{
case T_UNKNOWNFUNC:
break;
case T_STRINGFUNC:
if (kind!=T_STRINGVAR) error(E_TYPE_MISMATCH);
break;
case T_INTFUNC:
if (kind!=T_INTVAR&&kind!=T_FLOATVAR) error(E_TYPE_MISMATCH);
break;
case T_FLOATFUNC:
if(kind!=T_FLOATVAR) error(E_TYPE_MISMATCH);
break;
case T_GOSUB:
error(E_GOSUB_CANNOT_RETURN_VALUE);
default:
error(E_BAD_SYNTAX);
}
logger("deleting expression");
delete expr;
}
/* not allowed in function definitions directly */
void fn::generateBreak()
{
error(E_BAD_SYNTAX);
}
void fn::close()
{
/* check if no returns and no return type */
enum CODES t=this->getType();
if (this->kind==T_UNKNOWN && t==T_UNKNOWNFUNC)
{
/* generate a typeless return */
this->generateReturn();
}
funcs_h << "};\n";
if(DUMP)
{
auto i=locals.begin();
varNames << "\nLocal variables in function f" << this->getID() << "\n";
if(i==locals.end())
{
varNames << "no non-static locals" << endl;
}
else
{
do
{
varNames << "variable " << i->first << " has id v"
<< i->second.get()->getID() << endl;
++i;
}while(i!=locals.end());
}
i=statics.begin();
varNames << "\nStatic locals in function f" << this->getID() << "\n";
if (i==statics.end())
{
varNames << "no static locals" << endl;
}
else
{
do
{
varNames << "variable " << i->first << " has id v"
<< i->second.get()->getID() << endl;
++i;
} while (i!=statics.end());
}
auto iter=this->parameters.begin();
if (iter==this->parameters.end())
{
varNames << "no parameters passed to function f" << this->getID() << endl;
}
else
{
do
{
varNames << "paramater " << iter->first << " has id v"
<< iter->second->getID() << endl;
++iter;
} while (iter!=this->parameters.end());
}
}
locals.clear();
statics.clear();
skipDef->generate();
currentFunc=nullptr;
scopeGlobal=true;
}
fn *fn::declare(string &s, enum CODES t, operands *returnCode)
{
/* sd is the skipDef of this function */
label *sd=new label();
/* check for nesting error */
if (!scopeGlobal) error(E_END_FUNCTION);
/* check if this function name is already used */
if (fn::functions.find(s)!=fn::functions.end()) error(E_DUPLICATE_SYMBOL);
logger("declaration name cleared");
sd->generateJumpTo();
fn *self=new fn(t, returnCode);
logger("fn allocated");
fn::functions.insert({s,unique_ptr<fn>(self)});
logger("fn inserted");
/* initiate local scope */
self->skipDef=sd;
currentFunc=self;
scopeGlobal=false;
return self;
}
/* standard constructor for block-formatted functions */
fn::fn(enum CODES t, operands *returnCode)
{
this->params.clear();
this->type=t;
this->id= ++nextID;
/*define storage for locals*/
if (t!=T_GOSUB)
{
funcs_h << "struct f" << this->id <<":public subroutine\n{\nf"
<< this->id << "(unsigned int x):subroutine(x)\n{}\n"
<< "virtual ~f" << this->id <<"()\n{}\n";
}
/*keep track of where the return code will be sent to*/
this->rc=returnCode;
/*allocate and generate start address label*/
this->startAddr=new label();
startAddr->generate();
}
/* Destructor is called only at the end of the unique pointer's existence */
fn::~fn()
{
delete startAddr;
delete skipDef;
}