lac : f360fd5fc15c67297f0615d5f5ea023c299026b2
1: #include <stdio.h>
2: #include <sys/time.h>
3: #include <stdlib.h>
4: #include <setjmp.h>
5: #include <unistd.h>
6: #include <signal.h>
7: #include "lac.h"
8:
9: lenv_t *null_env;
10:
11: void lac_error_print(FILE *f)
12: {
13: fprintf(f, "(*LAC-ERROR* \"%s", lac_errmsg());
14: if (lac_errlreg() != NIL)
15: {
16: fprintf(f, ": ");
17: sexpr_print(f, lac_errlreg());
18: }
19: fprintf(f, "\")\n");
20: }
21:
22: void
23: sigint(int sig)
24: {
25: raise_exception("Interrupted", NIL);
26: }
27:
28: #define TDIFF_SEC(_a, _b) \
29: ((_b)->tv_usec >= (_a)->tv_usec \
30: ? (_b)->tv_sec - (_a)->tv_sec \
31: : (_b)->tv_sec - (_a)->tv_sec - 1)
32:
33: #define TDIFF_USEC(_a, _b) \
34: ((_b)->tv_usec >= t1.tv_usec \
35: ? (_b)->tv_usec - (_a)->tv_usec \
36: : (_b)->tv_usec + 1000000L - (_a)->tv_usec)
37:
38:
39: int repl(FILE *infd, FILE *outfd, FILE *errfd)
40: {
41: int r;
42: void *scan;
43: lreg_t res = NIL;
44: struct timeval t1, t2;
45:
46: restart:
47: lac_on_error({
48: lac_error_print(errfd);
49: sexpr_read_stop(scan);
50: res = NIL;
51: goto restart;
52: });
53:
54: sexpr_read_start(infd, &scan);
55: do {
56: r = sexpr_read(&res, scan);
57:
58: gettimeofday(&t1, NULL);
59: res = eval(res, null_env);
60: gettimeofday(&t2, NULL);
61:
62: if ( isatty(fileno(outfd)) ) {
63: fprintf(outfd, "=> ");
64: sexpr_print(outfd, res);
65: fprintf(outfd, "\n");
66: fprintf(outfd,
67: "Evaluation took %ld seconds and %ld microseconds.\n",
68: TDIFF_SEC(&t1, &t2), TDIFF_USEC(&t1, &t2));
69: }
70: } while(r);
71: sexpr_read_stop(scan);
72:
73: lac_off_error();
74: return r;
75: }
76:
77: int main()
78: {
79:
80: signal(SIGINT, sigint);
81:
82: lac_on_error({
83: lac_error_print(stderr);
84: exit(-1);
85: });
86:
87: null_env = lac_init();
88: repl(stdin, stdout, stderr);
89: fprintf(stdout, "\ngoodbye!\n");
90: return 0;
91: }
Generated by git2html.