-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeGenerator.cpp
338 lines (272 loc) · 10.3 KB
/
CodeGenerator.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Authors: Ivan Lim, Brooke Borges, Chad Lewis
// Class: CS 460, Programming Languages
// Assignment: Project 3
// File: CodeGenerator.cpp
// Date: Spring 2017
#include <stdio.h>
#include <string.h>
#include "CodeGenerator.h"
using namespace std;
CodeGenerator::CodeGenerator( char * file ) {
/********************************************************************************/
/* This function initializes the CodeGenerator object. */
/********************************************************************************/
isMain = false;
int fileNameLength = strlen( file );
// cout << "file name: " << file << endl;
// cout << "length: " << fileNameLength << endl;
char filename[fileNameLength + 1];
strncpy( filename, file, fileNameLength );
filename[fileNameLength - 2] = 'c';
filename[fileNameLength - 1] = 'p';
filename[fileNameLength] = 'p';
filename[fileNameLength + 1] = '\0';
cppFile.open( filename );
writeIncludes();
}
CodeGenerator::~CodeGenerator() {
/********************************************************************************/
/* This function closes the .cpp file and deletes the object. */
/********************************************************************************/
cppFile.close();
}
void CodeGenerator::writeIncludes() {
/********************************************************************************/
/* This function writes the necessary pre-compiler directives to the beginning */
/* of the cpp file. */
/********************************************************************************/
cppFile << "#include <iostream>\n";
cppFile << "#include \"Object.h\"\n\n";
cppFile << "using namespace std;\n\n";
}
void CodeGenerator::defineMain()
{
/********************************************************************************/
/* This function writes the function signature for main. */
/********************************************************************************/
isMain = true;
cppFile << "int main(";
}
void CodeGenerator::defineFunction( string ident )
{
/********************************************************************************/
/* This function sets up the function signature for any function that is not */
/* main. */
/********************************************************************************/
isMain = false;
cppFile << "Object " << ident << "(";
}
void CodeGenerator::endFunction()
{
/********************************************************************************/
/* This function writes the necessary code to end main or any other function. */
/********************************************************************************/
if (isMain)
{
indentCode();
cppFile << "return 0;\n";
minusIndent();
} else
{
indentCode();
cppFile << "return __retVal;\n";
minusIndent();
}
cppFile << "}\n\n";
}
void CodeGenerator::returnIdentifier( string toReturn )
{
/********************************************************************************/
/* This function sets toReturn to the value that will ultimately be returned */
/* from the function. */
/********************************************************************************/
indentCode();
cppFile << "__retVal = Object(" << toReturn << ");\n";
}
void CodeGenerator::returnCreatedObject( string toReturn )
{
/********************************************************************************/
/* This function writes the final return statement of non-main functions. */
/********************************************************************************/
cppFile << "return __retVal;\n";
}
void CodeGenerator::writeCode( string code )
{
/********************************************************************************/
/* This function writes arbitrary code to the .cpp file. */
/********************************************************************************/
cppFile << code;
}
void CodeGenerator::if_beginCondition()
{
/********************************************************************************/
/* This function sets up an if statement. */
/********************************************************************************/
indentCode();
cppFile << "if (";
}
void CodeGenerator::if_endCondition()
{
/********************************************************************************/
/* This function ends the condition portion of an if statement, and opens the */
/* control block with an open curly brace '{'. */
/********************************************************************************/
cppFile << ")\n";
indentCode();
cppFile << "{\n";
addIndent();
}
void CodeGenerator::else_begin()
{
/********************************************************************************/
/* This function sets up an else statement. */
/********************************************************************************/
indentCode();
cppFile << "else\n";
indentCode();
cppFile << "{\n";
addIndent();
indentCode();
}
void CodeGenerator::endControlStructure()
{
/********************************************************************************/
/* This function closes a control block such as if or else. */
/********************************************************************************/
minusIndent();
indentCode();
cppFile << "}\n";
}
void CodeGenerator::newline()
{
/********************************************************************************/
/* This function writes newline code to the .cpp file. */
/********************************************************************************/
indentCode();
cppFile << "cout << endl;\n";
}
void CodeGenerator::callFunction( string functionName )
{
/********************************************************************************/
/* This function calls a function with the name passed to it. */
/********************************************************************************/
cppFile << functionName << "(";
}
void CodeGenerator::callListOp( string listop )
{
/********************************************************************************/
/* This function calls the listop function with the listop as a string passed */
/* to it. */
/********************************************************************************/
cppFile << "listop( \"" << listop << "\", ";
}
void CodeGenerator::addParameter( string parameter )
{
/********************************************************************************/
/* This function adds a parameter to the list of function parameters in params. */
/********************************************************************************/
params.push_back( parameter );
}
void CodeGenerator::endParameters()
{
/********************************************************************************/
/* This function marks the end of a param_list. Params are output to the .cpp */
/* file and the function signature is completed. */
/********************************************************************************/
for ( int i = 0; i < params.size(); i++ )
{
cppFile << "Object ";
if ( i == params.size() - 1 )
{
cppFile << params.at(i);
}
else
{
cppFile << params.at(i) << ", ";
}
}
params.clear();
cppFile << ")\n";
cppFile << "{\n";
if (isMain != 1)
{
addIndent();
indentCode();
minusIndent();
cppFile << "Object __retVal;\n";
}
}
void CodeGenerator::makeObject_begin()
{
/********************************************************************************/
/* This function writes the code necessary to create an object. */
/********************************************************************************/
cppFile << "Object(";
}
void CodeGenerator::makeObject_end()
{
/********************************************************************************/
/* This function marks the end of a created object, and closes the parentheses. */
/********************************************************************************/
cppFile << ")";
}
void CodeGenerator::addToListStack( string listItem )
{
/********************************************************************************/
/* This function adds items to a list stack. */
/********************************************************************************/
listStack.push_back( listItem );
}
void CodeGenerator::endList()
{
/********************************************************************************/
/* This function marks the end of a list, and it writes the entire list to the */
/* .cpp file. */
/********************************************************************************/
cppFile << "(";
for ( int i = 0; i < listStack.size(); i++ )
{
cppFile << listStack.at(i);
if ( i != listStack.size() - 1 )
{
cppFile << " ";
}
}
cppFile << ")";
listStack.clear();
makeObject_end();
}
void CodeGenerator::addIndent()
{
/********************************************************************************/
/* This function increments the amount of indents used on new lines. */
/********************************************************************************/
numTabsIndented++;
}
void CodeGenerator::minusIndent()
{
/********************************************************************************/
/* This function decrements the amount of indents used on new lines. */
/********************************************************************************/
if (numTabsIndented > 0)
{
numTabsIndented--;
}
}
void CodeGenerator::indentCode()
{
/********************************************************************************/
/* This function writes the necessary number of spaces for indentation. */
/********************************************************************************/
for ( int i = 0; i < numTabsIndented; i++ )
{
cppFile << " ";
}
}
void CodeGenerator::displayOutput()
{
/********************************************************************************/
/* This function writes the initial code for (display 'some_thing) */
/********************************************************************************/
cppFile << "cout << ";
}