-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtin_functions.c
52 lines (51 loc) · 1.32 KB
/
builtin_functions.c
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
#include "builtin_functions.h"
#include <stdio.h>
#include <string.h>
#include "helpers.h"
int handle_print(char **code,int *sz,struct token **cToken,struct symbol *root_symbol){
char repr[] = "printf(\"";
strcpy(*code+*sz,repr);
*sz += count(repr);
*cToken = (*cToken)->next->next;
int type = (*cToken)->type;
if (type == INTEGER){
strcpy(*code+*sz,"%d\\n\"");
*sz += count("%d\\n\"");
} else if (type == FLOAT_){
strcpy(*code+*sz,"%f\\n\"");
*sz += count("%f\\n\"");
} else if (type == STRING){
strcpy(*code+*sz,"%s\\n\"");
*sz += count("%s\\n\"");
} else if (type == SYMBOL){
struct symbol *tmpSymbol = malloc(sizeof(struct symbol));
int found = findSymbol(root_symbol,(*cToken)->value,&tmpSymbol);
if (found){
type = tmpSymbol->dataType;
if (type == INTEGER){
strcpy(*code+*sz,"%d\\n\"");
*sz += count("%d\\n'");
} else if (type == FLOAT_){
strcpy(*code+*sz,"%f\\n\"");
*sz += count("%d\\n'");
} else if (type == STRING){
strcpy(*code+*sz,"%s\\n\"");
*sz += count("%d\\n'");
} else {
printf("Unknown type\n");
return 1;
}
} else {
printf("NOT FOUND!\n");
return 1;
}
}
strcpy(*code+*sz,",");
*sz += count(",");
strcpy(*code+*sz,(*cToken)->value);
*sz += count((*cToken)->value);
if ((*cToken)->type == SYMBOL){
writeType(code,sz,type);
}
return 0;
}