From 9e5b215158a40ad983cbc22464761524845dd9bf Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Sat, 19 Apr 2014 13:55:30 -0500 Subject: Perl: add readline interface and step0_repl --- README.md | 11 +++++++++++ docs/TODO | 1 - perl/readline.pm | 35 +++++++++++++++++++++++++++++++++++ perl/step0_repl.pl | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 perl/readline.pm create mode 100644 perl/step0_repl.pl diff --git a/README.md b/README.md index 2aa96ae..2688f21 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,17 @@ cd make make -f stepX_YYY.mk ``` +### Perl 5 + +For readline line editing support, install Term::ReadLine::Perl or +Term::ReadLine::Gnu from CPAN. + +``` +cd perl +perl stepX_YYY.pl +``` + + ### PHP 5.3 ``` diff --git a/docs/TODO b/docs/TODO index adde9a8..e5926e0 100644 --- a/docs/TODO +++ b/docs/TODO @@ -29,7 +29,6 @@ Clojure: Java: - step9_interop - - store ILambda in function (like c#)? Javascript: diff --git a/perl/readline.pm b/perl/readline.pm new file mode 100644 index 0000000..d2e2098 --- /dev/null +++ b/perl/readline.pm @@ -0,0 +1,35 @@ +# To get readline line editing functionality, please install +# Term::ReadLine::Gnu (GPL) or Term::ReadLine::Perl (GPL, Artistic) +# from CPAN. + +package readline; +use strict; +use warnings; +use Exporter 'import'; +our @EXPORT_OK = qw( readline ); + +use Term::ReadLine; + +my $_rl = Term::ReadLine->new('Mal'); +$_rl->ornaments(0); +#print "Using ReadLine implementation: " . $_rl->ReadLine() . "\n"; +my $OUT = $_rl->OUT || \*STDOUT; +my $_history_loaded = 0; + +sub readline { + my($prompt) = @_; + my $line = undef; + if (! $_history_loaded) { + $_history_loaded = 1; + # TODO: load history + } + + if (defined ($line = $_rl->readline($prompt))) { + $_rl->addhistory($line) if $line =~ /\S/; + # TODO: save history + return $line; + } else { + return undef; + } +} +1; diff --git a/perl/step0_repl.pl b/perl/step0_repl.pl new file mode 100644 index 0000000..8295cc0 --- /dev/null +++ b/perl/step0_repl.pl @@ -0,0 +1,33 @@ +use strict; +use warnings; +use readline qw(readline); + +# read +sub READ { + my $str = shift; + return $str; +} + +# eval +sub EVAL { + my($ast, $env) = @_; + return $ast; +} + +# print +sub PRINT { + my $exp = shift; + return $exp; +} + +# repl +sub REP { + my $str = shift; + return PRINT(EVAL(READ($str), {})); +} + +while (1) { + my $line = readline("user> "); + if (! defined $line) { last; } + print(REP($line), "\n"); +} -- cgit v1.2.3