lac : 8b2fb62d1ca3b495e951cffed3ec2d150a6d2580
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 <readline/readline.h>
8: #include <readline/history.h>
9: #include "lac.h"
10:
11: lenv_t *null_env;
12:
13: void
14: lac_error_print (FILE * f)
15: {
16: fprintf (f, "(*LAC-ERROR* \"%s", lac_errmsg ());
17: if (lac_errlreg () != NIL)
18: {
19: fprintf (f, ": ");
20: sexpr_fprint (f, lac_errlreg ());
21: }
22: fprintf (f, "\")\n");
23: }
24:
25: void
26: sigint (int sig)
27: {
28: raise_exception ("Interrupted", NIL);
29: }
30:
31: #define TDIFF_SEC(_a, _b) \
32: ((_b)->tv_usec >= (_a)->tv_usec \
33: ? (_b)->tv_sec - (_a)->tv_sec \
34: : (_b)->tv_sec - (_a)->tv_sec - 1)
35:
36: #define TDIFF_USEC(_a, _b) \
37: ((_b)->tv_usec >= t1.tv_usec \
38: ? (_b)->tv_usec - (_a)->tv_usec \
39: : (_b)->tv_usec + 1000000L - (_a)->tv_usec)
40:
41: lreg_t timed_eval(lreg_t lr, void *opq)
42: {
43: lreg_t ret;
44: struct timeval t1, t2;
45: lenv_t *env = (lenv_t *)opq;
46:
47: gettimeofday(&t1, NULL);
48: ret = eval(lr, env);
49: gettimeofday(&t2, NULL);
50:
51: fprintf(stderr, "Evaluation took %ld seconds and %ld microseconds.\n",
52: TDIFF_SEC(&t1, &t2), TDIFF_USEC(&t1, &t2));
53:
54: return ret;
55: }
56:
57: int
58: repl (FILE * infd, FILE * outfd, FILE * errfd)
59: {
60: int r;
61: char *buf = NULL;
62: lreg_t res = NIL;
63: struct timeval t1, t2;
64:
65: restart:
66: lac_on_error ({
67: lac_error_print (errfd); res = NIL; goto restart;
68: });
69:
70:
71: do
72: {
73: buf = readline ("LAC>");
74: if (buf == NULL)
75: return -1;
76: if (*buf != '\0')
77: add_history (buf);
78:
79: res = sexpr_parse_string (buf,
80: timed_eval,
81: (void *)null_env);
82: if (isatty (fileno (outfd)))
83: {
84: fprintf (outfd, "=> ");
85: sexpr_fprint (outfd, res);
86: fprintf (outfd, "\n");
87: }
88: }
89: while (1);
90:
91: lac_off_error ();
92: return r;
93: }
94:
95: int
96: main ()
97: {
98:
99: signal (SIGINT, sigint);
100:
101: lac_on_error ({
102: lac_error_print (stderr); exit (-1);
103: });
104:
105: null_env = lac_init ();
106: repl (stdin, stdout, stderr);
107: fprintf (stdout, "\ngoodbye!\n");
108: return 0;
109: }
Generated by git2html.