Rosetta Code/Run examples: Difference between revisions

m
m (→‎{{header|Raku}}: minimal changes to allow it to work with relocated site and new markup)
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by 2 users not shown)
Line 628:
continue
 
let url = "https://rosettacode.org/mww/index.php?title=" & task & "&action=edit"
let body = client.getContent(url)
var lang: string
Line 643:
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 regexheader = re(r"(?s)==\{\{header\|$#\}\}==.*?<lang $#>(.*?)</lang>".format(lang2, lang3))
let language = r"lt;syntaxhighlight lang=$#>".format(lang3)
let regex = re(header & r"==.*?&" & language & r"(.*?)</syntaxhighlight>")
var matches: array[2, string]
let idx = body.find(regex, matches)
Line 1,975 ⟶ 1,977:
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|WrenGo}}
{{libheader|Wren-set}}
{{libheader|Wren-pattern}}
{{libheader|Wren-str}}
Line 1,984:
This is designed to work for Go, Perl, Python and Wren itself though see the remarks in the Go entry about using the first three languages and other more general points.
 
As far as Wren is concerned, in addition to Wren-cli programs, this should be able to run DOME, Wren-gmp, Wren-sql, Wren-i64, Wren-linear, Wren-regex and Wren-i64psieve programs as it is easy to detect when these are being used and the filename is always given as a command line argument. All Wren modules are assumed to be present in the current working directory.
 
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="ecmascriptwren">/* rc_run_examplesRosetta_Code_Run_examples.wren */
 
import "./set" for Set
import "./pattern" for Pattern
import "./str" for Str
Line 2,017 ⟶ 2,016:
}
 
var getAllTasksp1 = FnPattern.new {("title/=\"[+1^\"]\"")
var pp2 = Pattern.new("<li><a hrefcmcontinue/=\"//wiki//[+01^\"]\"")
 
var url1 = "http://rosettacode.org/wiki/Category:Programming_Tasks"
var findTasks = Fn.new { |category|
var url2 = "http://rosettacode.org/wiki/Category:Draft_Programming_Tasks"
var url = "https://www.rosettacode.org/w/api.php?action=query&list=categorymembers&cmtitle=Category:%(category)&cmlimit=500&format=xml"
var urls = [url1, url2]
var taskscmcontinue = Set.new()""
forvar (urltasks in= urls) {[]
while (true) {
var bodycontent = Http.getBodyText(url + cmcontinue)
// find all tasks
var matchesmatches1 = pp1.findAll(bodycontent)
for (matchm in matchesmatches1) {
var title = m.capsText[0].replace("&#039;", "'").replace("&quot;", "\"")
// exclude any 'category' references
var task = matchtasks.capsText[0]add(title)
if (!task.startsWith("Category:")) tasks.add(task)
}
var m2 = p2.find(content)
if (m2) cmcontinue = "&cmcontinue=%(m2.capsText[0])" else break
}
return tasks
}
 
var tasks = getAllTasksfindTasks.call("Programming_Tasks") // 'full' tasks only
tasks.addAll(findTasks.call("Draft_Programming_Tasks"))
var langs = ["go", "perl", "python", "wren"]
while (true) {
System.write("Enter the exact name of the task : ")
var task = Stdin.readLine().trim().replace(" ", "_")
if (!tasks.contains(task)) {
System.print("Sorry a task with that name doesn't exist.")
} else {
task = task.replace(" ", "_")
var url = "https://rosettacode.org/mww/index.php?title=" + task + "&action=edit"
var page = Http.getBodyText(url).replace("&lt;", "<")
var lang
Line 2,070 ⟶ 2,072:
} else if (lang == "wren") {
lang2 = "Wren"
lang3 = "[ecmascriptwren|EcmascriptWren]"
ext = "wren"
}
var fileName = "rc_temp." + ext
var p1 = Pattern.new("/=/={{header/|%(lang2)}}/=/=")
var p2 = Pattern.new("<syntaxhighlight lang/=\"%(lang3)\">")
var p3 = Pattern.new("<//langsyntaxhighlight>")
var s = p1.split(page, 1, 0)
if (s.count > 1) s = p2.split(s[1], 1, 0)
Line 2,109 ⟶ 2,111:
} else if (preamble.contains("{{libheader|Wren-i64}}")) {
Exec.run2("./wren-i64", fileName)
} else if (preamble.contains("{{libheader|Wren-linear}}")) {
Exec.run2("./wren-linear", fileName)
} else if (preamble.contains("{{libheader|Wren-regex}}")) {
Exec.run2("./wren-regex", fileName)
} else if (preamble.contains("{{libheader|Wren-psieve}}")) {
Exec.run2("./wren-psieve", fileName)
} else { // Wren-cli
Exec.run2("wren4", fileName)
Line 2,122 ⟶ 2,130:
}</syntaxhighlight>
We now embed this script in the following Go program and run it:
<syntaxhighlight lang="go">/* go run rc_run_examplesRosetta_Code_Run_examples.go */
 
package main
Line 2,246 ⟶ 2,254:
 
module := wren.NewModule(classMap)
fileName := "rc_run_examplesRosetta_Code_Run_examples.wren"
vm.SetModule(fileName, module)
vm.InterpretFile(fileName)
9,476

edits