Rosetta Code/Run examples: Difference between revisions

→‎{{header|Perl 6}}: Add Perl6 example
(Liberty BASIC entry)
(→‎{{header|Perl 6}}: Add Perl6 example)
Line 259:
GetTempPath$ = buf$
End Function
</lang>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2018.03}}
This is a fairly comprehensive task code runner. It is set up to work for Perl 6 only at this point, but could be easily tweaked to work with other languages. There is so much variation to the task requirements and calling conventions that it would be problematic to make a general purpose, language agnostic code runner. (Heck, a single language one is hard enough.)
 
By default, this will download the Perl 6 section of any (every) task that has a Perl 6 example, extract the code blocks and attempt to run them. Many tasks require files or user interaction to proceed, others are not complete runnable code blocks (example code fragments), some tasks run forever. To try to deal with and compensate for this, this implementation can load a %resources hash that will: supply input files where necessary, skip unrunnable code fragments, limit long and/or infinite running blocks, supply user interaction code where possible, and skip blocks where user interaction is unavoidable.
 
The complete implementation is too large and cumbersome to post in it's entirety here, only the main task retrieval and execution code is included.
 
For the whole ball of wax see: [https://github.com/thundergnat/rc-run This github repository].
 
Run with no parameters to run every implemented task on Rosetta Code. Feed it a task name to only download / run that task.
 
Note: This is set up to run under Linux. It could be adapted for Windows (or OSX I suppose) fairly easily but I don't have access to those OSs, nor do I care to seek it.
 
<lang perl6>use HTTP::UserAgent;
use URI::Escape;
use JSON::Fast;
use Sort::Naturally;
use MONKEY-SEE-NO-EVAL;
 
my $client = HTTP::UserAgent.new;
my $url = 'http://rosettacode.org/mw';
 
my $lang = 'Perl_6'; # language
my $exe = 'perl6'; # executable to run perl6 in a shell
my $view = 'xdg-open'; #imager viewer, this will open default
my %resource = load-resources();
my $download = True;
 
my @tasks;
 
run('clear');
note 'Retreiving tasks';
 
if @*ARGS {
@tasks = |@*ARGS;
$download = False;
}
 
if $download {
@tasks = mediawiki-query(
$url, 'pages',
:generator<categorymembers>,
:gcmtitle("Category:$lang"),
:gcmlimit<350>,
:rawcontinue(),
:prop<title>
)»<title>.grep( * !~~ /^'Category:'/ );
}
 
for @tasks -> $title {
# If you want to resume partially into automatic
# downloads, adjust $skip to skip that many tasks
my $skip = 0;
next if $++ < $skip;
note "Skipping first $skip tasks..." if $skip;
next unless $title ~~ /\S/; # filter blank lines
say $skip + ++$, " $title";
 
my $page = $client.get("{ $url }/index.php?title={ uri-escape $title }&action=raw").content;
say "Whoops, can't find page: $url/$title :check spelling." and next if $page.elems == 0;
say "Getting code from: http://rosettacode.org/wiki/{ $title.subst(' ', '_', :g) }#Perl_6";
 
my $perl6 = $page.comb(/'=={{header|Perl 6}}==' .+? [<?before \n'=='<-[={]>*'{{header'> || $] /).Str // whoops;
 
if $perl6 ~~ /^^ 'See [[' (.+?) '/Perl_6' / {
$perl6 = $client.get("{ $url }/index.php?title={ uri-escape $/[0].Str ~ '/Perl_6' }&action=raw").content;
}
 
my $name = $title.subst(/<-[0..9A..Za..z]>/, '_', :g);
 
my $dir = mkdir "./rc/$name";
 
spurt( "./rc/$name/$name.txt", $perl6 );
 
# weird break in lang tag to placate terminally confused wiki formatter and syntax highlighter
my @blocks = $perl6.comb(/<?after '<lang perl6>'> .*? <?before '</' 'lang>'> /);
 
for @blocks.kv -> $k, $v {
my $n = $k > 0 ?? $k !! '';
spurt( "./rc/$name/$name$n.p6", $v );
say "Skipping $name$n: ", %resource{"$name$n"}<skip>, "\n"
and next if %resource{"$name$n"}<skip>;
say "\nTesting $name$n";
run-it($name, "$name$n");
}
say '=' x 79;
}
 
sub mediawiki-query ($site, $type, *%query) {
my $url = "$site/api.php?" ~ uri-query-string(
:action<query>, :format<json>, :formatversion<2>, |%query);
my $continue = '';
 
gather loop {
my $response = $client.get("$url&$continue");
my $data = from-json($response.content);
take $_ for $data.<query>.{$type}.values;
$continue = uri-query-string |($data.<query-continue>{*}».hash.hash or last);
}
}
 
sub run-it ($dir, $code) {
my $current = $*CWD;
chdir "./rc/$dir/";
if %resource{$code}<file> -> $fn {
copy "./../resources/{$fn}", "./{$fn}"
}
my @cmd = %resource{$code}<cmd> ?? |%resource{$code}<cmd> !! "$exe $code.p6";
for @cmd -> $cmd {
say "Command line: $cmd\n";
try EVAL(shell $cmd);
}
chdir $current;
say "\nDone $code";
}
 
sub uri-query-string (*%fields) { %fields.map({ "{.key}={uri-escape .value}" }).join('&') }
 
sub clear { "\r" ~ ' ' x 100 ~ "\r" }
 
sub whoops { note "{'#' x 79}\n\nNo code found\nMay be bad markup\n\n{'#' x 79}"; '' }
 
sub load-resources { () } # load resources for finer control
</lang>
 
10,327

edits