lac : bd5d10a43d4e891cb82ffeb765e1e14a65c95aa4

     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: 
    42: int
    43: repl (FILE * infd, FILE * outfd, FILE * errfd)
    44: {
    45:   int r;
    46:   char *buf = NULL;
    47:   lreg_t res = NIL;
    48:   struct timeval t1, t2;
    49: 
    50: restart:
    51:   lac_on_error ({
    52:       lac_error_print (errfd); res = NIL; goto restart;
    53:     });
    54: 
    55: 
    56:   do
    57:     {
    58:       buf = readline ("LAC>");
    59:       if (buf == NULL)
    60: 	return -1;
    61:       if (*buf != '\0')
    62: 	add_history (buf);
    63: 
    64:       res = sexpr_parse_string (buf,
    65: 				(lreg_t (*)(lreg_t, void *))eval,
    66: 				(void *)null_env);
    67:       if (isatty (fileno (outfd)))
    68: 	{
    69: 	  fprintf (outfd, "=> ");
    70: 	  sexpr_fprint (outfd, res);
    71: 	  fprintf (outfd, "\n");
    72: 	}
    73:     }
    74:   while (1);
    75: 
    76:   lac_off_error ();
    77:   return r;
    78: }
    79: 
    80: int
    81: main ()
    82: {
    83: 
    84:   signal (SIGINT, sigint);
    85: 
    86:   lac_on_error ({
    87:       lac_error_print (stderr); exit (-1);
    88:     });
    89: 
    90:   null_env = lac_init ();
    91:   repl (stdin, stdout, stderr);
    92:   fprintf (stdout, "\ngoodbye!\n");
    93:   return 0;
    94: }

Generated by git2html.