Rosetta Code/Run examples: Difference between revisions
→{{header|Perl 6}}: Make more language agnostic, various tweaks
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: Better newline spacing) |
Thundergnat (talk | contribs) (→{{header|Perl 6}}: Make more language agnostic, various tweaks) |
||
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.)
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 %
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
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:
use URI::Escape;
use JSON::Fast;
use MONKEY-SEE-NO-EVAL;
Line 284 ⟶ 283:
my $url = 'http://rosettacode.org/mw';
# Language specific variables
my $
my $
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 $download = True;
Line 293 ⟶ 299:
run('clear');
note '
if @*ARGS {
Line 304 ⟶ 310:
$url, 'pages',
:generator<categorymembers>,
:gcmtitle("Category:$
:gcmlimit<350>,
:rawcontinue(),
Line 322 ⟶ 328:
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) }#
my $
if $
$
}
my $name = $title.subst(/<-[-0..9A..Za..z]>/, '_', :g);
my $dir = mkdir "./rc/tasks/$name";
spurt( "./rc/tasks/$name/$name.txt", $
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 {
my $n =
spurt( "./rc/tasks/$name/$name$n
say "Skipping $name$n: ", %resource{"$name$n"}<skip>, "\n"
and next if %resource{"$name$n"}<skip>;
Line 365 ⟶ 377:
sub run-it ($dir, $code) {
my $current = $*CWD;
chdir "./rc/tasks/$dir/";
if %resource{$code}<file> -> $fn {
copy "./../../resources/{$fn}", "./{$fn}"
}
my @cmd = %resource{$code}<cmd> ?? |%resource{$code}<cmd> !! "$exe $code
for @cmd -> $cmd {
say "\nCommand line: $cmd";
Line 388 ⟶ 400:
{{out}} with command line: '''perl6 RC-run.p6 "Determine if a string is numeric"'''
<pre>
1 Determine if a string is numeric
Getting code from: http://rosettacode.org/wiki/Determine_if_a_string_is_numeric#Perl_6
Line 420 ⟶ 432:
Done Determine_if_a_string_is_numeric
===============================================================================</pre>
Or, if the full %
'''perl6 RC-run.p6 Lucky_and_even_lucky_numbers'''
<pre>
1 Lucky_and_even_lucky_numbers
Getting code from: http://rosettacode.org/wiki/Lucky_and_even_lucky_numbers#Perl_6
|