qse/ase/test/lsp/lisp.c

105 lines
1.8 KiB
C
Raw Normal View History

2005-02-05 05:03:05 +00:00
#include <xp/lisp/lisp.h>
2005-02-05 05:18:20 +00:00
#include <xp/c/stdio.h>
2005-02-05 05:43:55 +00:00
#include <xp/c/ctype.h>
2005-02-05 05:03:05 +00:00
2005-02-05 05:18:20 +00:00
#ifdef LINUX
#include <mcheck.h>
#endif
2005-02-15 09:09:09 +00:00
static int get_char (xp_cint_t* ch, void* arg)
2005-02-05 05:03:05 +00:00
{
2005-02-15 09:09:09 +00:00
xp_cint_t c;
2005-02-05 05:18:20 +00:00
2005-02-06 03:27:54 +00:00
c = xp_fgetc(xp_stdin);
2005-02-05 05:18:20 +00:00
if (c == XP_EOF) {
2005-02-06 03:27:54 +00:00
if (xp_ferror(xp_stdin)) return -1;
2005-02-05 05:18:20 +00:00
c = XP_EOF;
}
2005-02-05 05:03:05 +00:00
2005-02-05 05:18:20 +00:00
*ch = c;
2005-02-05 05:03:05 +00:00
return 0;
}
2005-02-05 05:18:20 +00:00
int to_int (const xp_char_t* str)
{
int r = 0;
while (*str != XP_CHAR('\0')) {
if (!xp_isdigit(*str)) break;
r = r * 10 + (*str - XP_CHAR('0'));
str++;
}
return r;
}
2005-02-07 15:10:41 +00:00
#include <locale.h>
2005-02-05 05:18:20 +00:00
int xp_main (int argc, xp_char_t* argv[])
{
xp_lisp_t* lisp;
xp_lisp_obj_t* obj;
#ifdef LINUX
mtrace ();
#endif
2005-02-07 15:10:41 +00:00
setlocale (LC_ALL, "");
2005-02-05 05:18:20 +00:00
if (argc != 3) {
2005-02-06 03:27:54 +00:00
xp_fprintf (xp_stderr, XP_TEXT("usage: %s mem_ubound mem_ubound_inc\n"), argv[0]);
2005-02-05 05:18:20 +00:00
return -1;
}
lisp = xp_lisp_new (to_int(argv[1]), to_int(argv[2]));
if (lisp == NULL) {
xp_fprintf (xp_stderr, XP_TEXT("can't create a lisp instance\n"));
return -1;
}
xp_printf (XP_TEXT("LISP 0.0001\n"));
xp_lisp_set_creader (lisp, get_char, NULL);
for (;;) {
xp_printf (XP_TEXT("%s> "), argv[0]);
obj = xp_lisp_read (lisp);
if (obj == NULL) {
if (lisp->error != XP_LISP_ERR_END &&
lisp->error != XP_LISP_ERR_ABORT) {
xp_fprintf (xp_stderr,
XP_TEXT("error while reading: %d\n"), lisp->error);
}
if (lisp->error < XP_LISP_ERR_SYNTAX) break;
continue;
}
if ((obj = xp_lisp_eval (lisp, obj)) != NULL) {
xp_lisp_print (lisp, obj);
xp_printf (XP_TEXT("\n"));
}
else {
if (lisp->error == XP_LISP_ERR_ABORT) break;
xp_fprintf (xp_stderr,
XP_TEXT("error while reading: %d\n"), lisp->error);
}
/*
printf ("-----------\n");
xp_lisp_print (lisp, obj);
printf ("\n-----------\n");
*/
}
xp_lisp_free (lisp);
#ifdef LINUX
muntrace ();
#endif
return 0;
}