aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-04-19 13:55:30 -0500
committerJoel Martin <github@martintribe.org>2014-04-19 13:57:00 -0500
commit9e5b215158a40ad983cbc22464761524845dd9bf (patch)
tree630a976d83ba43bbe78da57ea397b2d7e828b173
parent86b689f3d7111a9fa13da389a30f3dfdf877d1a4 (diff)
downloadmal-9e5b215158a40ad983cbc22464761524845dd9bf.tar.gz
mal-9e5b215158a40ad983cbc22464761524845dd9bf.zip
Perl: add readline interface and step0_repl
-rw-r--r--README.md11
-rw-r--r--docs/TODO1
-rw-r--r--perl/readline.pm35
-rw-r--r--perl/step0_repl.pl33
4 files changed, 79 insertions, 1 deletions
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");
+}