Rosetta Code/Run examples: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: Better newline spacing)
(→‎{{header|Perl 6}}: Make more language agnostic, various tweaks)
Line 265: Line 265:
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.)
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.
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 %resource 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.
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].
For the whole ball of wax see [https://github.com/thundergnat/rc-run the rc-run 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.
Run with no parameters to run every implemented task on Rosetta Code. Feed it a task name to only download / run that task.
Line 278: Line 278:
use URI::Escape;
use URI::Escape;
use JSON::Fast;
use JSON::Fast;
use Sort::Naturally;
use MONKEY-SEE-NO-EVAL;
use MONKEY-SEE-NO-EVAL;


Line 284: Line 283:
my $url = 'http://rosettacode.org/mw';
my $url = 'http://rosettacode.org/mw';


# Language specific variables
my $lang = 'Perl_6'; # language
my $exe = 'perl6'; # executable to run perl6 in a shell
my $language = 'Perl_6'; # language
my $view = 'xdg-open'; #imager viewer, this will open default
my $header = 'Perl 6'; # header text
my $exe = 'perl6'; # executable to run perl6 in a shell
my $ext = '.p6'; # file extension for perl6 code

# weird break in lang tag to placate terminally confused wiki formatter and syntax highlighter
my $tag = rx/<?after '<lang perl6>'> .*? <?before '</' 'lang>'>/; # language entry regex

my $view = 'xdg-open'; # image viewer, this will open default under Linux
my %resource = load-resources();
my %resource = load-resources();
my $download = True;
my $download = True;
Line 293: Line 299:


run('clear');
run('clear');
note 'Retreiving tasks';
note 'Retrieving tasks';


if @*ARGS {
if @*ARGS {
Line 304: Line 310:
$url, 'pages',
$url, 'pages',
:generator<categorymembers>,
:generator<categorymembers>,
:gcmtitle("Category:$lang"),
:gcmtitle("Category:$language"),
:gcmlimit<350>,
:gcmlimit<350>,
:rawcontinue(),
:rawcontinue(),
Line 322: Line 328:
my $page = $client.get("{ $url }/index.php?title={ uri-escape $title }&action=raw").content;
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 "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";
say "Getting code from: http://rosettacode.org/wiki/{ $title.subst(' ', '_', :g) }#$language";


my $perl6 = $page.comb(/'=={{header|Perl 6}}==' .+? [<?before \n'=='<-[={]>*'{{header'> || $] /).Str // whoops;
my $entry = $page.comb(/'=={{header|' $header '}}==' .+? [<?before \n'=='<-[={]>*'{{header'> || $] /).Str // whoops;


if $perl6 ~~ /^^ 'See [[' (.+?) '/Perl_6' / {
if $entry ~~ /^^ 'See [[' (.+?) '/' $language / {
$perl6 = $client.get("{ $url }/index.php?title={ uri-escape $/[0].Str ~ '/Perl_6' }&action=raw").content;
$entry = $client.get("{ $url }/index.php?title={ uri-escape $/[0].Str ~ '/' ~ $language }&action=raw").content;
}
}


my $name = $title.subst(/<-[0..9A..Za..z]>/, '_', :g);
my $name = $title.subst(/<-[-0..9A..Za..z]>/, '_', :g);


my $dir = mkdir "./rc/$name";
my $dir = mkdir "./rc/tasks/$name";


spurt( "./rc/$name/$name.txt", $perl6 );
spurt( "./rc/tasks/$name/$name.txt", $entry );


my @blocks = $entry.comb: $tag;
# weird break in lang tag to placate terminally confused wiki formatter and syntax highlighter

my @blocks = $perl6.comb(/<?after '<lang perl6>'> .*? <?before '</' 'lang>'> /);
unless @blocks {
if %resource{"$name"}<skip> {
whoops;
say "Skipping $name: ", %resource{"$name"}<skip>, "\n";
}
}


for @blocks.kv -> $k, $v {
for @blocks.kv -> $k, $v {
my $n = $k > 0 ?? $k !! '';
my $n = +@blocks == 1 ?? '' !! $k;
spurt( "./rc/$name/$name$n.p6", $v );
spurt( "./rc/tasks/$name/$name$n$ext", $v );
say "Skipping $name$n: ", %resource{"$name$n"}<skip>, "\n"
say "Skipping $name$n: ", %resource{"$name$n"}<skip>, "\n"
and next if %resource{"$name$n"}<skip>;
and next if %resource{"$name$n"}<skip>;
Line 365: Line 377:
sub run-it ($dir, $code) {
sub run-it ($dir, $code) {
my $current = $*CWD;
my $current = $*CWD;
chdir "./rc/$dir/";
chdir "./rc/tasks/$dir/";
if %resource{$code}<file> -> $fn {
if %resource{$code}<file> -> $fn {
copy "./../resources/{$fn}", "./{$fn}"
copy "./../../resources/{$fn}", "./{$fn}"
}
}
my @cmd = %resource{$code}<cmd> ?? |%resource{$code}<cmd> !! "$exe $code.p6";
my @cmd = %resource{$code}<cmd> ?? |%resource{$code}<cmd> !! "$exe $code$ext";
for @cmd -> $cmd {
for @cmd -> $cmd {
say "\nCommand line: $cmd";
say "\nCommand line: $cmd";
Line 388: Line 400:


{{out}} with command line: '''perl6 RC-run.p6 "Determine if a string is numeric"'''
{{out}} with command line: '''perl6 RC-run.p6 "Determine if a string is numeric"'''
<pre>Retreiving tasks
<pre>Retrieving tasks
1 Determine if a string is numeric
1 Determine if a string is numeric
Getting code from: http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Perl_6
Getting code from: http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Perl_6
Line 420: Line 432:
Done Determine_if_a_string_is_numeric
Done Determine_if_a_string_is_numeric
===============================================================================</pre>
===============================================================================</pre>
Or, if the full %resources hash is loaded it will autmatically feed input parameters to tasks that require them:<br>
Or, if the full %resource hash is loaded it will automatically feed input parameters to tasks that require them:<br>
'''perl6 RC-run.p6 Lucky_and_even_lucky_numbers'''
'''perl6 RC-run.p6 Lucky_and_even_lucky_numbers'''
<pre>Retreiving tasks
<pre>Retrieving tasks
1 Lucky_and_even_lucky_numbers
1 Lucky_and_even_lucky_numbers
Getting code from: http://rosettacode.org/wiki/Lucky_and_even_lucky_numbers#Perl_6
Getting code from: http://rosettacode.org/wiki/Lucky_and_even_lucky_numbers#Perl_6