Python envy. SRFI-22 -r can now be used without a script,

and the main will be inferred from loaded modules.
See issue #182.
This commit is contained in:
Alex Shinn 2013-06-19 23:15:43 +09:00
parent 11b7a05308
commit 6e5f1e80f3

18
main.c
View file

@ -1,5 +1,5 @@
/* main.c -- chibi-scheme command-line app */ /* main.c -- chibi-scheme command-line app */
/* Copyright (c) 2009-2012 Alex Shinn. All rights reserved. */ /* Copyright (c) 2009-2013 Alex Shinn. All rights reserved. */
/* BSD-style license: http://synthcode.com/license.txt */ /* BSD-style license: http://synthcode.com/license.txt */
#include "chibi/eval.h" #include "chibi/eval.h"
@ -40,6 +40,7 @@ void sexp_usage(int err) {
#endif #endif
" -e <expr> - evaluate an expression\n" " -e <expr> - evaluate an expression\n"
" -p <expr> - evaluate and print an expression\n" " -p <expr> - evaluate and print an expression\n"
" -r[<main>] - run a SRFI-22 main\n"
#if SEXP_USE_IMAGE_LOADING #if SEXP_USE_IMAGE_LOADING
" -d <file> - dump an image file and exit\n" " -d <file> - dump an image file and exit\n"
" -i <file> - load an image file\n" " -i <file> - load an image file\n"
@ -316,7 +317,7 @@ void run_main (int argc, char **argv) {
#endif #endif
char *arg, *prefix=NULL, *suffix=NULL, *main_symbol=NULL; char *arg, *prefix=NULL, *suffix=NULL, *main_symbol=NULL;
sexp_sint_t i, j, c, quit=0, print=0, init_loaded=0, mods_loaded=0, sexp_sint_t i, j, c, quit=0, print=0, init_loaded=0, mods_loaded=0,
fold_case=SEXP_DEFAULT_FOLD_CASE_SYMS; no_script=0, fold_case=SEXP_DEFAULT_FOLD_CASE_SYMS;
sexp_uint_t heap_size=0, heap_max_size=SEXP_MAXIMUM_HEAP_SIZE; sexp_uint_t heap_size=0, heap_max_size=SEXP_MAXIMUM_HEAP_SIZE;
sexp out=SEXP_FALSE, env=NULL, ctx=NULL; sexp out=SEXP_FALSE, env=NULL, ctx=NULL;
sexp_gc_var3(tmp, sym, args); sexp_gc_var3(tmp, sym, args);
@ -395,6 +396,7 @@ void run_main (int argc, char **argv) {
case '-': case '-':
if (argv[i][2] == '\0') { if (argv[i][2] == '\0') {
i++; i++;
no_script = 1;
goto done_options; goto done_options;
} }
sexp_usage(1); sexp_usage(1);
@ -467,13 +469,18 @@ void run_main (int argc, char **argv) {
done_options: done_options:
if (! quit) { if (! quit) {
load_init(); load_init();
/* build argument list */
if (i < argc) if (i < argc)
for (j=argc-1; j>=i; j--) for (j=argc-1; j>=i; j--)
args = sexp_cons(ctx, tmp=sexp_c_string(ctx,argv[j],-1), args); args = sexp_cons(ctx, tmp=sexp_c_string(ctx,argv[j],-1), args);
else if (i >= argc || no_script)
args = sexp_cons(ctx, tmp=sexp_c_string(ctx,argv[0],-1), args); args = sexp_cons(ctx, tmp=sexp_c_string(ctx,argv[0],-1), args);
sexp_set_parameter(ctx, env, sym=sexp_intern(ctx, sexp_argv_symbol, -1), args); sexp_set_parameter(ctx, env, sym=sexp_intern(ctx, sexp_argv_symbol, -1), args);
if (i < argc) { /* script usage */ if (i >= argc && main_symbol == NULL) {
/* no script or main, run interactively */
repl(ctx, env);
} else {
if (i < argc && !no_script) { /* script usage */
#if SEXP_USE_MODULES #if SEXP_USE_MODULES
/* reset the environment to have only the `import' and */ /* reset the environment to have only the `import' and */
/* `cond-expand' bindings */ /* `cond-expand' bindings */
@ -508,6 +515,7 @@ void run_main (int argc, char **argv) {
#if SEXP_USE_WARN_UNDEFS #if SEXP_USE_WARN_UNDEFS
sexp_warn_undefs(ctx, env, tmp, SEXP_VOID); sexp_warn_undefs(ctx, env, tmp, SEXP_VOID);
#endif #endif
}
/* SRFI-22: run main if specified */ /* SRFI-22: run main if specified */
if (main_symbol) { if (main_symbol) {
sym = sexp_intern(ctx, main_symbol, -1); sym = sexp_intern(ctx, main_symbol, -1);
@ -517,8 +525,6 @@ void run_main (int argc, char **argv) {
check_exception(ctx, sexp_apply(ctx, tmp, args)); check_exception(ctx, sexp_apply(ctx, tmp, args));
} }
} }
} else {
repl(ctx, env);
} }
} }