root/misc/kp6-repl.pl

Revision 18420, 3.0 kB (checked in by putter, 15 months ago)

misc/kp6-repl.pl - An draft p5 repl for kp6.

  • Property svn:mime-type set to text/plain; charset=UTF-8
  • Property svn:eol-style set to native
Line 
1package RenameMe; #XXX
2
3BEGIN {
4    chomp(my $lib = `kp6 -lib`);
5    eval("use lib '$lib';");
6}
7use KindaPerl6::Runtime::Perl5::Runtime;
8use File::Temp 'tmpnam';
9use Scriptalicious 1.05;
10use strict;
11
12sub call_kp6 {
13    my($input,$args)=@_;
14    my $tmpfn = tmpnam();
15    open(F,"|kp6 $args > $tmpfn") or die;
16    print F $input;
17    close(F);
18    my $output = `cat $tmpfn`;
19    unlink($tmpfn);
20    return $output;
21}
22
23sub print_repl_help {
24    print ":h             show this help\n";
25    print ":q             quit\n";
26    print ":v             toggles verbose output\n";
27    print ":5 <p5code>    run perl5 code\n";
28    print " <p6code>      run perl6 code\n";
29    print ":l <filename>  run perl6 file\n";
30}
31
32# Design issue [Getting-Expression-Value]
33# kp6 currently generates p5 code which always evaluates to 1.
34# So how should we print the value of the user's expression?
35# One can wrap simple expressions in "say".  "say 3;".
36# But kp6 doesn't currently understand "{;3}" as a way of
37# wrapping expressions without altering scope.
38# And wrapping a function and calling it seems likely to
39# cause trouble (eg, "sub wrap(){ 3 } say wrap();").
40# For now, fudge.
41
42# Design issue [Perserving-Variables]
43# kp6 currently uses our() variables, so they are lost at the
44# end of the eval(), and unavailable for subsequent user input.
45# For now, accumulate all non-error causing p6, and eval the
46# entire accumulation.
47# We can't accumulate p5 instead of p6, because compilation
48# requires knowing previous declarations.
49# So the accumulated p6 keeps getting bigger. :(  XXX
50
51my $accumulated_p6; # See [Perserving-Variables].
52sub run_repl {
53    my $verbose = 0;
54    my $eval_p5 = sub {
55        my($p5)=@_;
56        my @result = eval($p5);
57        print $@ if $@;
58        # @result is always [1].  See [Getting-Expression-Value].
59        #print "",(map{defined $_ ? $_ : 'undef'}@result),"\n";
60    };
61    my $eval_p6 = sub {
62        my($p6)=@_;
63        my $code = $p6;
64
65        # XXX Wrap one-liners in say().  See [Getting-Expression-Value].
66        $code = "say $code;" if $code =~ tr/\n/\n/ <= 1;
67
68        # XXX See [Perserving-Variables].
69        $code = $accumulated_p6 ."\n". $code;
70        my $new_accumulation = $accumulated_p6 ."\n". $p6;
71
72        my $p5 = call_kp6($code,'');
73        if($verbose) {
74            print call_kp6($p6,'-ast | perltidy');
75            print "# p5\n",number_the_lines($p5),"\n";
76        }
77        print "----\n";
78        $eval_p5->($p5);
79
80        $accumulated_p6 = $new_accumulation if !$@;
81    };
82    print_repl_help();
83    while (1) {
84        my $line = prompt_string("p5ugs> ");
85        last if !defined $line;
86        if ($line =~ /\A:h\s*\Z/) { print_repl_help(); next;}
87        if ($line =~ /\A:q\s*\Z/) { exit(0);}
88        if ($line =~ /\A:v\s*\Z/) { $verbose = !$verbose; next;}
89        if ($line =~ /\A:5\s+(.+)/) {
90            $eval_p5->($1);
91            next;
92        }
93        if ($line =~ /\A:l\s+(\S+)/) {
94            my $filename = $1;
95            my $p6 = `cat $filename`;
96            $eval_p6->($p6);
97            next;
98        }
99        $eval_p6->($line);
100    }
101}
102
103sub number_the_lines {
104    my($s)=@_;
105    my $cnt = 1;
106    $s =~ s/^/$cnt++."\t"/mge;
107    $s;
108}
109
110#XXX remove me
111run_repl();
112
1131;
114__END__
Note: See TracBrowser for help on using the browser.