1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-10 12:47:45 +03:00
mal/perl/step1_read_print.pl

63 lines
1.2 KiB
Perl
Raw Normal View History

2014-04-20 00:12:13 +04:00
use strict;
use warnings FATAL => qw(all);
no if $] >= 5.018, warnings => "experimental::smartmatch";
2014-05-11 01:03:25 +04:00
use File::Basename;
use lib dirname (__FILE__);
use readline qw(mal_readline set_rl_mode);
2014-04-20 00:12:13 +04:00
use feature qw(switch);
use reader;
use printer;
2014-04-20 00:12:13 +04:00
# read
sub READ {
my $str = shift;
return reader::read_str($str);
2014-04-20 00:12:13 +04:00
}
# eval
sub EVAL {
my($ast, $env) = @_;
return $ast;
}
# print
sub PRINT {
my $exp = shift;
return printer::_pr_str($exp);
2014-04-20 00:12:13 +04:00
}
# repl
sub REP {
my $str = shift;
return PRINT(EVAL(READ($str), {}));
}
if (scalar(@ARGV) > 0 && $ARGV[0] eq "--raw") {
set_rl_mode("raw");
}
2014-04-20 00:12:13 +04:00
while (1) {
my $line = mal_readline("user> ");
2014-04-20 00:12:13 +04:00
if (! defined $line) { last; }
do {
local $@;
my $ret;
eval {
use autodie; # always "throw" errors
print(REP($line), "\n");
1;
} or do {
my $err = $@;
given (ref $err) {
when (/^BlankException/) {
# ignore and continue
}
default {
chomp $err;
print "Error: $err\n";
}
}
};
};
2014-04-20 00:12:13 +04:00
}