-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessor.cpp
718 lines (687 loc) · 28.9 KB
/
preprocessor.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
#include "preprocessor.h"
#define DEBUG 1
// includes const
static bool is_type_specifier(int token) {
return token == VOID || token == CHAR
|| token == INT || token == LONG
|| token == FLOAT || token == DOUBLE
|| token == SIGNED || token == UNSIGNED
|| token == TYPE_NAME || token == CONST
|| token == '*' || token == CLASS_NAME;
}
static void error(const char *message) {
perror(message);
exit(EXIT_FAILURE);
}
static std::string unique_function_identifier(std::string scope, std::string identifier, std::vector<std::string> arguments) {
std::string result = "_" + scope + "_" + identifier;
// puts("\n\n\n\t\tUNIQUE_FUNCTION_IDENTIFIER\n\n\n");
for (size_t i = 0; i < arguments.size(); i++) {
result += "_" + arguments[i];
if (result.back() == '*') {
result.pop_back();
result += "ptr";
}
// printf("arguments[%lu]: %s\n", i, arguments[i].c_str());
}
// printf("result: %s\n", result.c_str());
return result;
}
static std::string unique_function_identifier(std::string scope, std::string identifier, std::vector<Parameter> params) {
std::vector<std::string> arguments;
for (size_t i = 0; i < params.size(); i++) {
std::string temp;
for (size_t j = 0; j < params[i].specifiers.size(); j++) {
if (j > 0) temp.push_back('_');
temp += params[i].specifiers[j]->value;
}
arguments.push_back(temp);
}
return unique_function_identifier(scope, identifier, arguments);
}
static std::string unique_class_identifier(std::string identifier) {
return "_" + identifier;
}
Parameter::Parameter(std::string identifier, std::vector<MToken *>specifiers) {
this->name = identifier;
this->specifiers = specifiers;
}
Function::Function(std::string scope, std::vector<MToken *> specifiers, std::string identifier, std::vector<Parameter> params, std::vector<MToken *> body_tokens) {
this->scope = scope;
this->body_tokens = body_tokens;
this->identifier = identifier;
this->params = params;
this->specifiers = specifiers;
}
void Function::print() {
printf("Class: %s\n", identifier.c_str());
printf("specifiers: ");
for(size_t i = 0; i < specifiers.size(); i++) {
specifiers[i]->print();
}
}
Preprocessor::Preprocessor(std::string filename) {
FILE *fp = fopen(filename.c_str(), "r");
if (fp) {
MToken **tks = lex(fp, &tokenCount, ¯o_text);
tokens = std::vector<MToken *>(tks, tks + tokenCount);
#if DEBUG
printf("tokenCount: %lu\n", tokenCount);
for (size_t i = 0; i < tokenCount; i++)
printf("%s\n", stringRepresentation(tokens[i]));
#endif
} else {
fprintf(stderr, "Error with opening file %s\n", filename.c_str());
exit(EXIT_FAILURE);
}
cursor = 0;
}
// have to do a preliminary pass to gather user-defined types
// second pass to gather type info for variables
void Preprocessor::process(std::string outfile) {
first_pass(); // gather user defined types
second_pass(); // gather type info for all variables
std::string result = macro_text + "#include \"mlib.h\"\n", scope, temp, class_definition_string = "#define bool int\n#define true 1\n#define false 0\n";
// std::vector<MToken *> tokens;
// third pass - replace class definitions with appropriate code
std::vector<MToken *> specifiers;
Function * function_ptr = nullptr;
for(cursor = 0; cursor < tokenCount; cursor++) {
loop:
if (!(temp = class_declaration()).empty()) {
result += temp + "\n";
goto loop;
}
if (tokens[cursor]->type == IDENTIFIER) {
std::string scope, identifier, variable_identifier = tokens[cursor]->value;
// 2 cases
int type = tokens[cursor + 1]->type;
if ((type == PTR_OP || type == '.') && tokens[cursor + 2]->type == IDENTIFIER) {
identifier = tokens[cursor + 2]->value;
printf("\n\nit happed with identifier: %s\n", identifier.c_str());
scope = variable_types[variable_identifier];
if (scope.empty()) {
result += variable_identifier;
continue;
}
std::vector<std::string> arguments;
std::vector<std::string> argument_types;
// function
if (tokens[cursor + 3]->type == '(') {
collect_function_arguments(cursor += 3, tokens, &arguments, &argument_types);
// generate full method identifier and make sure it's valid
identifier = unique_function_identifier(scope, identifier, argument_types);
printf("\nidentifier: %s\n\n", identifier.c_str());
if (function_info[identifier]) {
result += identifier + (type == PTR_OP ? "(" : "(&") + variable_identifier;
for (size_t j = 0; j < arguments.size(); j++) {
result += ", " + arguments[j];
}
result += ")";
} else {
error("Invalid method identifier 1");
}
} else { // variable
cursor += 2;
result += type == PTR_OP ? "->" : ".";
result += identifier + " ";
}
} else {
result += variable_identifier;
}
} else if (tokens[cursor]->type == CLASS_NAME) { // static
printf("it was a class name");
result += unique_class_identifier(tokens[cursor]->value);
result += " ";
// deal with this in second version
} else {
printf("token/type: %s/%d\n", tokens[cursor]->value, tokens[cursor]->type);
result += tokens[cursor]->value;
if (tokens[cursor]->type == ';') {
result.push_back('\n');
} else if (tokens[cursor]->type != '*')
result.push_back(' ');
}
}
// replace variable and method usage with appropriate name and pointers
// for (cursor = 0; cursor < new_tokens.size(); cursor++) {
// }
std::string output = class_definition_string + result;
// need to make lexer lex a string, not just a file
FILE *fp = fopen(outfile.c_str(), "w+");
fwrite(output.c_str(), 1, output.length(), fp);
fclose(fp);
}
// add inheritance later
std::string Preprocessor::class_declaration() {
puts("class declaration called");
bool deinit_found_flag = false;
if (accept(CLASS) && accept(CLASS_NAME)) {
#if DEBUG
puts("\n\n\n\t\tINSIDE CLASS DECLARATION\n\n\n");
#endif
std::string super_class_identifier, identifier, item_string, function_declarations, class_body = "typedef struct {\n\t";
// here the identifier and super identifier are in the original format
if (accept('{')) {
identifier = tokens[cursor - 2]->value;
super_class_identifier = "MObject";
} else if (accept(':') && accept(CLASS_NAME) && accept('{')) {
super_class_identifier = tokens[cursor - 2]->value;
identifier = tokens[cursor - 4]->value;
} else {
error("Error: Syntax error");
}
super_class[identifier] = super_class_identifier; // this is correct
super_class_identifier = unique_class_identifier(super_class_identifier);
class_body += super_class_identifier + ";\n";
while(1) {
bool outside_class_body = false;
// passing original identifier to class item
if (!(item_string = class_item(identifier, &outside_class_body)).empty()) {
printf("item string: %s\n", item_string.c_str());
// if back of string is semicolon, it's a variable dec
if (outside_class_body) {
function_declarations += item_string;
} else {
class_body += '\t' + item_string;
}
} else if (!(item_string = initializer()).empty()) {
function_declarations += item_string;
} else if (!deinit_found_flag && !(item_string = deinitializer()).empty()) {
deinit_found_flag = true;
function_declarations += item_string;
} else if (deinit_found_flag && !(item_string = deinitializer()).empty()) {
error("Error: cannot have multiple deinitializers");
} else if (accept('}')) {
return class_body + "} " + unique_class_identifier(identifier) + ";\n" + function_declarations;
} else {
error("Class contains invalid stuff");
}
}
} else {
return "";
}
}
// Variable definition has to be pasted into the generated initializer later
// I will implement that at a later time
// I still have to implement part of the preprocessor myself if that is going to work
std::string Preprocessor::class_item(std::string class_name, bool *outside_class_body) {
#if DEBUG
puts("class_item");
#endif
std::string result;
std::vector<MToken *> specifiers = declaration_specifiers();
if (specifiers.size() > 0) {
// check for return type
bool contains_return_type = false, is_private = false;
std::string identifier;
for (size_t i = 0; i < specifiers.size(); i++) {
if (!contains_return_type && is_type_specifier(specifiers[i]->type)) {
contains_return_type = true;
break;
} else if (!is_private && specifiers[i]->type == PRIVATE) {
is_private = true;
} else if (is_private && specifiers[i]->type == PRIVATE) {
error("Error: multiple access specifiers");
} else if (!(*outside_class_body) && specifiers[i]->type == STATIC) {
puts("STATIC FOUND");
*outside_class_body = true;
} else if (*outside_class_body && specifiers[i]->type == STATIC) {
error("Error: duplicate use of 'static' keyword");
}
}
assert(contains_return_type);
// get identifier
if (accept(IDENTIFIER)) {
identifier = tokens[cursor - 1]->value;
} else {
error("Error: No identifier");
}
if (is_private) {
private_identifiers[identifier] = class_name; // original class name
}
for(size_t i = 0; i < specifiers.size(); i++) {
if (specifiers[i]->type != PRIVATE && specifiers[i]->type != STATIC) {
if (i > 0) result += " ";
result += std::string(specifiers[i]->value);
#if DEBUG
specifiers[i]->print();
#endif
}
}
// distinguish between variable dec, variable def, or function def
if (accept(';')) { // variable declaration
result += " " + identifier;
variable_types[identifier] = class_name;
printf("variable: scope -> %s: %s\n", identifier.c_str(), class_name.c_str());
return result + ";\n";
} else if (accept(',')) {
result += " " + identifier;
variable_types[identifier] = class_name;
printf("variable: scope -> %s: %s\n", identifier.c_str(), class_name.c_str());
for (;cursor < tokenCount; cursor++) {
if (accept(';')) {
break;
} else if (tokens[cursor]->type == '*') {
assert(tokens[++cursor]->type == IDENTIFIER);
variable_types[tokens[cursor]->value] = class_name;
result += ", *";
result += tokens[cursor]->value;
} else if (tokens[cursor]->type == IDENTIFIER) {
variable_types[tokens[cursor]->value] = class_name;
result += ", ";
result += tokens[cursor]->value;
} else if (tokens[cursor]->type == ',') {
continue;
} else {
error("Invalid token in variable declaration list");
}
}
return result + ";\n";
} else if (accept('(')) { // function definition
std::vector<Parameter> params = parameter_list();
std::string method_name, parameter_string = unique_class_identifier(class_name) + " *self";
for (size_t i = 0; i < params.size(); i++) {
parameter_string += ", ";
for (size_t j = 0; j < params[i].specifiers.size(); j++) {
if (j > 0) parameter_string += " ";
if (params[i].specifiers[i]->type == CLASS_NAME) {
parameter_string += unique_class_identifier(params[i].specifiers[j]->value);
} else {
parameter_string += params[i].specifiers[j]->value;
}
}
parameter_string += params[i].name;
}
if (accept(')') && accept('{')) {
// get body tokens
size_t open_brack_count = 1;
std::vector<MToken *> body_tokens;
while(1) {
if (accept('{')) {
open_brack_count++;
} else if (accept('}')) {
if (--open_brack_count == 0) {
break;
}
} else {
body_tokens.push_back(tokens[cursor++]);
}
}
// original scope here as well
method_name = unique_function_identifier(class_name, identifier, params);
printf("method_name: %s\n", method_name.c_str());
result += " " + method_name;
// original scope
Function *function = new Function(class_name, specifiers, identifier, params, body_tokens);
function_info[method_name] = function;
result += "(" + parameter_string + ") {\n\t" + generate_function_body(function);
result.pop_back();
result += "}\n";
*outside_class_body = true;
return result;
} else {
error("Error: Syntax error in function definition");
}
} else if (accept('=')) {
// push to a queue for initialization inside initializer then return result
//TODO: skip for now. Will be implemented in version 2
error("Error: Not implemented yet");
} else {
error("Error: Invalid token");
}
} else {
return result;
}
return result;
}
std::string Preprocessor::initializer() {
std::string result;
if (accept(INIT)) {
printf("init\n");
}
return result;
}
std::string Preprocessor::deinitializer() {
std::string result;
if (accept(DEINIT)) {
printf("deinit\n");
}
return result;
}
std::vector<MToken *> Preprocessor::declaration_specifiers() {
#if DEBUG
puts("declaration_specifiers");
#endif
std::vector<MToken *> result;
MToken *token = nullptr;
while(1) {
printf("decspec loop token: %s\n", tokens[cursor]->value);
if (accept(TYPEDEF)) {
// skip typedefs completely
while(cursor < tokenCount && tokens[cursor++]->type != ';');
// printf("\n\n\n\n\n HERE %s \n\n\n\n", tokens[cursor]->value);
// return result;
} else if ((token = type_qualifier())) {
result.push_back(token);
} else if ((token = storage_class_specifier())) {
result.push_back(token);
} else if ((token = type_specifier())) {
result.push_back(token);
} else if (accept('*')) {
result.push_back(tokens[cursor - 1]);
} else {
if (!result.empty()) {
puts("\n\n\tDECLARATION SPECIFIERS FOUND\n\n");
for (size_t i = 0; i < result.size(); i++)
printf("%s ", result[i]->value);
printf("\n");
}
return result;
}
}
}
std::vector<Parameter> Preprocessor::parameter_list() {
std::vector<Parameter> result;
std::vector<MToken *> specifiers;
std::string identifier;
while(1) {
specifiers = declaration_specifiers();
if (specifiers.size() > 0) {
if (accept(IDENTIFIER)) {
identifier = tokens[cursor - 1]->value;
result.push_back(Parameter(identifier, specifiers));
if (accept(',')) {
continue;
} else {
return result;
}
} else {
error("Error: expected identifier");
}
} else {
return result;
}
}
}
MToken * Preprocessor::type_specifier() {
if (accept(VOID) || accept(CHAR)
|| accept(SHORT) || accept(INT)
|| accept(LONG) || accept(FLOAT)
|| accept(DOUBLE) || accept(SIGNED)
|| accept(UNSIGNED) || accept(TYPE_NAME)
|| accept(CLASS_NAME)) {
return tokens[cursor - 1];
} else {
printf("returning nullptr for token: %s\n", tokens[cursor]->value);
return nullptr;
}
}
MToken * Preprocessor::type_qualifier() {
if (accept(PRIVATE) || accept(CONST) || accept(VOLATILE)) {
return tokens[cursor - 1];
} else {
return nullptr;
}
}
MToken * Preprocessor::storage_class_specifier() {
if (accept(EXTERN) || accept(STATIC) || accept(AUTO) || accept(REGISTER)) {
return tokens[cursor - 1];
} else {
return nullptr;
}
}
int Preprocessor::accept(int token) {
if (tokens[cursor]->type == token) {
#if DEBUG
printf("Accepted token %d\n", token);
#endif
cursor++;
return 1;
} else {
return 0;
}
}
void Preprocessor::unaccept() {
assert(--cursor - 1 >= 0);
#if DEBUG
printf("unaccepted token %d\n", tokens[cursor]->type);
#endif
}
// anonymous classes or class typedefs are not allowed
// replace IDENTIFIER with TYPE_NAME for user defined types
void Preprocessor::first_pass() {
#if DEBUG
puts("first_pass called");
#endif
std::map<std::string, int> type_names;
for (size_t i = 0; i < tokenCount; i++) {
int token = tokens[i]->type;
printf("token: %d\n", token);
if (token == CLASS) {
assert(tokens[++i]->type == IDENTIFIER);
tokens[i]->type = CLASS_NAME;
type_names[tokens[i]->value] = CLASS_NAME;
} else if (token == STRUCT || token == UNION || token == ENUM) {
assert(tokens[++i]->type == IDENTIFIER);
tokens[i]->type = TYPE_NAME;
type_names[tokens[i]->value] = TYPE_NAME;
} else if (tokens[i]->type == TYPEDEF) {
token = tokens[++i]->type;
if (token == STRUCT || token == UNION || token == ENUM) {
assert(tokens[++i]->type == '{');
int open_count = 1;
while(open_count > 0) {
if (tokens[++i]->type == '{') {
open_count++;
} else if (tokens[i]->type == '}') {
open_count--;
}
}
assert(tokens[++i]->type == IDENTIFIER);
tokens[i]->type = TYPE_NAME;
type_names[tokens[i]->value] = TYPE_NAME;
// declaration list
while(1) {
if (tokens[++i]->type == ',') {
assert(tokens[++i]->type == IDENTIFIER);
tokens[i]->type = TYPE_NAME;
type_names[tokens[i]->value] = TYPE_NAME;
} else {
break;
}
}
} else {
while (tokens[++i]->type != ';');
tokens[i - 1]->type = TYPE_NAME;
type_names[tokens[i - 1]->value] = TYPE_NAME;
printf("just inserted identifier %s\n", tokens[i]->value);
}
} else if (tokens[i]->type == IDENTIFIER && type_names[tokens[i]->value]) {
tokens[i]->type = type_names[tokens[i]->value];
} else {
printf("rejecting identifier/type: %s/%d\n", tokens[i]->value, tokens[i]->type);
}
}
for (size_t i = 0; i < tokenCount; i++) {
if (tokens[i]->type == TYPE_NAME) {
printf("typename: %s\n", tokens[i]->value);
} else if (tokens[i]->type == CLASS_NAME) {
printf("class_name: %s\n", tokens[i]->value);
}
}
}
// look for variable definitions and record type
void Preprocessor::second_pass() {
#if DEBUG
puts("second_pass called");
#endif
while(cursor < tokenCount) {
std::vector<MToken *> specifiers = declaration_specifiers();
std::string identifier;
if (accept(IDENTIFIER)) {
identifier = tokens[cursor - 1]->value;
if (accept(';') || accept('=') || accept(',') || accept(')')) {
if (specifiers.size() > 0) {
std::string type;
for (size_t i = 0; i < specifiers.size(); i++) {
if (is_type_specifier(specifiers[i]->type)) {
if (!type.empty()) type += " ";
type += specifiers[i]->value;
}
}
variable_types[identifier] = type;
if (tokens[cursor - 1]->type == ',') {
for (;cursor < tokenCount; cursor++) {
if (tokens[cursor]->type == IDENTIFIER) {
variable_types[tokens[cursor]->value] = type;
} else if (tokens[cursor]->type == ',') {
continue;
} else {
break;
}
}
}
continue;
}
}
}
cursor++;
}
printf("\n\n\tGATHERED VARIABLE TYPES\n\nvariable: type\n");
for (std::map<std::string, std::string>::iterator it = variable_types.begin(); it != variable_types.end(); it++) {
printf("%s : %s\n\n", it->first.c_str(), it->second.c_str());
}
cursor = 0;
}
static bool is_float(const char *string) {
for(size_t i = 0; i < strlen(string); i++) {
if (string[i] == '.') return true;
}
return false;
}
// only supports double and int parameter types..
void Preprocessor::collect_function_arguments(size_t &i, std::vector<MToken *> tokens, std::vector<std::string> *arguments, std::vector<std::string> *argument_types) {
for(; tokens[i]->type != ')' && i < tokens.size(); i++) {
if (tokens[i]->type == IDENTIFIER) {
arguments->push_back(tokens[i]->value);
argument_types->push_back(variable_types[tokens[i]->value]);
} else if (tokens[i]->type == STRING_LITERAL) {
arguments->push_back(tokens[i]->value);
argument_types->push_back("const_char_*");
} else if (tokens[i]->type == CONSTANT) {
arguments->push_back(tokens[i]->value);
// determine type of token
argument_types->push_back(is_float(tokens[i]->value) ? "double" : "int");
}
}
}
std::string Preprocessor::generate_function_body(Function *function) {
printf("\n\n\tCALL\n\ngenerate_function_body for %s\n", function->identifier.c_str());
std::string result;
MToken *token = function->body_tokens[0];
for (size_t i = 0; i < function->body_tokens.size(); token = function->body_tokens[++i]) {
printf("token/type: %s/%d\n", token->value, token->type);
if (token->type == IDENTIFIER) {
Function *method_ptr;
std::string scope, identifier, variable_identifier = token->value;
// 3 cases
int type = function->body_tokens[i + 1]->type;
if ((type == PTR_OP || type == '.') && function->body_tokens[i + 2]->type == IDENTIFIER) {
identifier = function->body_tokens[i + 2]->value;
// original class name
scope = variable_types[variable_identifier];
std::vector<std::string> arguments;
std::vector<std::string> argument_types;
// gather parameters
if (function->body_tokens[i + 3]->type == '(') {
collect_function_arguments(i += 3, function->body_tokens, &arguments, &argument_types);
// generate full method identifier and make sure it's valid
// original class name
identifier = unique_function_identifier(scope, identifier, argument_types);
if (function_info[identifier]) {
result += identifier + (type == PTR_OP ? "(" : "(&") + variable_identifier;
for (size_t j = 0; j < arguments.size(); j++) {
result += ", " + arguments[j];
}
} else {
error("Invalid method identifier 3");
}
} else { // variable
i += 2;
result += variable_identifier + (PTR_OP ? "->" : ".") + identifier + " ";
}
// good
} else if (variable_types[variable_identifier] == function->scope) {
/* the following clause prevents the transformation of CLASS_NAME IDENTIFIER
to CLASS_NAME self->IDENTIFIER.
i.e. variable declaration. ex: Person person_object; should not be transformed.
*/
if (i > 0 && function->body_tokens[i - 1]->type != CLASS_NAME) {
result += "self->" + variable_identifier; // this might not always work
} else {
result += variable_identifier;
}
} else if (function->body_tokens[i + 1]->type == '(') {
std::vector<std::string> arguments;
std::vector<std::string> argument_types;
// gather parameters
collect_function_arguments(i += 1, function->body_tokens, &arguments, &argument_types);
// generate full method identifier and make sure it's valid
identifier = unique_function_identifier(scope, identifier, argument_types);
if (function_info[identifier]) {
result += identifier + "(self";
for (size_t j = 0; j < arguments.size(); j++) {
result += ", " + arguments[j];
}
result += ")";
} else {
error("Invalid method identifier");
}
} else {
// spacing just to be safe...
printf("variable_identifier: %s\n", variable_identifier.c_str());
result += variable_identifier + " ";
}
// this isn't recursive so it won't allow super.super for now. Need to fix later
} else if (token->type == SUPER) {
std::string super = super_class[function->scope];
int type = function->body_tokens[i + 1]->type;
if ((type == PTR_OP || type == '.') && function->body_tokens[i + 2]->type == IDENTIFIER) {
std::string identifier = function->body_tokens[i + 2]->value;
if (function->body_tokens[i + 3]->type == '(') {
std::vector<std::string> arguments;
std::vector<std::string> argument_types;
collect_function_arguments(i += 3, function->body_tokens, &arguments, &argument_types);
identifier = unique_function_identifier(super, identifier, argument_types);
if (function_info[identifier]) {
// implicit C cast from self* to super*
result += identifier + "(self";
for (size_t j = 0; j < arguments.size(); j++) {
result += ", " + arguments[j];
}
result += ")";
} else {
error("Invalid method identifier");
}
} else { // variable
i += 2;
result += super + (PTR_OP ? "->" : ".") + identifier + " ";
}
} else {
error("Error: Invalid use of super");
}
} else if (token->type == CLASS_NAME) {
result += unique_class_identifier(token->value);
result += " ";
} else {
result += token->value;
if (token->type == ';') {
result += "\n\t";
} else if (token->type != '*') {
result.push_back(' ');
}
}
}
return result;
}