-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard_library.c
664 lines (617 loc) · 30.1 KB
/
standard_library.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
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
#include "common.h"
#include "standard_library.h"
void yoinkle_std_print(Variable *args, int num_args) {
// Printing the arguments
// printf("Arg 2: %d\n", args[2].value.int_value);
// If there are multiple arguments, then the first argument is a string
// Getting the format string and replacing all the %V with the arguments
char *format_string = args[0].value.string_value;
int format_string_len = strlen(format_string);
int format_string_index = 0;
int arg_index = 1;
if (num_args > 1) {
for (int pos = 0; pos < format_string_len; pos++) {
if (format_string[pos] == '%' && format_string[pos + 1] == 'V') {
// Getting the argument
Variable arg = args[arg_index];
switch (arg.type) {
char *dest;
char *argument;
char *rest;
int arg_length;
case VAR_INT:
;
// Copy the first part of the format string
dest = malloc(pos + 1);
strncpy(dest, format_string, pos);
dest[pos] = '\0';
// Insert the argument
arg_length = snprintf(NULL, 0, "%lld", arg.value.int_value);
argument = malloc(arg_length + 1); // Allocate space for string + null terminator
snprintf(argument, arg_length + 1, "%lld", arg.value.int_value); // Convert integer to string
// Copy the rest of the format string
rest = malloc(format_string_len - pos - 2);
strncpy(rest, format_string + pos + 2, format_string_len - pos - 2);
rest[format_string_len - pos - 2] = '\0';
// Concatenate the strings
format_string = malloc(strlen(dest) + strlen(argument) + strlen(rest) + 1);
strcpy(format_string, dest);
strcat(format_string, argument);
strcat(format_string, rest);
format_string_len = strlen(format_string);
free(dest);
free(argument);
free(rest);
arg_index++;
break;
case VAR_FLOAT:
;
// Copy the first part of the format string
dest = malloc(pos + 1);
strncpy(dest, format_string, pos);
dest[pos] = '\0';
// Insert the argument
arg_length = snprintf(NULL, 0, "%f", arg.value.float_value);
argument = malloc(arg_length + 1); // Allocate space for string + null terminator
snprintf(argument, arg_length + 1, "%f", arg.value.float_value); // Convert float to string
// Copy the rest of the format string
rest = malloc(format_string_len - pos - 2);
strncpy(rest, format_string + pos + 2, format_string_len - pos - 2);
rest[format_string_len - pos - 2] = '\0';
// Concatenate the strings
format_string = malloc(strlen(dest) + strlen(argument) + strlen(rest) + 1);
strcpy(format_string, dest);
strcat(format_string, argument);
strcat(format_string, rest);
format_string_len = strlen(format_string);
free(dest);
free(argument);
free(rest);
arg_index++;
break;
case VAR_BOOLEAN:
;
// Copy the first part of the format string
dest = malloc(pos + 1);
strncpy(dest, format_string, pos);
dest[pos] = '\0';
// Insert the argument
arg_length = snprintf(NULL, 0, "%s", arg.value.boolean_value ? "True" : "False");
argument = malloc(arg_length + 1); // Allocate space for string + null terminator
snprintf(argument, arg_length + 1, "%s", arg.value.boolean_value ? "True" : "False"); // Convert boolean to string
// Copy the rest of the format string
rest = malloc(format_string_len - pos - 2);
strncpy(rest, format_string + pos + 2, format_string_len - pos - 2);
rest[format_string_len - pos - 2] = '\0';
// Concatenate the strings
format_string = malloc(strlen(dest) + strlen(argument) + strlen(rest) + 1);
strcpy(format_string, dest);
strcat(format_string, argument);
strcat(format_string, rest);
format_string_len = strlen(format_string);
free(dest);
free(argument);
free(rest);
arg_index++;
break;
case VAR_STRING:
;
// Copy the first part of the format string
dest = malloc(pos + 1);
strncpy(dest, format_string, pos);
dest[pos] = '\0';
// Insert the argument
arg_length = strlen(arg.value.string_value);
argument = malloc(arg_length + 1); // Allocate space for string + null terminator
strcpy(argument, arg.value.string_value); // Copy the string
// Copy the rest of the format string
rest = malloc(format_string_len - pos - 2);
strncpy(rest, format_string + pos + 2, format_string_len - pos - 2);
rest[format_string_len - pos - 2] = '\0';
// Concatenate the strings
format_string = malloc(strlen(dest) + strlen(argument) + strlen(rest) + 1);
strcpy(format_string, dest);
strcat(format_string, argument);
strcat(format_string, rest);
format_string_len = strlen(format_string);
free(dest);
free(argument);
free(rest);
arg_index++;
break;
}
}
}
}
while (*format_string)
{
if (*format_string == '\\') {
format_string++; // Move to the next character after the backslash
switch (*format_string) {
case 't':
putchar('\t'); // Print a tab character
break;
case 'n':
putchar('\n'); // Print a new line character
break;
case 'r':
putchar('\r'); // Carriage return
break;
case 'b':
putchar('\b'); // Backspace (may not show effect)
break;
case '\\':
putchar('\\'); // Backslash
break;
default:
putchar('\\'); // Print the backslash if it's not followed by t or n
putchar(*format_string); // Print the next character
}
} else {
putchar(*format_string); // Print the regular character
}
format_string++; // Move to the next character in the string
}
}
void yoinkle_std_println(Variable *args, int num_args) {
// Printing the arguments
// If there are multiple arguments, then the first argument is a string
// Getting the format string and replacing all the %V with the arguments
char *format_string = args[0].value.string_value;
int format_string_len = strlen(format_string);
int format_string_index = 0;
int arg_index = 1;
if (num_args > 1) {
for (int pos = 0; pos < format_string_len; pos++) {
if (format_string[pos] == '%' && format_string[pos + 1] == 'V') {
// Getting the argument
Variable arg = args[arg_index];
switch (arg.type) {
char *dest;
char *argument;
char *rest;
int arg_length;
case VAR_INT:
;
// Copy the first part of the format string
dest = malloc(pos + 1);
strncpy(dest, format_string, pos);
dest[pos] = '\0';
// Insert the argument
arg_length = snprintf(NULL, 0, "%lld", arg.value.int_value);
argument = malloc(arg_length + 1); // Allocate space for string + null terminator
snprintf(argument, arg_length + 1, "%lld", arg.value.int_value); // Convert integer to string
// Copy the rest of the format string
rest = malloc(format_string_len - pos - 2);
strncpy(rest, format_string + pos + 2, format_string_len - pos - 2);
rest[format_string_len - pos - 2] = '\0';
// Concatenate the strings
format_string = malloc(strlen(dest) + strlen(argument) + strlen(rest) + 1);
strcpy(format_string, dest);
strcat(format_string, argument);
strcat(format_string, rest);
format_string_len = strlen(format_string);
free(dest);
free(argument);
free(rest);
arg_index++;
break;
case VAR_FLOAT:
;
// Copy the first part of the format string
dest = malloc(pos + 1);
strncpy(dest, format_string, pos);
dest[pos] = '\0';
// Insert the argument
arg_length = snprintf(NULL, 0, "%f", arg.value.float_value);
argument = malloc(arg_length + 1); // Allocate space for string + null terminator
snprintf(argument, arg_length + 1, "%f", arg.value.float_value); // Convert float to string
// Copy the rest of the format string
rest = malloc(format_string_len - pos - 2);
strncpy(rest, format_string + pos + 2, format_string_len - pos - 2);
rest[format_string_len - pos - 2] = '\0';
// Concatenate the strings
format_string = malloc(strlen(dest) + strlen(argument) + strlen(rest) + 1);
strcpy(format_string, dest);
strcat(format_string, argument);
strcat(format_string, rest);
format_string_len = strlen(format_string);
free(dest);
free(argument);
free(rest);
arg_index++;
break;
case VAR_BOOLEAN:
;
// Copy the first part of the format string
dest = malloc(pos + 1);
strncpy(dest, format_string, pos);
dest[pos] = '\0';
// Insert the argument
arg_length = snprintf(NULL, 0, "%s", arg.value.boolean_value ? "True" : "False");
argument = malloc(arg_length + 1); // Allocate space for string + null terminator
snprintf(argument, arg_length + 1, "%s", arg.value.boolean_value ? "True" : "False"); // Convert boolean to string
// Copy the rest of the format string
rest = malloc(format_string_len - pos - 2);
strncpy(rest, format_string + pos + 2, format_string_len - pos - 2);
rest[format_string_len - pos - 2] = '\0';
// Concatenate the strings
format_string = malloc(strlen(dest) + strlen(argument) + strlen(rest) + 1);
strcpy(format_string, dest);
strcat(format_string, argument);
strcat(format_string, rest);
format_string_len = strlen(format_string);
free(dest);
free(argument);
free(rest);
arg_index++;
break;
case VAR_STRING:
;
// Copy the first part of the format string
dest = malloc(pos + 1);
strncpy(dest, format_string, pos);
dest[pos] = '\0';
// Insert the argument
arg_length = strlen(arg.value.string_value);
argument = malloc(arg_length + 1); // Allocate space for string + null terminator
strcpy(argument, arg.value.string_value); // Copy the string
// Copy the rest of the format string
rest = malloc(format_string_len - pos - 2);
strncpy(rest, format_string + pos + 2, format_string_len - pos - 2);
rest[format_string_len - pos - 2] = '\0';
// Concatenate the strings
format_string = malloc(strlen(dest) + strlen(argument) + strlen(rest) + 1);
strcpy(format_string, dest);
strcat(format_string, argument);
strcat(format_string, rest);
format_string_len = strlen(format_string);
free(dest);
free(argument);
free(rest);
arg_index++;
break;
}
}
}
}
while (*format_string)
{
if (*format_string == '\\') {
format_string++; // Move to the next character after the backslash
switch (*format_string) {
case 't':
putchar('\t'); // Print a tab character
break;
case 'n':
putchar('\n'); // Print a new line character
break;
case 'r':
putchar('\r'); // Carriage return
break;
case 'b':
putchar('\b'); // Backspace (may not show effect)
break;
case '\\':
putchar('\\'); // Backslash
break;
default:
putchar('\\'); // Print the backslash if it's not followed by t or n
putchar(*format_string); // Print the next character
}
} else {
putchar(*format_string); // Print the regular character
}
format_string++; // Move to the next character in the string
}
putchar('\n');
}
void yoinkle_std_exit(Node *AST, Token *p_tokens, Variable *var_stack, int exit_code) {
free(AST);
free(p_tokens);
free(var_stack);
AST = NULL;
p_tokens = NULL;
var_stack = NULL;
printf("\nExiting with code %d\n", exit_code);
exit(exit_code);
}
char *yoinkle_std_read(char *prompt) {
if (prompt != NULL) {
char *format_string = prompt;
while (*format_string)
{
if (*format_string == '\\') {
format_string++; // Move to the next character after the backslash
switch (*format_string) {
case 't':
putchar('\t'); // Print a tab character
break;
case 'n':
putchar('\n'); // Print a new line character
break;
case 'r':
putchar('\r'); // Carriage return
break;
case 'b':
putchar('\b'); // Backspace (may not show effect)
break;
case '\\':
putchar('\\'); // Backslash
break;
default:
putchar('\\'); // Print the backslash if it's not followed by t or n
putchar(*format_string); // Print the next character
}
} else {
putchar(*format_string); // Print the regular character
}
format_string++; // Move to the next character in the string
}
}
char *input = malloc(1000);
fgets(input, 1000, stdin);
// Remove the newline character from the input
input[strcspn(input, "\n")] = 0;
return input;
}
Variable *yoinkle_std_value_to_int(Variable *var) {
Variable *new_var = malloc(sizeof(Variable));
new_var->name = NULL;
new_var->type = VAR_INT;
if (var->type == VAR_INT) {
new_var->value.int_value = var->value.int_value;
} else if (var->type == VAR_FLOAT) {
new_var->value.int_value = (long long)var->value.float_value;
} else if (var->type == VAR_STRING) {
char *end;
new_var->value.int_value = strtoll(var->value.string_value, &end, 10);
} else if (var->type == VAR_BOOLEAN) {
new_var->value.int_value = var->value.boolean_value;
}
return new_var;
}
Variable *yoinkle_std_value_to_float(Variable *var) {
Variable *new_var = malloc(sizeof(Variable));
new_var->name = NULL;
new_var->type = VAR_FLOAT;
if (var->type == VAR_INT) {
new_var->value.float_value = (float)var->value.int_value;
} else if (var->type == VAR_FLOAT) {
new_var->value.float_value = var->value.float_value;
} else if (var->type == VAR_STRING) {
char *end;
new_var->value.float_value = strtof(var->value.string_value, &end);
} else if (var->type == VAR_BOOLEAN) {
new_var->value.float_value = var->value.boolean_value;
}
return new_var;
}
Variable *yoinkle_std_value_to_string(Variable *var) {
Variable *new_var = malloc(sizeof(Variable));
new_var->name = NULL;
new_var->type = VAR_STRING;
if (var->type == VAR_INT) {
char *string_value = malloc(20);
sprintf(string_value, "%lld", var->value.int_value);
new_var->value.string_value = string_value;
} else if (var->type == VAR_FLOAT) {
char *string_value = malloc(20);
sprintf(string_value, "%f", var->value.float_value);
new_var->value.string_value = string_value;
} else if (var->type == VAR_STRING) {
char *string_value = malloc(strlen(var->value.string_value) + 1);
strcpy(string_value, var->value.string_value);
new_var->value.string_value = string_value;
} else if (var->type == VAR_BOOLEAN) {
char *string_value = malloc(6);
strcpy(string_value, var->value.boolean_value ? "True" : "False");
new_var->value.string_value = string_value;
}
return new_var;
}
Variable *yoinkle_std_value_to_bool(Variable *var) {
Variable *new_var = malloc(sizeof(Variable));
new_var->name = NULL;
new_var->type = VAR_BOOLEAN;
if (var->type == VAR_INT) {
new_var->value.boolean_value = var->value.int_value != 0 ? 1 : 0;
} else if (var->type == VAR_FLOAT) {
new_var->value.boolean_value = var->value.float_value != 0.0 ? 1 : 0;
} else if (var->type == VAR_STRING) {
new_var->value.boolean_value = strcmp(var->value.string_value, "") != 0 ? 1 : 0;
} else if (var->type == VAR_BOOLEAN) {
new_var->value.boolean_value = var->value.boolean_value;
}
return new_var;
}
Variable *yoinkle_std_get_system_time(char *format_string) {
if (format_string == NULL) {
time_t t = time(NULL);
struct tm tm = *localtime(&t);
Variable *new_var = malloc(sizeof(Variable));
new_var->name = NULL;
new_var->type = VAR_STRING;
char *string_value = malloc(20);
sprintf(string_value, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
new_var->value.string_value = string_value;
return new_var;
} else {
// Getting the format string and replacing all %d, %m, %Y, %H, %M, %S with the system time
char *new_format_string = format_string;
int format_string_len = strlen(format_string);
for (int pos = 0; pos < format_string_len; pos++) {
if (format_string[pos] == '%' && pos + 1 < format_string_len) {
switch (format_string[pos + 1]) {
// %d - day of the month as a decimal number (01-31)
case 'd':
;
time_t t = time(NULL);
struct tm tm = *localtime(&t);
char *dest = malloc(pos + 1);
strncpy(dest, new_format_string, pos);
dest[pos] = '\0';
char *day = malloc(3);
sprintf(day, "%02d", tm.tm_mday);
char *rest = malloc(format_string_len - pos - 2);
strncpy(rest, new_format_string + pos + 2, format_string_len - pos - 2);
rest[format_string_len - pos - 2] = '\0';
new_format_string = malloc(strlen(dest) + 3 + strlen(rest) + 1);
strcpy(new_format_string, dest);
strcat(new_format_string, day);
strcat(new_format_string, rest);
format_string_len = strlen(new_format_string);
free(dest);
free(day);
free(rest);
break;
// %m - month as a decimal number (01-12)
case 'm':
;
time_t t2 = time(NULL);
struct tm tm2 = *localtime(&t2);
char *dest2 = malloc(pos + 1);
strncpy(dest2, new_format_string, pos);
dest2[pos] = '\0';
char *month = malloc(3);
sprintf(month, "%02d", tm2.tm_mon + 1);
char *rest2 = malloc(format_string_len - pos - 2);
strncpy(rest2, new_format_string + pos + 2, format_string_len - pos - 2);
rest2[format_string_len - pos - 2] = '\0';
new_format_string = malloc(strlen(dest2) + 3 + strlen(rest2) + 1);
strcpy(new_format_string, dest2);
strcat(new_format_string, month);
strcat(new_format_string, rest2);
format_string_len = strlen(new_format_string);
free(dest2);
free(month);
free(rest2);
break;
// %Y - year as a decimal number
case 'Y':
;
time_t t3 = time(NULL);
struct tm tm3 = *localtime(&t3);
char *dest3 = malloc(pos + 1);
strncpy(dest3, new_format_string, pos);
dest3[pos] = '\0';
char *year = malloc(5);
sprintf(year, "%d", tm3.tm_year + 1900);
char *rest3 = malloc(format_string_len - pos - 2);
strncpy(rest3, new_format_string + pos + 2, format_string_len - pos - 2);
rest3[format_string_len - pos - 2] = '\0';
new_format_string = malloc(strlen(dest3) + 5 + strlen(rest3) + 1);
strcpy(new_format_string, dest3);
strcat(new_format_string, year);
strcat(new_format_string, rest3);
format_string_len = strlen(new_format_string);
free(dest3);
free(year);
free(rest3);
break;
// %H - hour as a decimal number (00-23)
case 'H':
;
time_t t4 = time(NULL);
struct tm tm4 = *localtime(&t4);
char *dest4 = malloc(pos + 1);
strncpy(dest4, new_format_string, pos);
dest4[pos] = '\0';
char *hour = malloc(3);
sprintf(hour, "%02d", tm4.tm_hour);
char *rest4 = malloc(format_string_len - pos - 2);
strncpy(rest4, new_format_string + pos + 2, format_string_len - pos - 2);
rest4[format_string_len - pos - 2] = '\0';
new_format_string = malloc(strlen(dest4) + 3 + strlen(rest4) + 1);
strcpy(new_format_string, dest4);
strcat(new_format_string, hour);
strcat(new_format_string, rest4);
format_string_len = strlen(new_format_string);
free(dest4);
free(hour);
free(rest4);
break;
// %M - minute as a decimal number (00-59)
case 'M':
;
time_t t5 = time(NULL);
struct tm tm5 = *localtime(&t5);
char *dest5 = malloc(pos + 1);
strncpy(dest5, new_format_string, pos);
dest5[pos] = '\0';
char *minute = malloc(3);
sprintf(minute, "%02d", tm5.tm_min);
char *rest5 = malloc(format_string_len - pos - 2);
strncpy(rest5, new_format_string + pos + 2, format_string_len - pos - 2);
rest5[format_string_len - pos - 2] = '\0';
new_format_string = malloc(strlen(dest5) + 3 + strlen(rest5) + 1);
strcpy(new_format_string, dest5);
strcat(new_format_string, minute);
strcat(new_format_string, rest5);
format_string_len = strlen(new_format_string);
free(dest5);
free(minute);
free(rest5);
break;
// %S - second as a decimal number (00-59)
case 'S':
;
time_t t6 = time(NULL);
struct tm tm6 = *localtime(&t6);
char *dest6 = malloc(pos + 1);
strncpy(dest6, new_format_string, pos);
dest6[pos] = '\0';
char *second = malloc(3);
sprintf(second, "%02d", tm6.tm_sec);
char *rest6 = malloc(format_string_len - pos - 2);
strncpy(rest6, new_format_string + pos + 2, format_string_len - pos - 2);
rest6[format_string_len - pos - 2] = '\0';
new_format_string = malloc(strlen(dest6) + 3 + strlen(rest6) + 1);
strcpy(new_format_string, dest6);
strcat(new_format_string, second);
strcat(new_format_string, rest6);
format_string_len = strlen(new_format_string);
free(dest6);
free(second);
free(rest6);
break;
// Millisecond as a decimal number (000-999)
case 's':
;
time_t t7 = time(NULL);
struct tm tm7 = *localtime(&t7);
struct timeval tv;
mingw_gettimeofday(&tv, NULL);
char *dest7 = malloc(pos + 1);
strncpy(dest7, new_format_string, pos);
dest7[pos] = '\0';
char *millisecond = malloc(4);
sprintf(millisecond, "%03d", (int)tv.tv_usec / 1000);
char *rest7 = malloc(format_string_len - pos - 2);
strncpy(rest7, new_format_string + pos + 2, format_string_len - pos - 2);
rest7[format_string_len - pos - 2] = '\0';
new_format_string = malloc(strlen(dest7) + 4 + strlen(rest7) + 1);
strcpy(new_format_string, dest7);
strcat(new_format_string, millisecond);
strcat(new_format_string, rest7);
format_string_len = strlen(new_format_string);
free(dest7);
free(millisecond);
free(rest7);
break;
}
}
}
Variable *new_var = malloc(sizeof(Variable));
new_var->name = NULL;
new_var->type = VAR_STRING;
new_var->value.string_value = new_format_string;
return new_var;
}
}
void *yoinkle_std_delay(Variable *time_in_milliseconds) {
// Storing start time
clock_t start_time = clock();
// looping till required time is not achieved
while (clock() < start_time + time_in_milliseconds->value.int_value);
}