Rosetta Code/Run examples: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: use common code) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 26: | Line 26: | ||
No attempt has been at 'extra credit' which would take all day for Go alone and 'more credit' seems pointless as program output is seldom, if ever, exactly the same for all languages. |
No attempt has been at 'extra credit' which would take all day for Go alone and 'more credit' seems pointless as program output is seldom, if ever, exactly the same for all languages. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 155: | Line 155: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 342: | Line 342: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
' ******************************************************************** |
' ******************************************************************** |
||
' ** ** |
' ** ** |
||
Line 584: | Line 584: | ||
GetTempPath$ = buf$ |
GetTempPath$ = buf$ |
||
End Function |
End Function |
||
</syntaxhighlight> |
|||
</lang> |
|||
Line 597: | Line 597: | ||
The present program must be compiled with option <code>-d:ssl</code> |
The present program must be compiled with option <code>-d:ssl</code> |
||
< |
<syntaxhighlight lang="nim">import htmlparser, httpclient, os, osproc, re, sets, strutils, xmltree |
||
const |
const |
||
Line 687: | Line 687: | ||
answer = stdin.readLine() |
answer = stdin.readLine() |
||
if answer notin ["y", "Y"]: |
if answer notin ["y", "Y"]: |
||
break</ |
break</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Screenshot [http://phix.x10.mx/run_examples.png here] |
Screenshot [http://phix.x10.mx/run_examples.png here] |
||
<!--< |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<span style="color: #000080;font-style:italic;">-- |
<span style="color: #000080;font-style:italic;">-- |
||
-- demo\rosetta\Run_examples.exw |
-- demo\rosetta\Run_examples.exw |
||
Line 1,344: | Line 1,344: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
||
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 1,365: | Line 1,365: | ||
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. |
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 |
<syntaxhighlight lang="raku" line>use HTTP::UserAgent; |
||
use URI::Escape; |
use URI::Escape; |
||
use JSON::Fast; |
use JSON::Fast; |
||
Line 1,677: | Line 1,677: | ||
# note that this tag only selects the syntax highlighting, continue to |
# note that this tag only selects the syntax highlighting, continue to |
||
# use 'perl6' until 'raku' as added on the site. |
# use 'perl6' until 'raku' as added on the site. |
||
tag => rx/<?after '< |
tag => rx/<?after '<syntaxhighlight lang="' 'perl6' '">' > .*? <?before '</' 'lang>'>/, |
||
) } |
) } |
||
Line 1,686: | Line 1,686: | ||
dir => 'perl', |
dir => 'perl', |
||
header => 'Perl', |
header => 'Perl', |
||
tag => rx/:i <?after '< |
tag => rx/:i <?after '<syntaxhighlight lang="' 'perl' '">' > .*? <?before '</' 'lang>'>/, |
||
) } |
) } |
||
Line 1,695: | Line 1,695: | ||
dir => 'python', |
dir => 'python', |
||
header => 'Python', |
header => 'Python', |
||
tag => rx/:i <?after '< |
tag => rx/:i <?after '<syntaxhighlight lang="' 'python' '">' > .*? <?before '</' 'lang>'>/, |
||
) } |
) } |
||
Line 1,704: | Line 1,704: | ||
dir => 'go', |
dir => 'go', |
||
header => 'Go', |
header => 'Go', |
||
tag => rx/:i <?after '< |
tag => rx/:i <?after '<syntaxhighlight lang="' 'go' '">' > .*? <?before '</' 'lang>'>/, |
||
) } |
) } |
||
Line 1,713: | Line 1,713: | ||
dir => 'tcl', |
dir => 'tcl', |
||
header => 'Tcl', |
header => 'Tcl', |
||
tag => rx/:i <?after '< |
tag => rx/:i <?after '<syntaxhighlight lang="' 'tcl' '">' > .*? <?before '</' 'lang>'>/, |
||
) } |
) } |
||
multi load-lang ($unknown) { die "Sorry, don't know how to handle $unknown language." }; |
multi load-lang ($unknown) { die "Sorry, don't know how to handle $unknown language." }; |
||
multi load-resources ($unknown) { () };</ |
multi load-resources ($unknown) { () };</syntaxhighlight> |
||
{{out}} with command line: '''raku RC-run.p6 -q "Determine if a string is numeric"''' |
{{out}} with command line: '''raku RC-run.p6 -q "Determine if a string is numeric"''' |
||
Line 1,825: | Line 1,825: | ||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">bf$ = "<SPAN STYLE='font-family:Arial; font-weight:700; font-size:12pt'>" |
||
a$ = httpGet$("http://rosettacode.org/wiki/Category:Run_BASIC") ' get RB tasks from [RC] |
a$ = httpGet$("http://rosettacode.org/wiki/Category:Run_BASIC") ' get RB tasks from [RC] |
||
a1$ = word$(a$,2,"Pages in category ""Run BASIC") |
a1$ = word$(a$,2,"Pages in category ""Run BASIC") |
||
Line 1,905: | Line 1,905: | ||
i = i + 1 |
i = i + 1 |
||
WEND |
WEND |
||
END FUNCTION</ |
END FUNCTION</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
This code only includes support for running Tcl task solutions, but it can download any language's; it assumes that the first <nowiki><lang…></nowiki> is sufficient when it comes to task extraction (definitely not true universally, but mostly good enough). |
This code only includes support for running Tcl task solutions, but it can download any language's; it assumes that the first <nowiki><lang…></nowiki> is sufficient when it comes to task extraction (definitely not true universally, but mostly good enough). |
||
{{tcllib|uri}} |
{{tcllib|uri}} |
||
< |
<syntaxhighlight lang="tcl"># Code to download task contents from find-bare-lang-tags task |
||
package require Tcl 8.5 |
package require Tcl 8.5 |
||
package require http |
package require http |
||
Line 1,938: | Line 1,938: | ||
} |
} |
||
# Code to extract the first <lang> section for a language |
# Code to extract the first <syntaxhighlight lang="text"> section for a language |
||
proc getTaskCodeForLanguage {task language} { |
proc getTaskCodeForLanguage {task language} { |
||
set content [getTaskContent $task] |
set content [getTaskContent $task] |
||
Line 1,951: | Line 1,951: | ||
} |
} |
||
set content [string range $content [lindex $start 1] [lindex $end 0]] |
set content [string range $content [lindex $start 1] [lindex $end 0]] |
||
# Extended format RE used to allow embedding within _this_ task's <lang>! |
# Extended format RE used to allow embedding within _this_ task's <syntaxhighlight lang="text">! |
||
if {![regexp {(?x)< |
if {![regexp {(?x)<syntaxhighlight lang=".*?">(.*?)</ lang>} $content -> solution]} { |
||
error "$language solution of task \"$task\" has no useful code" |
error "$language solution of task \"$task\" has no useful code" |
||
} |
} |
||
Line 1,969: | Line 1,969: | ||
exec [info nameofexecutable] $filename <@stdin >@stdout 2>@stderr |
exec [info nameofexecutable] $filename <@stdin >@stdout 2>@stderr |
||
} |
} |
||
runTclTaskForLanguage {*}$argv</ |
runTclTaskForLanguage {*}$argv</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 1,987: | Line 1,987: | ||
However, no attempt has been made - at least for now - to run other embedded programs (which can be identified by the presence of the 'foreign' keyword) due to a number of technical difficulties in doing so. |
However, no attempt has been made - at least for now - to run other embedded programs (which can be identified by the presence of the 'foreign' keyword) due to a number of technical difficulties in doing so. |
||
< |
<syntaxhighlight lang="ecmascript">/* rc_run_examples.wren */ |
||
import "./set" for Set |
import "./set" for Set |
||
Line 2,075: | Line 2,075: | ||
var fileName = "rc_temp." + ext |
var fileName = "rc_temp." + ext |
||
var p1 = Pattern.new("/=/={{header/|%(lang2)}}/=/=") |
var p1 = Pattern.new("/=/={{header/|%(lang2)}}/=/=") |
||
var p2 = Pattern.new("< |
var p2 = Pattern.new("<syntaxhighlight lang="%(lang3)">") |
||
var p3 = Pattern.new("<//lang>") |
var p3 = Pattern.new("<//lang>") |
||
var s = p1.split(page, 1, 0) |
var s = p1.split(page, 1, 0) |
||
Line 2,120: | Line 2,120: | ||
if (yn != "y" && yn != "Y") return |
if (yn != "y" && yn != "Y") return |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
We now embed this script in the following Go program and run it: |
We now embed this script in the following Go program and run it: |
||
< |
<syntaxhighlight lang="go">/* go run rc_run_examples.go */ |
||
package main |
package main |
||
Line 2,250: | Line 2,250: | ||
vm.InterpretFile(fileName) |
vm.InterpretFile(fileName) |
||
vm.Free() |
vm.Free() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |