Rosetta Code/Run examples: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Better newline spacing) |
Thundergnat (talk | contribs) (→{{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 % |
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 |
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 $ |
my $language = 'Perl_6'; # language |
||
my $ |
my $header = 'Perl 6'; # header text |
||
my $exe = 'perl6'; # executable to run perl6 in a shell |
|||
my $ext = '.p6'; # file extension for perl6 code |
|||
⚫ | |||
⚫ | |||
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 ' |
note 'Retrieving tasks'; |
||
if @*ARGS { |
if @*ARGS { |
||
Line 304: | Line 310: | ||
$url, 'pages', |
$url, 'pages', |
||
:generator<categorymembers>, |
:generator<categorymembers>, |
||
:gcmtitle("Category:$ |
: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) }# |
say "Getting code from: http://rosettacode.org/wiki/{ $title.subst(' ', '_', :g) }#$language"; |
||
my $ |
my $entry = $page.comb(/'=={{header|' $header '}}==' .+? [<?before \n'=='<-[={]>*'{{header'> || $] /).Str // whoops; |
||
if $ |
if $entry ~~ /^^ 'See [[' (.+?) '/' $language / { |
||
$ |
$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", $ |
spurt( "./rc/tasks/$name/$name.txt", $entry ); |
||
my @blocks = $entry.comb: $tag; |
|||
⚫ | |||
⚫ | |||
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 = |
my $n = +@blocks == 1 ?? '' !! $k; |
||
spurt( "./rc/$name/$name$n |
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 |
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> |
<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 % |
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> |
<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 |