lac : e2e4da1b44bbf4732f7520c8137017b61ed9c924

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

Generated by git2html.