Rosetta Code/Run examples: Difference between revisions

m (→‎{{header|Raku}}: Fix up Perl 6 -> Raku code, calling conventions)
Line 585:
End Function
</lang>
 
 
=={{header|Nim}}==
{{trans|Go}}
This is a translation of the Go solution. The accepted languages are Go, Nim, Perl and Python (version 3 only). There is no tentative to check if the requirements to run a program are fulfilled. If they are not, the execution of the external program will fail but without consequences on the main program.
 
On Linux, Perl and Python are installed by default but third party modules may be not. For Go and Nim, an installation is required.
 
Note that when running a Nim program a compilation is done and messages are displayed on the terminal before the messages displayed when executing the compiled program. To avoid compiling issues and execution issues, programs are compiled with options <code>-d:release -d:ssl --threads:on</code>. The tow last options are not needed for most programs.
 
The present program must be compiled with option <code>-d:ssl</code>
 
<lang Nim>import htmlparser, httpclient, os, osproc, re, sets, strutils, xmltree
 
const
Url1 = "http://rosettacode.org/wiki/Category:Programming_Tasks"
Url2 = "http://rosettacode.org/wiki/Category:Draft_Programming_Tasks"
Urls = [Url1, Url2]
 
proc getAllTasks(client: HttpClient): HashSet[string] =
let regex = re("""<li><a href="/wiki/(.*?)"""")
var matches: array[1, string]
var start = 0
for url in Urls:
let body = client.getContent(url)
# Find all tasks.
while true:
start = body.find(regex, matches, start) + 1
if start == 0: break
if not matches[0].startsWith("Category:"):
result.incl matches[0]
 
let client = newHttpClient()
let tasks = client.getAllTasks()
 
while true:
stdout.write "Enter the exact name of the task: "
stdout.flushFile()
let task = stdin.readLine().strip().replace(' ', '_')
if task notin tasks:
echo "Sorry a task with that name doesn't exist."
continue
 
let url = "https://rosettacode.org/mw/index.php?title=" & task & "&action=edit"
let body = client.getContent(url)
var lang: string
while true:
stdout.write "Enter the language Go/Nim/Perl/Python : "
stdout.flushFile()
lang = stdin.readLine().strip().toLowerAscii
if lang in ["go", "nim", "perl", "python"]:
break
echo "Sorry that language is not supported."
 
var lang2, lang3, ext: string
case lang
of "go":
lang2 = "Go"
lang3 = "(go|Go|GO)"
ext = "go"
of "nim":
lang2 = "Nim"
lang3 = "(nim|Nim)"
ext = "nim"
of "perl":
lang2 = "Perl"
lang3 = "(perl|Perl)"
ext = "pl"
of "python":
lang2 = "Python"
lang3 = "(python|Python)"
ext = "py"
 
let fileName = "rc_temp." & ext
let regex = re(r"(?s)==\{\{header\|$#\}\}==.*?&lt;lang $#>(.*?)&lt;/lang>".format(lang2, lang3))
var matches: array[2, string]
let idx = body.find(regex, matches)
if idx < 0:
echo "No runnable task entry for that language was detected."
continue
 
let source = parseHtml(matches[1]).innerText()
echo "\nThis is the source code for the first or only runnable program:\n"
echo source
stdout.write "\nDo you want to run it (y/n)? "
stdout.flushFile()
var answer = stdin.readLine()
if answer in ["y", "Y"]:
fileName.writeFile(source)
let cmd = case lang
of "go": "go run " & fileName
of "nim": "nim r -d:release --threads:on -d:ssl " & fileName
of "perl": "perl " & fileName
of "python": "python " & fileName
else: ""
discard execCmd cmd
removeFile fileName
 
stdout.write "\nDo another one (y/n)? "
stdout.flushFile()
answer = stdin.readLine()
if answer notin ["y", "Y"]:
break</lang>
 
=={{header|Raku}}==
Anonymous user