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
|
#include "code_generator.h"
#include <compiler/parser/parser.h>
#include <stdio.h>
#include <stdlib.h>
#include "utils/memory/memory.h"
#include "utils/types.h"
const char *COMMAND_STRINGS[] = {
"COMMAND_NONE",
"COMMAND_PRINT",
"COMMAND_PUSH_STRING",
};
void printInstruction(Instruction instruction) {
printf("%s", COMMAND_STRINGS[instruction.command]);
switch (instruction.command) {
case COMMAND_NONE:
case COMMAND_PRINT:
break;
case COMMAND_PUSH_STRING:
SizedString *sizedString = instruction.operand;
printf(" '%.*s'", (int)sizedString->size, sizedString->str);
break;
default:
fprintf(stderr, "bad instruction %d\n", instruction.command);
}
printf("\n");
}
void printInstructions(Instructions instructions) {
for (size_t i = 0; i < instructions.size; ++i) {
printInstruction(instructions.instructions[i]);
}
}
void deleteInstruction(Instruction instruction) {
switch (instruction.command) {
case COMMAND_NONE:
case COMMAND_PRINT:
break;
case COMMAND_PUSH_STRING:
SizedString *sizedString = instruction.operand;
free(sizedString->str);
free(sizedString);
break;
default:
fprintf(stderr, "bad instruction %d\n", instruction.command);
}
}
void deleteInstructions(Instructions instructions) {
for (size_t i = 0; i < instructions.size; ++i) {
deleteInstruction(instructions.instructions[i]);
}
free(instructions.instructions);
}
Instructions codeGenerator(ParsedNode *root) {
const ScopeMetadata *metadata = root->metadata;
size_t instructions_size = 10;
Instruction *instructions =
a404m_malloc(instructions_size * sizeof(Instruction));
size_t instructions_inserted = 0;
for (size_t i = 0; i < metadata->operands_size; ++i) {
ParsedNode *node = metadata->operands[i];
if (!nodeToInstruction(node, &instructions, &instructions_size,
&instructions_inserted)) {
goto RETURN_ERROR;
}
}
Instructions result = {
.instructions = a404m_realloc(
instructions, instructions_inserted * sizeof(Instruction)),
.size = instructions_inserted,
};
return result;
RETURN_ERROR:
const Instructions error = {
.instructions = NULL,
.size = ERROR_SIZE,
};
return error;
}
bool nodeToInstruction(ParsedNode *node, Instruction **instructions,
size_t *instructions_size,
size_t *instructions_inserted) {
switch (node->token) {
// TODO: this is wrong when you want functions
case PARSED_TOKEN_PARENTHESIS: {
const ScopeMetadata *metadata = node->metadata;
for (size_t i = 0; i < metadata->operands_size; ++i) {
if (!nodeToInstruction(metadata->operands[i], instructions,
instructions_size, instructions_inserted)) {
return false;
}
}
return true;
}
case PARSED_TOKEN_EOL:
return nodeToInstruction(node->metadata, instructions, instructions_size,
instructions_inserted);
case PARSED_TOKEN_PRINT:
if (nodeToInstruction(node->metadata, instructions, instructions_size,
instructions_inserted)) {
const Instruction instruction = {
.command = COMMAND_PRINT,
.operand = NULL,
};
insertInstruction(instruction, instructions, instructions_size,
instructions_inserted);
return true;
} else {
return false;
}
case PARSED_TOKEN_VALUE_STRING: {
SizedString *string = nodeToString(node);
if (string == NULL) {
return false;
}
const Instruction instruction = {
.command = COMMAND_PUSH_STRING,
.operand = string,
};
insertInstruction(instruction, instructions, instructions_size,
instructions_inserted);
return true;
}
case PARSED_TOKEN_NONE:
case PARSED_TOKEN_ROOT:
}
fprintf(stderr, "unexpected token %s\n", PARSED_TOKEN_STRINGS[node->token]);
return false;
}
void insertInstruction(const Instruction instruction,
Instruction **restrict instructions,
size_t *restrict instructions_size,
size_t *restrict instructions_inserted) {
if (*instructions_inserted == *instructions_size) {
*instructions_size += *instructions_size / 2 + 1;
*instructions =
a404m_realloc(*instructions, *instructions_size * sizeof(Instruction));
}
(*instructions)[*instructions_inserted] = instruction;
++*instructions_inserted;
}
SizedString *nodeToString(ParsedNode const *node) {
const char *strBegin = node->strBegin + 1;
const char *strEnd = node->strEnd - 1;
char *str = a404m_malloc((strEnd - strBegin + 1) * sizeof(char));
size_t inserted = 0;
for (char const *iter = strBegin; iter < strEnd; ++iter) {
char c = *iter;
if (c == '\\') {
if (++iter < strEnd) {
switch (*iter) {
case '\'':
c = '\'';
break;
case '\"':
c = '\"';
break;
case '\\':
c = '\\';
break;
case 'a':
c = '\a';
break;
case 'b':
c = '\b';
break;
case 'f':
c = '\f';
break;
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case 'v':
c = '\v';
break;
/*case 'u':*/ // TODO: do it
/* c = '';*/
/* break;*/
default:
fprintf(stderr, "bad string, bad '\\'\n");
goto RETURN_ERROR;
}
} else {
fprintf(stderr, "bad string, bad '\\'\n");
goto RETURN_ERROR;
}
}
str[inserted] = c;
++inserted;
}
str[inserted] = '\0';
SizedString *string = a404m_malloc(sizeof(SizedString));
string->str = a404m_realloc(str, (inserted + 1) * sizeof(char));
string->size = inserted;
return string;
RETURN_ERROR:
free(str);
return NULL;
}
|