klisp

an open source interpreter for the Kernel Programming Language.
git clone http://git.hanabi.in/repos/klisp.git
Log | Files | Refs | README

commit 1f9c663b61890c75280b3163e2667c7c216a2d7d
parent b2460708bd14e45c1c32de32dcb55eec5c20a8ea
Author: Andres Navarro <canavarro82@gmail.com>
Date:   Mon, 21 Nov 2011 05:30:02 -0300

Added back repl. Work in the new standalone interpreter almost done. (There is still some cleaning to do and add back script params and ret values)

Diffstat:
Msrc/Makefile | 87+++++++++++++++++++++++++++++++++++++++++++------------------------------------
Msrc/kgports.c | 8++++----
Msrc/kgports.h | 4++--
Msrc/kground.c | 4++--
Msrc/klisp.c | 121++++++++++++++++++-------------------------------------------------------------
Msrc/klispconf.h | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/krepl.c | 136+++++++++++++++++++++++++++++++------------------------------------------------
Msrc/kstate.c | 17+++++++++++++++++
8 files changed, 248 insertions(+), 224 deletions(-)

diff --git a/src/Makefile b/src/Makefile @@ -71,8 +71,9 @@ $(KRN_T): $(KRN_O) $(KRN_A) clean: $(RM) $(ALL_T) $(ALL_O) kgffi.o +# XXX this fails if USE_LIBFFI is not defined depend: - @$(CC) $(CFLAGS) -MM k*.c imath.c imrat.c + @$(CC) $(CFLAGS) -DKUSE_LIBFFI=1 -MM k*.c imath.c imrat.c echo: @echo "PLAT = $(PLAT)" @@ -117,7 +118,7 @@ kapplicative.o: kapplicative.c kobject.h klimits.h klisp.h klispconf.h \ kauxlib.o: kauxlib.c klisp.h kobject.h klimits.h klispconf.h kstate.h \ ktoken.h kmem.h kbytevector.o: kbytevector.c kbytevector.h kobject.h klimits.h klisp.h \ -klispconf.h kstate.h ktoken.h kmem.h kgc.h kstring.h + klispconf.h kstate.h ktoken.h kmem.h kgc.h kstring.h kcontinuation.o: kcontinuation.c kcontinuation.h kobject.h klimits.h \ klisp.h klispconf.h kstate.h ktoken.h kmem.h kgc.h kencapsulation.o: kencapsulation.c kobject.h klimits.h klisp.h \ @@ -126,23 +127,23 @@ kenvironment.o: kenvironment.c kenvironment.h kobject.h klimits.h klisp.h \ klispconf.h kstate.h ktoken.h kmem.h kpair.h kgc.h ksymbol.h kstring.h \ kerror.h ktable.h kapplicative.h koperative.h kerror.o: kerror.c klisp.h kobject.h klimits.h klispconf.h kpair.h \ - kstate.h ktoken.h kmem.h kgc.h kstring.h + kstate.h ktoken.h kmem.h kgc.h kstring.h kerror.h keval.o: keval.c klisp.h kobject.h klimits.h klispconf.h kstate.h \ ktoken.h kmem.h kpair.h kgc.h kenvironment.h kcontinuation.h kerror.h -kgbytevectors.o: kgbytevectors.c kstate.h klimits.h klisp.h kobject.h \ - klispconf.h ktoken.h kmem.h kapplicative.h koperative.h kcontinuation.h \ - kerror.h kbytevector.h kghelpers.h kpair.h kgc.h kenvironment.h ksymbol.h \ - kstring.h kgbytevectors.h kgnumbers.h kgbooleans.o: kgbooleans.c kobject.h klimits.h klisp.h klispconf.h \ kstate.h ktoken.h kmem.h kpair.h kgc.h ksymbol.h kstring.h \ kcontinuation.h kerror.h kghelpers.h kapplicative.h koperative.h \ kenvironment.h +kgbytevectors.o: kgbytevectors.c kstate.h klimits.h klisp.h kobject.h \ + klispconf.h ktoken.h kmem.h kapplicative.h koperative.h kcontinuation.h \ + kerror.h kpair.h kgc.h kbytevector.h kghelpers.h kenvironment.h \ + ksymbol.h kstring.h kgbytevectors.h kgnumbers.h kgc.o: kgc.c kgc.h kobject.h klimits.h klisp.h klispconf.h kstate.h \ ktoken.h kmem.h kport.h imath.h imrat.h ktable.h kstring.h kbytevector.h \ - kerror.h + kerror.h kpair.h kgchars.o: kgchars.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ ktoken.h kmem.h kapplicative.h koperative.h kcontinuation.h kerror.h \ - kghelpers.h kpair.h kgc.h kenvironment.h ksymbol.h kstring.h kgchars.h + kpair.h kgc.h kghelpers.h kenvironment.h ksymbol.h kstring.h kgchars.h kgcombiners.o: kgcombiners.c kstate.h klimits.h klisp.h kobject.h \ klispconf.h ktoken.h kmem.h kpair.h kgc.h kenvironment.h kcontinuation.h \ ksymbol.h kstring.h koperative.h kapplicative.h kerror.h kghelpers.h \ @@ -157,7 +158,7 @@ kgcontrol.o: kgcontrol.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ kgcontrol.h kgcombiners.h kgencapsulations.o: kgencapsulations.c kstate.h klimits.h klisp.h \ kobject.h klispconf.h ktoken.h kmem.h kencapsulation.h kapplicative.h \ - koperative.h kerror.h kghelpers.h kpair.h kgc.h kcontinuation.h \ + koperative.h kerror.h kpair.h kgc.h kghelpers.h kcontinuation.h \ kenvironment.h ksymbol.h kstring.h kgencapsulations.h kgenv_mut.o: kgenv_mut.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ ktoken.h kmem.h kpair.h kgc.h kenvironment.h kcontinuation.h ksymbol.h \ @@ -173,12 +174,17 @@ kgeqp.o: kgeqp.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ kinteger.h imath.h krational.h imrat.h kgequalp.o: kgequalp.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ ktoken.h kmem.h kpair.h kgc.h kstring.h kbytevector.h kcontinuation.h \ - kerror.h kghelpers.h kapplicative.h koperative.h kenvironment.h ksymbol.h \ - kgeqp.h kinteger.h imath.h krational.h imrat.h kgequalp.h + kerror.h kghelpers.h kapplicative.h koperative.h kenvironment.h \ + ksymbol.h kgeqp.h kinteger.h imath.h krational.h imrat.h kgequalp.h kgerror.o: kgerror.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ ktoken.h kmem.h kstring.h kpair.h kgc.h kerror.h kghelpers.h \ kapplicative.h koperative.h kcontinuation.h kenvironment.h ksymbol.h \ - kgerror.h + kgerror.h +kgffi.o: kgffi.c imath.h kobject.h klimits.h klisp.h klispconf.h kstate.h \ + ktoken.h kmem.h kinteger.h kpair.h kgc.h kerror.h kbytevector.h \ + kencapsulation.h ktable.h kghelpers.h kapplicative.h koperative.h \ + kcontinuation.h kenvironment.h ksymbol.h kstring.h kgencapsulations.h \ + kgcombiners.h kgcontinuations.h kgffi.h kghelpers.o: kghelpers.c kghelpers.h kstate.h klimits.h klisp.h kobject.h \ klispconf.h ktoken.h kmem.h kerror.h kpair.h kgc.h kapplicative.h \ koperative.h kcontinuation.h kenvironment.h ksymbol.h kstring.h @@ -192,8 +198,8 @@ kgks_vars.o: kgks_vars.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ kgks_vars.h kgnumbers.o: kgnumbers.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ ktoken.h kmem.h kapplicative.h koperative.h kcontinuation.h kerror.h \ - ksymbol.h kstring.h kinteger.h imath.h krational.h imrat.h kreal.h \ - kghelpers.h kpair.h kgc.h kenvironment.h kgnumbers.h kgkd_vars.h + kpair.h kgc.h ksymbol.h kstring.h kinteger.h imath.h krational.h imrat.h \ + kreal.h kghelpers.h kenvironment.h kgnumbers.h kgkd_vars.h kgpair_mut.o: kgpair_mut.c kstate.h klimits.h klisp.h kobject.h \ klispconf.h ktoken.h kmem.h kpair.h kgc.h kcontinuation.h ksymbol.h \ kstring.h kerror.h kghelpers.h kapplicative.h koperative.h \ @@ -204,10 +210,10 @@ kgpairs_lists.o: kgpairs_lists.c kstate.h klimits.h klisp.h kobject.h \ kenvironment.h ksymbol.h kerror.h kghelpers.h kapplicative.h \ koperative.h kgequalp.h kgpairs_lists.h kgnumbers.h kinteger.h imath.h kgports.o: kgports.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ - ktoken.h kmem.h kport.h kenvironment.h kapplicative.h koperative.h \ - kcontinuation.h kpair.h kgc.h kerror.h ksymbol.h kstring.h kread.h \ - kwrite.h kghelpers.h kgports.h kgcontinuations.h kgcontrol.h kgkd_vars.h \ - kscript.h kbytevector.h + ktoken.h kmem.h kport.h kstring.h kbytevector.h kenvironment.h \ + kapplicative.h koperative.h kcontinuation.h kpair.h kgc.h kerror.h \ + ksymbol.h kread.h kwrite.h kscript.h kghelpers.h kgports.h \ + kgcontinuations.h kgcontrol.h kgkd_vars.h kgpromises.o: kgpromises.c kstate.h klimits.h klisp.h kobject.h \ klispconf.h ktoken.h kmem.h kpromise.h kpair.h kgc.h kapplicative.h \ koperative.h kcontinuation.h kerror.h kghelpers.h kenvironment.h \ @@ -219,11 +225,11 @@ kground.o: kground.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ kgequalp.h kgsymbols.h kgcontrol.h kgpairs_lists.h kgpair_mut.h \ kgenvironments.h kgenv_mut.h kgcombiners.h kgcontinuations.h \ kgencapsulations.h kgpromises.h kgkd_vars.h kgks_vars.h kgnumbers.h \ - kgstrings.h kgchars.h kgports.h kgbytevectors.h ktable.h keval.h krepl.h \ - kscript.h kgsystem.h kgerror.h kgffi.h + kgstrings.h kgchars.h kgports.h kgbytevectors.h kgsystem.h kgerror.h \ + $(if $(USE_LIBFFI), kgffi.h) ktable.h keval.h krepl.h kscript.h kgstrings.o: kgstrings.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ ktoken.h kmem.h kapplicative.h koperative.h kcontinuation.h kerror.h \ - ksymbol.h kstring.h kghelpers.h kpair.h kgc.h kenvironment.h kgchars.h \ + kpair.h kgc.h ksymbol.h kstring.h kghelpers.h kenvironment.h kgchars.h \ kgstrings.h kgnumbers.h kgsymbols.o: kgsymbols.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ ktoken.h kmem.h kcontinuation.h kpair.h kgc.h kstring.h ksymbol.h \ @@ -233,23 +239,22 @@ kgsystem.o: kgsystem.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ ktoken.h kmem.h kpair.h kgc.h kerror.h kghelpers.h kapplicative.h \ koperative.h kcontinuation.h kenvironment.h ksymbol.h kstring.h \ kgsystem.h -kgffi.o: kgsystem.c kstate.h klimits.h klisp.h kobject.h klispconf.h \ - ktoken.h kmem.h kpair.h kgc.h kerror.h kghelpers.h kapplicative.h \ - koperative.h kcontinuation.h kenvironment.h ksymbol.h kstring.h \ - kbytevector.h kencapsulation.h kgencapsulations.h kgffi.h kinteger.o: kinteger.c kinteger.h kobject.h klimits.h klisp.h klispconf.h \ kstate.h ktoken.h kmem.h imath.h kgc.h klisp.o: klisp.c klimits.h klisp.h kobject.h klispconf.h kstate.h \ - ktoken.h kmem.h kauxlib.h + ktoken.h kmem.h kauxlib.h kstring.h kcontinuation.h koperative.h \ + kenvironment.h kport.h kread.h kwrite.h kerror.h kpair.h kgc.h \ + kgcontinuations.h kghelpers.h kapplicative.h ksymbol.h kgcontrol.h \ + kscript.h krepl.h kmem.o: kmem.c klisp.h kobject.h klimits.h klispconf.h kstate.h ktoken.h \ - kmem.h kerror.h kgc.h + kmem.h kerror.h kpair.h kgc.h kobject.o: kobject.c kobject.h klimits.h klisp.h klispconf.h koperative.o: koperative.c koperative.h kobject.h klimits.h klisp.h \ klispconf.h kstate.h ktoken.h kmem.h kgc.h kpair.o: kpair.c kpair.h kobject.h klimits.h klisp.h klispconf.h kstate.h \ ktoken.h kmem.h kgc.h kport.o: kport.c kport.h kobject.h klimits.h klisp.h klispconf.h kstate.h \ - ktoken.h kmem.h kerror.h kstring.h kbytevector.h kgc.h + ktoken.h kmem.h kerror.h kpair.h kgc.h kstring.h kbytevector.h kpromise.o: kpromise.c kobject.h klimits.h klisp.h klispconf.h kstate.h \ ktoken.h kmem.h kpromise.h kpair.h kgc.h krational.o: krational.c krational.h kobject.h klimits.h klisp.h \ @@ -260,32 +265,34 @@ kreal.o: kreal.c kreal.h kobject.h klimits.h klisp.h klispconf.h kstate.h \ ktoken.h kmem.h kinteger.h imath.h krational.h imrat.h kgc.h kpair.h \ kerror.h krepl.o: krepl.c klisp.h kobject.h klimits.h klispconf.h kstate.h \ - ktoken.h kmem.h kcontinuation.h kenvironment.h kerror.h kread.h kwrite.h \ - kstring.h krepl.h ksymbol.h kport.h kpair.h kgc.h ktable.h + ktoken.h kmem.h kcontinuation.h kenvironment.h kerror.h kpair.h kgc.h \ + kread.h kwrite.h kstring.h krepl.h ksymbol.h kport.h kgerror.h \ + kghelpers.h kapplicative.h koperative.h ktable.h kgcontinuations.h kscript.o: kscript.c klisp.h kobject.h klimits.h klispconf.h kstate.h \ - ktoken.h kmem.h kcontinuation.h kenvironment.h kerror.h kread.h kwrite.h \ - kstring.h krepl.h kscript.h ksymbol.h kport.h kpair.h kgc.h ktable.h \ - kgcontrol.h + ktoken.h kmem.h kcontinuation.h kenvironment.h kerror.h kpair.h kgc.h \ + kread.h kwrite.h kstring.h krepl.h kscript.h ksymbol.h kport.h \ + kgcontrol.h kghelpers.h kapplicative.h koperative.h kgerror.h ktable.h kstate.o: kstate.c klisp.h kobject.h klimits.h klispconf.h kstate.h \ ktoken.h kmem.h kstring.h kpair.h kgc.h keval.h koperative.h \ - kapplicative.h kcontinuation.h kenvironment.h kground.h krepl.h kscript.h \ - ksymbol.h kport.h ktable.h kbytevector.h kgpairs_lists.h kghelpers.h kerror.h + kapplicative.h kcontinuation.h kenvironment.h kground.h krepl.h \ + kscript.h ksymbol.h kport.h ktable.h kbytevector.h kgpairs_lists.h \ + kghelpers.h kerror.h kgerror.h kstring.o: kstring.c kstring.h kobject.h klimits.h klisp.h klispconf.h \ kstate.h ktoken.h kmem.h kgc.h ksymbol.o: ksymbol.c ksymbol.h kobject.h klimits.h klisp.h klispconf.h \ - kstate.h ktoken.h kmem.h kstring.h kgc.h kstring.h + kstate.h ktoken.h kmem.h kstring.h kgc.h ktable.o: ktable.c klisp.h kobject.h klimits.h klispconf.h kgc.h kstate.h \ ktoken.h kmem.h ktable.h kapplicative.h koperative.h kgeqp.h kinteger.h \ imath.h krational.h imrat.h kghelpers.h kerror.h kpair.h kcontinuation.h \ kenvironment.h ksymbol.h kstring.h ktoken.o: ktoken.c ktoken.h kobject.h klimits.h klisp.h klispconf.h \ kstate.h kmem.h kinteger.h imath.h krational.h imrat.h kreal.h kpair.h \ - kgc.h kstring.h ksymbol.h kerror.h kport.h kbytevector.h + kgc.h kstring.h kbytevector.h ksymbol.h kerror.h kport.h kwrite.o: kwrite.c kwrite.h kobject.h klimits.h klisp.h klispconf.h \ kstate.h ktoken.h kmem.h kinteger.h imath.h krational.h imrat.h kreal.h \ kpair.h kgc.h kstring.h ksymbol.h kerror.h ktable.h kport.h \ kenvironment.h kbytevector.h imath.o: imath.c imath.h kobject.h klimits.h klisp.h klispconf.h kstate.h \ - ktoken.h kmem.h kerror.h + ktoken.h kmem.h kerror.h kpair.h kgc.h imrat.o: imrat.c imrat.h imath.h kobject.h klimits.h klisp.h klispconf.h \ - kstate.h ktoken.h kmem.h kerror.h + kstate.h ktoken.h kmem.h kerror.h kpair.h kgc.h diff --git a/src/kgports.c b/src/kgports.c @@ -245,7 +245,7 @@ void get_output_buffer(klisp_State *K, TValue *xparams, TValue ptree, } /* 15.1.7 read */ -void read(klisp_State *K, TValue *xparams, TValue ptree, TValue denv) +void gread(klisp_State *K, TValue *xparams, TValue ptree, TValue denv) { UNUSED(xparams); UNUSED(denv); @@ -272,7 +272,7 @@ void read(klisp_State *K, TValue *xparams, TValue ptree, TValue denv) } /* 15.1.8 write */ -void write(klisp_State *K, TValue *xparams, TValue ptree, TValue denv) +void gwrite(klisp_State *K, TValue *xparams, TValue ptree, TValue denv) { UNUSED(xparams); UNUSED(denv); @@ -935,9 +935,9 @@ void kinit_ports_ground_env(klisp_State *K) 1, b2tv(true)); /* 15.1.7 read */ - add_applicative(K, ground_env, "read", read, 0); + add_applicative(K, ground_env, "read", gread, 0); /* 15.1.8 write */ - add_applicative(K, ground_env, "write", write, 0); + add_applicative(K, ground_env, "write", gwrite, 0); /* 15.1.? eof-object? */ add_applicative(K, ground_env, "eof-object?", typep, 2, symbol, diff --git a/src/kgports.h b/src/kgports.h @@ -61,10 +61,10 @@ void get_output_buffer(klisp_State *K, TValue *xparams, TValue ptree, TValue denv); /* 15.1.7 read */ -void read(klisp_State *K, TValue *xparams, TValue ptree, TValue denv); +void gread(klisp_State *K, TValue *xparams, TValue ptree, TValue denv); /* 15.1.8 write */ -void write(klisp_State *K, TValue *xparams, TValue ptree, TValue denv); +void gwrite(klisp_State *K, TValue *xparams, TValue ptree, TValue denv); /* 15.1.? eof-object? */ /* uses typep */ diff --git a/src/kground.c b/src/kground.c @@ -67,11 +67,11 @@ void kinit_cont_names(klisp_State *K) Table *t = tv2table(K->cont_name_table); /* REPL, root-continuation & error-continuation */ - add_cont_name(K, t, do_repl_exit, "exit"); + add_cont_name(K, t, do_root_exit, "exit"); + add_cont_name(K, t, do_error_exit, "error"); add_cont_name(K, t, do_repl_read, "repl-read"); add_cont_name(K, t, do_repl_eval, "repl-eval"); add_cont_name(K, t, do_repl_loop, "repl-loop"); - add_cont_name(K, t, do_repl_error, "repl-report-error"); /* SCRIPT, root-continuation & error-continuation */ add_cont_name(K, t, do_script_exit, "script-exit"); diff --git a/src/klisp.c b/src/klisp.c @@ -4,6 +4,11 @@ ** See Copyright Notice in klisp.h */ +/* +** TODO This needs a serious clean up, I hacked it together during +** an all nighter... +*/ + #include <stdio.h> #include <string.h> #include <stdlib.h> @@ -28,97 +33,10 @@ #include "kgcontinuations.h" /* for do_pass_value */ #include "kgcontrol.h" /* for do_seq */ #include "kscript.h" +#include "krepl.h" /* TODO update dependencies in makefile */ -/* TODO this should be moved to a file named like klispconf.h (see lua) */ - -/* -** ================================================================== -** Search for "@@" to find all configurable definitions. -** =================================================================== -*/ - -/* -@@ KLISP_ANSI controls the use of non-ansi features. -** CHANGE it (define it) if you want Klisp to avoid the use of any -** non-ansi feature or library. -*/ -#if defined(__STRICT_ANSI__) -#define KLISP_ANSI -#endif - - -#if !defined(KLISP_ANSI) && defined(_WIN32) -#define KLISP_WIN -#endif - -#if defined(KLISP_USE_LINUX) -#define KLISP_USE_POSIX -#define KLISP_USE_DLOPEN /* needs an extra library: -ldl */ -#define KLISP_USE_READLINE /* needs some extra libraries */ -#endif - -#if defined(KLISP_USE_MACOSX) -#define KLISP_USE_POSIX -#define KLISP_DL_DYLD /* does not need extra library */ -#endif - -/* -@@ KLISP_PROGNAME is the default name for the stand-alone klisp program. -** CHANGE it if your stand-alone interpreter has a different name and -** your system is not able to detect that name automatically. -*/ -#define KLISP_PROGNAME "klisp" - -/* -@@ KLISP_QL describes how error messages quote program elements. -** CHANGE it if you want a different appearance. -*/ -#define KLISP_QL(x) "'" x "'" -#define KLISP_QS KLISP_QL("%s") -/* /TODO */ - -/* -@@ KLISP_USE_POSIX includes all functionallity listed as X/Open System -@* Interfaces Extension (XSI). -** CHANGE it (define it) if your system is XSI compatible. -*/ -#if defined(KLISP_USE_POSIX) -#define KLISP_USE_MKSTEMP -#define KLISP_USE_ISATTY -#define KLISP_USE_POPEN -#define KLISP_USE_ULONGJMP -#endif - -/* -@@ LUA_PATH and LUA_CPATH are the names of the environment variables that -@* Lua check to set its paths. -@@ KLISP_INIT is the name of the environment variable that klisp -@* checks for initialization code. -** CHANGE them if you want different names. -*/ -//#define LUA_PATH "LUA_PATH" -//#define LUA_CPATH "LUA_CPATH" -#define KLISP_INIT "KLISP_INIT" - -/* -@@ klisp_stdin_is_tty detects whether the standard input is a 'tty' (that -@* is, whether we're running klisp interactively). -** CHANGE it if you have a better definition for non-POSIX/non-Windows -** systems. -*/ -#if defined(KLISP_USE_ISATTY) -#include <unistd.h> -#define klisp_stdin_is_tty() isatty(0) -#elif defined(KLISP_WIN) -#include <io.h> -#include <stdio.h> -#define klisp_stdin_is_tty() _isatty(_fileno(stdin)) -#else -#define klisp_stdin_is_tty() 1 /* assume stdin is a tty */ -#endif - static const char *progname = KLISP_PROGNAME; static void print_usage (void) @@ -146,7 +64,8 @@ static void k_message (const char *pname, const char *msg) fflush(stderr); } -/* TODO move this to a common place to use it from elsewhere */ +/* TODO move this to a common place to use it from elsewhere +(like the repl) */ static void show_error(klisp_State *K, TValue obj) { /* FOR NOW used only for irritant list */ TValue port = kcdr(K->kd_error_port_key); @@ -381,6 +300,7 @@ static int dofile(klisp_State *K, const char *name) /* create a file input port (unless it's stdin, then just use) */ TValue port; + /* XXX better do this in a continuation */ if (name == NULL) { port = kcdr(K->kd_in_port_key); } else { @@ -388,11 +308,14 @@ static int dofile(klisp_State *K, const char *name) if (file == NULL) { TValue mode_str = kstring_new_b(K, "r"); krooted_tvs_push(K, mode_str); + TValue name_str = kstring_new_b(K, name); + krooted_tvs_push(K, mode_str); TValue error_obj = klispE_new_simple_with_errno_irritants - (K, "fopen", 2, name, mode_str); + (K, "fopen", 2, name_str, mode_str); + krooted_tvs_pop(K); krooted_tvs_pop(K); K->next_value = error_obj; - return 1; + return report(K, 1); } TValue name_str = kstring_new_b(K, name); @@ -454,6 +377,15 @@ static int dofile(klisp_State *K, const char *name) return report(K, status); } +static void dotty(klisp_State *K) +{ + TValue env = K->next_env; + kinit_repl(K); + klispS_run(K); + /* get the standard environment again in K->next_env */ + K->next_env = env; +} + static int handle_script(klisp_State *K, char **argv, int n) { const char *fname; @@ -558,6 +490,7 @@ static int pmain(klisp_State *K) /* This is weird but was done to follow lua scheme */ struct Smain *s = (struct Smain *) pvalue(K->next_value); char **argv = s->argv; + s->status = 0; /* There is a standard env in K->next_env, a common one is used for all evaluations (init, expression args, script/repl) */ @@ -603,12 +536,12 @@ static int pmain(klisp_State *K) if (s->status != 0) return 0; - if (has_i) { /* TODO FIX REPL */ - s->status = 0; + if (has_i) { + dotty(K); } else if (script == 0 && !has_e && !has_v) { if (true) { print_version(); - s->status = 0; /* TODO FIX REPL */ + dotty(K); } else { s->status = dofile(K, NULL); } diff --git a/src/klispconf.h b/src/klispconf.h @@ -13,6 +13,101 @@ #include <stdint.h> #include <stdbool.h> +/* +** ================================================================== +** Search for "@@" to find all configurable definitions. +** =================================================================== +*/ + +/* +@@ KLISP_ANSI controls the use of non-ansi features. +** CHANGE it (define it) if you want Klisp to avoid the use of any +** non-ansi feature or library. +*/ +#if defined(__STRICT_ANSI__) +#define KLISP_ANSI +#endif + + +#if !defined(KLISP_ANSI) && defined(_WIN32) +#define KLISP_WIN +#endif + +#if defined(KLISP_USE_LINUX) +#define KLISP_USE_POSIX +#define KLISP_USE_DLOPEN /* needs an extra library: -ldl */ +#define KLISP_USE_READLINE /* needs some extra libraries */ +#endif + +#if defined(KLISP_USE_MACOSX) +#define KLISP_USE_POSIX +#define KLISP_DL_DYLD /* does not need extra library */ +#endif + +/* +@@ KLISP_PROGNAME is the default name for the stand-alone klisp program. +** CHANGE it if your stand-alone interpreter has a different name and +** your system is not able to detect that name automatically. +*/ +#define KLISP_PROGNAME "klisp" + +/* +@@ KLISP_QL describes how error messages quote program elements. +** CHANGE it if you want a different appearance. +*/ +#define KLISP_QL(x) "'" x "'" +#define KLISP_QS KLISP_QL("%s") +/* /TODO */ + +/* +@@ KLISP_USE_POSIX includes all functionallity listed as X/Open System +@* Interfaces Extension (XSI). +** CHANGE it (define it) if your system is XSI compatible. +*/ +#if defined(KLISP_USE_POSIX) +#define KLISP_USE_MKSTEMP +#define KLISP_USE_ISATTY +#define KLISP_USE_POPEN +#define KLISP_USE_ULONGJMP +#endif + +/* +@@ LUA_PATH and LUA_CPATH are the names of the environment variables that +@* Lua check to set its paths. +@@ KLISP_INIT is the name of the environment variable that klisp +@* checks for initialization code. +** CHANGE them if you want different names. +*/ +//#define LUA_PATH "LUA_PATH" +//#define LUA_CPATH "LUA_CPATH" +#define KLISP_INIT "KLISP_INIT" + +/* +@@ klisp_stdin_is_tty detects whether the standard input is a 'tty' (that +@* is, whether we're running klisp interactively). +** CHANGE it if you have a better definition for non-POSIX/non-Windows +** systems. +*/ +#if defined(KLISP_USE_ISATTY) +#include <unistd.h> +#define klisp_stdin_is_tty() isatty(0) +#elif defined(KLISP_WIN) +#include <io.h> +#include <stdio.h> +#define klisp_stdin_is_tty() _isatty(_fileno(stdin)) +#else +#define klisp_stdin_is_tty() 1 /* assume stdin is a tty */ +#endif + +/* +@@ KLISP_PROMPT is the default prompt used by stand-alone Klisp. +@@ KLISP_PROMPT2 is not currently used. +** CHANGE them if you want different prompts. +*/ +#define KLISP_PROMPT "klisp> " +/* XXX not used for now */ +#define KLISP_PROMPT2 ">> " + /* temp defines till gc is stabilized */ #define KUSE_GC 1 /* Print msgs when starting and ending gc */ diff --git a/src/krepl.c b/src/krepl.c @@ -22,20 +22,11 @@ #include "kgerror.h" /* for names */ #include "ktable.h" +/* for do_pass_value */ +#include "kgcontinuations.h" /* TODO add names & source info to the repl continuations */ -/* the exit continuation, it exits the loop */ -void do_repl_exit(klisp_State *K, TValue *xparams, TValue obj) -{ - UNUSED(xparams); - UNUSED(obj); - - /* force the loop to terminate */ - K->next_func = NULL; - return; -} - /* the underlying function of the read cont */ void do_repl_read(klisp_State *K, TValue *xparams, TValue obj) { @@ -43,8 +34,7 @@ void do_repl_read(klisp_State *K, TValue *xparams, TValue obj) UNUSED(obj); /* show prompt */ - /* TODO put this in a variable like in lua */ - fprintf(stdout, "klisp> "); + fprintf(stdout, KLISP_PROMPT); TValue port = kcdr(K->kd_in_port_key); klisp_assert(kfport_file(port) == stdin); @@ -72,6 +62,7 @@ void do_repl_eval(klisp_State *K, TValue *xparams, TValue obj) /* this will in turn call main_cont */ /* print a newline to allow the shell a fresh line */ printf("\n"); + /* This is ok because there is no interception possible */ kset_cc(K, K->root_cont); kapply_cc(K, KINERT); } else { @@ -86,14 +77,46 @@ void do_repl_eval(klisp_State *K, TValue *xparams, TValue obj) } void do_repl_loop(klisp_State *K, TValue *xparams, TValue obj); +void do_int_repl_error(klisp_State *K, TValue *xparams, TValue ptree, + TValue denv); /* this is called from both do_repl_loop and do_repl_error */ /* GC: assumes denv is NOT rooted */ -inline void create_loop(klisp_State *K, TValue denv) +void create_loop(klisp_State *K, TValue denv) { krooted_tvs_push(K, denv); + + /* TODO this should be factored out, it is quite common */ + TValue error_int = kmake_operative(K, do_int_repl_error, 1, denv); + krooted_tvs_pop(K); /* already in cont */ + krooted_tvs_push(K, error_int); + TValue exit_guard = kcons(K, K->error_cont, error_int); + krooted_tvs_pop(K); /* already in guard */ + krooted_tvs_push(K, exit_guard); + TValue exit_guards = kcons(K, exit_guard, KNIL); + krooted_tvs_pop(K); /* already in guards */ + krooted_tvs_push(K, exit_guards); + + TValue entry_guards = KNIL; + + /* this is needed for interception code */ + TValue env = kmake_empty_environment(K); + krooted_tvs_push(K, env); + TValue outer_cont = kmake_continuation(K, K->root_cont, + do_pass_value, 2, entry_guards, env); + kset_outer_cont(outer_cont); + krooted_tvs_push(K, outer_cont); + TValue inner_cont = kmake_continuation(K, outer_cont, + do_pass_value, 2, exit_guards, env); + kset_inner_cont(inner_cont); + krooted_tvs_pop(K); krooted_tvs_pop(K); krooted_tvs_pop(K); + + /* stack is empty now */ + krooted_tvs_push(K, inner_cont); + TValue loop_cont = - kmake_continuation(K, K->root_cont, do_repl_loop, 1, denv); + kmake_continuation(K, inner_cont, do_repl_loop, 1, denv); + krooted_tvs_pop(K); /* in loop cont */ krooted_tvs_push(K, loop_cont); TValue eval_cont = kmake_continuation(K, loop_cont, do_repl_eval, 1, denv); krooted_tvs_pop(K); /* in eval cont */ @@ -101,7 +124,6 @@ inline void create_loop(klisp_State *K, TValue denv) TValue read_cont = kmake_continuation(K, eval_cont, do_repl_read, 0); kset_cc(K, read_cont); krooted_tvs_pop(K); - krooted_tvs_pop(K); kapply_cc(K, KINERT); } @@ -124,12 +146,21 @@ void do_repl_loop(klisp_State *K, TValue *xparams, TValue obj) } /* the underlying function of the error cont */ -void do_repl_error(klisp_State *K, TValue *xparams, TValue obj) +void do_int_repl_error(klisp_State *K, TValue *xparams, TValue ptree, + TValue denv) { /* ** xparams[0]: dynamic environment */ + UNUSED(denv); + + /* + ** ptree is (object divert) + */ + TValue obj = kcar(ptree); + TValue divert = kcadr(ptree); + /* FOR NOW used only for irritant list */ TValue port = kcdr(K->kd_error_port_key); klisp_assert(ttisfport(port) && kfport_file(port) == stderr); @@ -198,76 +229,17 @@ void do_repl_error(klisp_State *K, TValue *xparams, TValue obj) "error continuation"); } - TValue denv = xparams[0]; - create_loop(K, denv); + UNUSED(divert); + TValue old_denv = xparams[0]; + /* this is the same as a divert */ + create_loop(K, old_denv); } /* call this to init the repl in a newly created klisp state */ +/* the standard environment should be in K->next_env */ void kinit_repl(klisp_State *K) { - TValue std_env = kmake_environment(K, K->ground_env); - krooted_tvs_push(K, std_env); - - /* set up the continuations */ - TValue root_cont = kmake_continuation(K, KNIL, do_repl_exit, 0); - krooted_tvs_push(K, root_cont); - - TValue error_cont = kmake_continuation(K, root_cont, do_repl_error, - 1, std_env); - krooted_tvs_push(K, error_cont); - - /* update the ground environment with these two conts */ - TValue symbol; - /* TODO si */ - symbol = ksymbol_new(K, "root-continuation", KNIL); - krooted_tvs_push(K, symbol); - kadd_binding(K, K->ground_env, symbol, root_cont); - krooted_tvs_pop(K); - - #if KTRACK_SI - /* TODO: find a cleaner way of doing this..., maybe disable gc */ - /* Add source info to the cont */ - TValue str = kstring_new_b_imm(K, __FILE__); - krooted_tvs_push(K, str); - TValue tail = kcons(K, i2tv(__LINE__), i2tv(0)); - krooted_tvs_push(K, tail); - TValue si = kcons(K, str, tail); - krooted_tvs_push(K, si); - kset_source_info(K, root_cont, si); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - #endif - - /* TODO si */ - symbol = ksymbol_new(K, "error-continuation", KNIL); - krooted_tvs_push(K, symbol); - kadd_binding(K, K->ground_env, symbol, error_cont); - krooted_tvs_pop(K); - - #if KTRACK_SI - str = kstring_new_b_imm(K, __FILE__); - krooted_tvs_push(K, str); - tail = kcons(K, i2tv(__LINE__), i2tv(0)); - krooted_tvs_push(K, tail); - si = kcons(K, str, tail); - krooted_tvs_push(K, si); - kset_source_info(K, error_cont, si); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - #endif - - /* and save them in the structure */ - K->root_cont = root_cont; - K->error_cont = error_cont; - - krooted_tvs_pop(K); - krooted_tvs_pop(K); - krooted_tvs_pop(K); - - /* Create error continuation hierarchy. */ - kinit_error_hierarchy(K); + TValue std_env = K->next_env; #if KTRACK_SI /* save the root cont in next_si to let the loop continuations have diff --git a/src/kstate.c b/src/kstate.c @@ -242,7 +242,24 @@ klisp_State *klisp_newstate (klisp_Alloc f, void *ud) { /* Create the root and error continuation (will be added to the environment in kinit_ground_env) */ K->root_cont = kmake_continuation(K, KNIL, do_root_exit, 0); + + #if KTRACK_SI + /* Add source info to the cont */ + TValue str = kstring_new_b_imm(K, __FILE__); + TValue tail = kcons(K, i2tv(__LINE__), i2tv(0)); + si = kcons(K, str, tail); + kset_source_info(K, K->root_cont, si); + #endif + K->error_cont = kmake_continuation(K, K->root_cont, do_error_exit, 0); + + #if KTRACK_SI + str = kstring_new_b_imm(K, __FILE__); + tail = kcons(K, i2tv(__LINE__), i2tv(0)); + si = kcons(K, str, tail); + kset_source_info(K, K->error_cont, si); + #endif + /* this must be done before calling kinit_ground_env */ kinit_error_hierarchy(K);