This repository was archived by the owner on Jan 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.c
More file actions
110 lines (95 loc) · 2.5 KB
/
main.c
File metadata and controls
110 lines (95 loc) · 2.5 KB
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include "src/sc.h"
static void usage(void);
int main(int argc, char **argv)
{
bool stats = false;
char *eval, *path;
eval = path = NULL;
int c;
while ((c = getopt(argc, argv, "hse:f:")) != -1) {
switch (c) {
case 'f':
path = optarg;
break;
case 'e':
eval = optarg;
break;
case 's':
stats = true;
break;
case 'h':
default:
usage();
}
}
argc -= optind;
argv += optind;
srand(time(NULL));
struct sc_ctx ctx = { 0 };
sc_value res = sc_nil;
if (eval != NULL && path != NULL) {
fprintf(stderr, "sc: specify either -f or -e!");
return 1;
}
if (eval != NULL) res = sc_eval(&ctx, eval, strlen(eval));
else if (path != NULL) {
char *buf = NULL;
int written = 0;
FILE *f = fopen(path, "r");
for (int i = 1; !feof(f); i++) {
buf = realloc(buf, BUFSIZ * i);
written += fread(buf + written, 1, BUFSIZ, f);
}
*strrchr(buf, '\n') = 0;
fclose(f);
res = sc_eval(&ctx, buf, strlen(buf));
} else {
for (;;) {
char *in = NULL;
size_t size = 0;
printf(">> ");
fflush(stdout);
getline(&in, &size, stdin);
*strrchr(in, '\n') = 0;
if (strcmp(".q", in) == 0 || strcmp(".exit", in) == 0) {
free(in);
exit(0);
} else if (strcmp(".stats", in) == 0) {
printf("Peak memory usage: %dB\n", sc_heap_usage(&ctx));
free(in);
continue;
}
res = sc_eval(&ctx, in, size);
if (res.type == SC_ERROR_VAL) fprintf(stderr, "sc error: %s\n", res.err);
else if (res.type != SC_NOTHING_VAL) {
sc_display(&ctx, &res, 1);
putchar('\n');
}
free(in);
}
return 0;
}
if (res.type == SC_ERROR_VAL) {
fprintf(stderr, "sc error: %s\n", res.err);
abort();
}
if (eval != NULL) {
sc_display(&ctx, &res, 1);
putchar('\n');
}
if (stats) {
printf("Peak memory usage: %dB\n", sc_heap_usage(&ctx));
}
return 0;
}
static void usage(void)
{
fprintf(stderr, "usage: sc [-hs] [-e str|-f file]\n");
exit(1);
}