-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
78 lines (59 loc) · 1.43 KB
/
main.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
/*
* main.c
*
* Copyright (c) 2021-2024 Xiongfei Shi
*
* Author: Xiongfei Shi <xiongfei.shi(a)icloud.com>
* License: Apache-2.0
*
* https://github.com/shixiongfei/ape
*/
#include <setjmp.h>
#include <stdio.h>
#include "ape.h"
static long prompt = 0;
static jmp_buf toplevel;
static void on_error(ape_State *A, const char *errmsg, ape_Object calllist) {
ape_Object cl = calllist;
fprintf(stderr, "error: %s\n", errmsg);
for (; !ape_isnil(A, cl); cl = ape_cdr(A, cl)) {
char buf[128];
ape_tostring(A, ape_car(A, cl), buf, sizeof(buf));
fprintf(stderr, "=> %s\n", buf);
}
longjmp(toplevel, -1);
}
static int do_file(const char *filename) {
ape_State *A = ape_newstate(NULL, NULL);
ape_load(A, filename, NULL);
ape_close(A);
return 0;
}
static int do_repl(void) {
ape_State *A = ape_newstate(NULL, NULL);
ape_Object expr;
ape_handlers(A)->error = on_error;
printf("Welcome to Ape v%s\n", APE_RELEASE);
setjmp(toplevel);
/* repl */
for (;;) {
printf("%ld| in> ", ++prompt);
expr = ape_readfp(A, stdin);
if (!expr)
break;
printf("%ld|out> ", prompt);
/* Maybe eval will take a long time.
Let's display the prompt first. */
fflush(stdout);
expr = ape_eval(A, expr, NULL);
ape_writefp(A, expr, stdout);
printf("\n");
}
ape_close(A);
return 0;
}
int main(int argc, char *argv[]) {
if (argc > 1)
return do_file(argv[1]);
return do_repl();
}