From 458304c62174048c658bb5fcc02900ed5bb16c6f Mon Sep 17 00:00:00 2001 From: Yevgen Muntyan <17531749+muntyan@users.noreply.github.com> Date: Wed, 8 Mar 2006 02:19:44 -0600 Subject: [PATCH] Interactive interpreter --- moo.kdevelop | 8 +++--- tests/Makefile.incl | 14 +++++------ tests/mscript.c | 61 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 13 deletions(-) diff --git a/moo.kdevelop b/moo.kdevelop index 8f8fda9c..566b2e14 100644 --- a/moo.kdevelop +++ b/moo.kdevelop @@ -268,16 +268,16 @@ - + A new empty GAP source file - + A new empty C++ file. - + A new empty header file for C/C++. - + A new empty C file. diff --git a/tests/Makefile.incl b/tests/Makefile.incl index b96583cb..ebd875cf 100644 --- a/tests/Makefile.incl +++ b/tests/Makefile.incl @@ -1,4 +1,4 @@ -EXTRA_PROGRAMS += medit mterm markup termbuffer testfileview testpaned testpanedfileview miniglade langparser testobject testfold mscript +EXTRA_PROGRAMS += medit mterm markup termbuffer testfileview testpaned testpanedfileview miniglade langparser testobject testfold ms bin_PROGRAMS = @@ -99,14 +99,14 @@ endif ############################################################################## -## mscript +## ms ## -mscript_LDFLAGS = $(ldflags) -mscript_LDADD = $(ldadd) -mscript_SOURCES = tests/mscript.c +ms_LDFLAGS = $(ldflags) +ms_LDADD = $(ldadd) -lreadline +ms_SOURCES = tests/mscript.c if !MOO_BUILD_LIB -mscript_SOURCES += $(libmoo_sources) -nodist_mscript_SOURCES = $(nodist_libmoo_sources) +ms_SOURCES += $(libmoo_sources) +nodist_ms_SOURCES = $(nodist_libmoo_sources) endif diff --git a/tests/mscript.c b/tests/mscript.c index 6b7bbee8..a351a93e 100644 --- a/tests/mscript.c +++ b/tests/mscript.c @@ -15,6 +15,9 @@ #include #include #include +#include +#include +#include static void usage (const char *prg) @@ -24,6 +27,60 @@ static void usage (const char *prg) } +static int run_interactive (void) +{ + MSContext *ctx; + + using_history (); + + ctx = ms_context_new (); + + while (TRUE) + { + char *line; + MSNode *node; + MSValue *val; + + line = readline (">>> "); + + if (!line) + { + g_print ("\n"); + return 0; + } + + node = ms_script_parse (line); + add_history (line); + free (line); + + if (!node) + { + g_print ("syntax error\n"); + continue; + } + + val = ms_top_node_eval (node, ctx); + ms_node_unref (node); + + if (!val) + { + g_print ("%s\n", ms_context_get_error_msg (ctx)); + ms_context_clear_error (ctx); + continue; + } + + if (!ms_value_is_none (val)) + { + char *s = ms_value_print (val); + g_print ("%s\n", s); + g_free (s); + } + + ms_value_unref (val); + } +} + + int main (int argc, char *argv[]) { const char *file = NULL; @@ -32,11 +89,11 @@ int main (int argc, char *argv[]) MSValue *val; MSContext *ctx; -// gtk_init (&argc, &argv); g_type_init (); + ms_type_init (); if (argc < 2) - usage (argv[0]); + return run_interactive (); if (argc == 2) file = argv[1];