#!/usr/bin/perl -w

use Lisp::Interpreter qw(lisp_read_eval_print);
use Lisp::Subr::All;  # make builtins available

use Getopt::Std;
use vars qw($opt_v $opt_f);

unless (getopts("vf:")) {
    $0 =~ s,.*/,,;
    die "Usage: $0 [-v] [-f <file>] <forms>...\n";
}

$Lisp::Interpreter::DEBUG++ if $opt_v;

if ($opt_f || @ARGV) {
    my $text = "";
    $text = `cat $opt_f` if $opt_f;
    if (@ARGV) {
	$text .= "@ARGV";
    }
    print lisp_read_eval_print($text), "\n";
} else {
    #local($/) = ".";
    lisp_read_eval_print(<<'EOT');
         ;; Set up some helper functions
         (defun q () (write "Goodbye!") (exit))
         (fset 'quit (symbol-function 'q))
EOT

    my $bold = `tput bold 2>/dev/null`;
    my $norm = `tput rmso 2>/dev/null`;
    my $prompt = "${bold}p-lisp>$norm ";
    print $prompt;
    while (<STDIN>) {
	next if /^\s*$/;
	eval {
	    print lisp_read_eval_print($_), "\n";
	};
	if ($@) {
	    $@ =~ s/ at \S+ line \d+// unless $opt_v;
	    print "\aException: $@";
	}
    } continue {
	print $prompt;
    }
    print "\n";
}
