-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcnc.c
446 lines (393 loc) · 12.7 KB
/
cnc.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
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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// Token types
typedef enum {
TOKEN_IDENTIFIER,
TOKEN_NUMBER,
TOKEN_OPERATOR,
TOKEN_COMPARISON,
TOKEN_LOGICAL,
TOKEN_BOOLEAN,
TOKEN_STRING,
TOKEN_UNKNOWN,
TOKEN_LPAREN,
TOKEN_RPAREN,
TOKEN_LBRACE,
TOKEN_RBRACE,
TOKEN_EOF
} TokenType;
// Token structure
typedef struct {
TokenType type;
char value[256];
} Token;
// Lexer function that takes input string and returns an array of tokens
Token* lexer(const char *input, int *tokenCount) {
const char *cursor = input;
int capacity = 10;
int count = 0;
Token *tokens = malloc(capacity * sizeof(Token));
while (1) {
// Skip whitespace
while (isspace(*cursor)) {
cursor++;
}
// End of input
if (*cursor == '\0') {
if (count >= capacity) {
capacity *= 2;
tokens = realloc(tokens, capacity * sizeof(Token));
}
tokens[count].type = TOKEN_EOF;
tokens[count].value[0] = '\0';
count++;
break;
}
// Allocate more space if necessary
if (count >= capacity) {
capacity *= 2;
tokens = realloc(tokens, capacity * sizeof(Token));
}
Token token;
token.value[0] = '\0'; // Initialize token value to an empty string
// Check for parentheses and braces
if (*cursor == '(') {
token.type = TOKEN_LPAREN;
token.value[0] = '(';
token.value[1] = '\0';
cursor++;
} else if (*cursor == ')') {
token.type = TOKEN_RPAREN;
token.value[0] = ')';
token.value[1] = '\0';
cursor++;
} else if (*cursor == '{') {
token.type = TOKEN_LBRACE;
token.value[0] = '{';
token.value[1] = '\0';
cursor++;
} else if (*cursor == '}') {
token.type = TOKEN_RBRACE;
token.value[0] = '}';
token.value[1] = '\0';
cursor++;
}
// Check for comparison operators
else if (strncmp(cursor, "==", 2) == 0 || strncmp(cursor, "!=", 2) == 0 ||
strncmp(cursor, ">=", 2) == 0 || strncmp(cursor, "<=", 2) == 0) {
token.type = TOKEN_COMPARISON;
strncpy(token.value, cursor, 2);
token.value[2] = '\0';
cursor += 2;
} else if (*cursor == '>' || *cursor == '<') {
token.type = TOKEN_COMPARISON;
token.value[0] = *cursor;
token.value[1] = '\0';
cursor++;
}
// Check for logical operators
else if (strncmp(cursor, "&&", 2) == 0 || strncmp(cursor, "||", 2) == 0) {
token.type = TOKEN_LOGICAL;
strncpy(token.value, cursor, 2);
token.value[2] = '\0';
cursor += 2;
}
// Check for boolean values
else if (strncmp(cursor, "true", 4) == 0 && !isalnum(cursor[4])) {
token.type = TOKEN_BOOLEAN;
strncpy(token.value, "true", 4);
token.value[4] = '\0';
cursor += 4;
} else if (strncmp(cursor, "false", 5) == 0 && !isalnum(cursor[5])) {
token.type = TOKEN_BOOLEAN;
strncpy(token.value, "false", 5);
token.value[5] = '\0';
cursor += 5;
}
// Check for strings
else if (*cursor == '"') {
token.type = TOKEN_STRING;
int i = 0;
cursor++; // Skip opening quote
while (*cursor != '"' && *cursor != '\0') {
token.value[i++] = *cursor;
cursor++;
}
token.value[i] = '\0';
if (*cursor == '"') {
cursor++; // Skip closing quote
}
}
// Check for operators
else if (strchr("+-*/", *cursor)) {
token.type = TOKEN_OPERATOR;
token.value[0] = *cursor;
token.value[1] = '\0';
cursor++;
}
// Check for numbers
else if (isdigit(*cursor)) {
token.type = TOKEN_NUMBER;
int i = 0;
while (isdigit(*cursor)) {
token.value[i++] = *cursor;
cursor++;
}
token.value[i] = '\0';
}
// Check for identifiers
else if (isalpha(*cursor)) {
token.type = TOKEN_IDENTIFIER;
int i = 0;
while (isalnum(*cursor)) {
token.value[i++] = *cursor;
cursor++;
}
token.value[i] = '\0';
}
// Unknown token
else {
token.type = TOKEN_UNKNOWN;
token.value[0] = *cursor;
token.value[1] = '\0';
cursor++;
}
tokens[count++] = token;
}
*tokenCount = count;
return tokens;
}
// Define the existing string as a dynamically allocated pointer
char *Main = NULL;
char *includesList = NULL;
// Function to append a string to the existing string
void appendMain(const char *stringToAppend) {
// Calculate the new required length
size_t newLength = (Main ? strlen(Main) : 0) + strlen(stringToAppend) + 1;
// Reallocate memory for the new length
char *temp = realloc(Main, newLength);
if (temp == NULL) {
printf("Error: Memory allocation failed.\n");
free(Main); // Free existing memory on failure
Main = NULL;
return;
}
Main = temp;
// Append the new string
if (Main[0] == '\0') {
strcpy(Main, stringToAppend); // Initialize if it's empty
} else {
strcat(Main, stringToAppend); // Append otherwise
}
}
// Function to append a string to the existing string
void appendIncludes(const char *stringToAppend) {
// Calculate the new required length
size_t newLength = (includesList ? strlen(includesList) : 0) + strlen(stringToAppend) + 1;
// Reallocate memory for the new length
char *temp = realloc(includesList, newLength);
if (temp == NULL) {
printf("Error: Memory allocation failed.\n");
free(includesList); // Free existing memory on failure
includesList = NULL;
return;
}
includesList = temp;
// Append the new string
if (includesList[0] == '\0') {
strcpy(includesList, stringToAppend); // Initialize if it's empty
} else {
strcat(includesList, stringToAppend); // Append otherwise
}
}
typedef enum {
DEFAULT,
VOID,
IN_MAIN
} Status;
Status currentStatus;
int braceCnt = 0;
void tok_id(char* value, Token* tokens, int current_idx) {
if (strcmp(value, "void") == 0) {
currentStatus = VOID;
return;
} else if (strcmp(value, "main") == 0 && currentStatus == VOID) {
appendMain("int main ");
currentStatus = IN_MAIN;
return;
} else {
appendMain(value);
}
if (currentStatus != IN_MAIN) {
currentStatus = DEFAULT;
}
appendMain(" ");
}
void tok_num(char* value, Token* tokens, int current_idx) {
appendMain(value);
}
void tok_op(char* value, Token* tokens, int current_idx) {
appendMain(value);
}
void tok_comp(char* value, Token* tokens, int current_idx) {
appendMain(value);
}
void tok_logi(char* value, Token* tokens, int current_idx) {
appendMain(value);
}
void tok_bool(char* value, Token* tokens, int current_idx) {
appendMain(value);
}
void tok_str(char* value, Token* tokens, int current_idx) {
appendMain("\"");
appendMain(value);
appendMain("\"");
}
void tok_unknown(char* value, Token* tokens, int current_idx) {
// only accept certain chars?
// if (
// strcmp((const char*)value , ";") == 0 ||
// strcmp((const char*)value , "\n") == 0
// strcmp((const char*)value , "*") == 0 ||
// strcmp((const char*)value , "*") == 0 ||
// strcmp((const char*)value , "*") == 0 ||
// strcmp((const char*)value , "*") == 0 ||
// strcmp((const char*)value , "*") == 0 ||
// strcmp((const char*)value , "*") == 0
// ){
// }
appendMain(value);
}
void tok_lparen(char* value, Token* tokens, int current_idx) {
if (currentStatus == IN_MAIN && braceCnt == 0) {
appendMain(value);
appendMain("int argc, char const *argv[] ");
return;
}
appendMain(value);
}
void tok_rparen(char* value, Token* tokens, int current_idx) {
appendMain(value);
}
void tok_lbrace(char* value, Token* tokens, int current_idx) {
braceCnt ++;
appendMain(value);
}
void tok_rbrace(char* value, Token* tokens, int current_idx) {
braceCnt --;
if (currentStatus == IN_MAIN && braceCnt == 0) {
appendMain("return 0;");
currentStatus = DEFAULT;
}
appendMain(value);
}
void tok_eof(char* value, Token* tokens, int current_idx) {
}
void convertC(Token* tokens, int tokenCount) {
currentStatus = DEFAULT;
for (int i = 0; i < tokenCount; i++) {
switch (tokens[i].type) {
case TOKEN_IDENTIFIER:
tok_id(tokens[i].value, tokens, i);
break;
case TOKEN_NUMBER:
tok_num(tokens[i].value, tokens, i);
break;
case TOKEN_OPERATOR:
tok_op(tokens[i].value, tokens, i);
break;
case TOKEN_COMPARISON:
tok_comp(tokens[i].value, tokens, i);
break;
case TOKEN_LOGICAL:
tok_logi(tokens[i].value, tokens, i);
break;
case TOKEN_BOOLEAN:
tok_bool(tokens[i].value, tokens, i);
break;
case TOKEN_STRING:
tok_str(tokens[i].value, tokens, i);
break;
case TOKEN_UNKNOWN:
tok_unknown(tokens[i].value, tokens, i);
break;
case TOKEN_LPAREN:
tok_lparen(tokens[i].value, tokens, i);
break;
case TOKEN_RPAREN:
tok_rparen(tokens[i].value, tokens, i);
break;
case TOKEN_LBRACE:
tok_lbrace(tokens[i].value, tokens, i);
break;
case TOKEN_RBRACE:
tok_rbrace(tokens[i].value, tokens, i);
break;
case TOKEN_EOF:
tok_eof(tokens[i].value, tokens, i);
break;
}
}
}
// Function to read the contents of a file
char* readFile(const char* path) {
FILE* file = fopen(path, "rb");
fseek(file, 0, SEEK_END);
size_t fileSize = ftell(file);
rewind(file);
char* buffer = (char*)malloc(fileSize + 1);
size_t bytesRead = fread(buffer, sizeof(char), fileSize, file);
buffer[bytesRead] = '\0';
fclose(file);
return buffer;
}
// Function to write a string to a file
void writeFile(const char *fileName, const char *content) {
FILE *file = fopen(fileName, "w");
if (file == NULL) {
printf("Error: Could not open file %s for writing.\n", fileName);
return;
}
// Write the content to the file
fprintf(file, "%s", content);
fclose(file);
}
int main(int argc, char const *argv[]) {
if (!system(NULL)) {
printf("Error: Command processor is not available.\n");
return 1;
}
if (argv[1] == NULL || argv[2] == NULL) {
printf("Usage: %s [c-compiler] [file-name]\n", argv[0]);
return 1;
}
const char *input = readFile(argv[2]);
// Initialize the existing string with a starting value
Main = malloc(1); // Start with an empty string
includesList = malloc(1); // Start with an empty string
if (Main == NULL || includesList == NULL) {
printf("Error: Initial memory allocation failed.\n");
return 1;
}
Main[0] = '\0'; // Null-terminate the empty string
includesList[0] = '\0'; // Null-terminate the empty string
int tokenCount;
Token *tokens = lexer(input, &tokenCount);
convertC(tokens, tokenCount);
// appendMain(" return 0;\n}");
appendIncludes("#include <stdio.h>\n#include <string.h>\n#include <ctype.h>\n#include <stdlib.h>\n#include <stddef.h>\n#include <math.h>\n#include <time.h>\n#include <signal.h>\n#include <setjmp.h>\n#include <locale.h>\n#include <stdint.h>\n#include <stdbool.h>\n#include <limits.h>\n#include <float.h>\n#include <inttypes.h>\n#include <errno.h>\n#include <wchar.h>\n#include <wctype.h>\n#include <stdarg.h>\n");
appendIncludes(Main);
const char* out = "out.c";
writeFile(out, includesList);
// printf(strcat(strcat((char*)argv[1] ," "), out));
int result = system(strcat(strcat((char*)argv[1] ," "), out));
if (result == 0){
remove(out);
}
free(Main);
free(includesList);
free(tokens);
return 0;
}