aboutsummaryrefslogtreecommitdiff
path: root/perl
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 /perl
parent86b689f3d7111a9fa13da389a30f3dfdf877d1a4 (diff)
downloadmal-9e5b215158a40ad983cbc22464761524845dd9bf.tar.gz
mal-9e5b215158a40ad983cbc22464761524845dd9bf.zip
Perl: add readline interface and step0_repl
Diffstat (limited to 'perl')
-rw-r--r--perl/readline.pm35
-rw-r--r--perl/step0_repl.pl33
2 files changed, 68 insertions, 0 deletions
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");
+}