2 # Copyright (C) 2009, Parrot Foundation.
9 use lib "$Bin/../lib"; # install location
10 use lib "$Bin/../../lib"; # build location
14 use File::Temp qw/ tempfile /;
15 use Benchmark qw/timeit timestr :hireswallclock/;
19 tools/dev/parrot_shell.pl - The Parrot Shell
23 % perl tools/dev/parrot_shell.pl
27 The Parrot Shell allows you to rapidly prototype Parrot code. It wraps your code
28 in a ".sub main" and ".end", so you don't have to, unless your code begins with
29 ".sub". It reads code from STDIN until it sees a line containing a single ".",
30 which is how you tell parrot_shell to run the code you are giving to it:
33 parrot_shell 0> $I0 = 42
41 Thanks for visiting the Parrot Shell, come back soon!
43 Each numbered Parrot Shell session is run in its own interpreter, so no registers
44 or variables are shared/leaked between them.
52 $parrot = File::Spec->catfile( ".", "parrot");
54 warn "$parrot not found, attempting to use an installed parrot";
57 my $exefile = $parrot . $PConfig{exe};
64 show_prompt($session_no);
66 while( my $line = <STDIN> ) {
67 exit_shell() if $line =~ m/^q(uit)?$/;
69 if ($line =~ m/^h(elp)?$/) {
71 show_prompt($session_no) if !defined $code;
74 if ($line =~ m/^\s*\.\s*$/) { # Run it, baby!
75 print eval_snippet($code);
88 Welcome to the Parrot Shell, it's experimental!
89 Type h or help for some basic help
90 Type q or quit to flee the madness
96 my ($session_no) = @_;
97 print "\nparrot_shell $session_no> ";
100 print "Thanks for visiting the Parrot Shell, come back soon!\n";
107 The Parrot Shell allows you to rapidly prototype Parrot code. It wraps your code
108 in a ".sub main" and ".end", so you don't have to, unless your code begins with
109 ".sub". It reads code from STDIN until it sees a line containing a single ".",
110 which is how you tell parrot_shell to run the code you are giving to it:
113 parrot_shell> $I0 = 42
124 my $codefn = get_tempfile
();
125 my $stdoutfn = get_tempfile
();
126 my $f = IO
::File
->new(">$codefn");
128 $f->print(normalize_snippet
($snippet || ''));
131 my $time = timestr
(timeit
(1, sub { system("$parrot $codefn >$stdoutfn 2>&1") } ));
134 handle_errors
($?
) if $?
;
136 $f = IO
::File
->new($stdoutfn);
138 my $output = join( '', <$f> );
139 return "Time: $time\nOutput:\n$output";
143 my ($exit_code) = @_;
144 if ($exit_code == -1) {
145 print "Error: failed to execute: $!\n";
147 elsif ($exit_code & 127) {
148 printf "Error: child died with signal %d, %s coredump\n",
149 ($exit_code & 127), ($exit_code & 128) ?
'with' : 'without';
152 printf "Error: child exited with value %d\n", $?
>> 8;
157 my (undef, $name) = tempfile
( CLEANUP
=> 1);
161 sub normalize_snippet
{
164 if ($snippet =~ m/^\.sub/) {
179 # cperl-indent-level: 4
182 # vim: expandtab shiftwidth=4: