Rosetta Code/Run examples

From Rosetta Code
Rosetta Code/Run examples is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

This task is based on an idea hatched from this C1R Implementation.

Write a program that will accept as input the name of a task from Rosetta Code and the name of a language. The program should then download the solution for the specified task in the specified language, present the source to the user and prompt the user to confirm running the example.

The program should verify that the tools needed to compile or run the solution are present before running it. If the solution can not be run, a graceful exit should happen. (i.e. the program should not crash)

Besides its own language, the program should support at least two other languages. (Ideally it would support most of the languages available, but that is too much to ask. Though some languages are simple, e.g. python, pike, perl, bash and several more only need the solution saved in a file and given to the language as argument to run, so it should be easy to support many languages like that).

If you know what is needed to support a particular language, please help to add support for that language to implementations in other languages.

Extra credit: add a function to get a list of all solutions of a given language and run them to create a report on which solutions failed to run.

More credit: also test if the output of a solution compares to a given result. The expected output should be loaded from a file with the name of the task. (This way all implementations can share the same set of files, and anyone can add more files. In the future the content of these files could be stored directly in a section of the task on Rosetta Code itself.)

Run BASIC

<lang runbasic>bf$ = "" 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$(a1$,1,"")

i = 2

b$ = word$(a1$,i,"

  • <a href=""/wiki/") ' ' Create a drop down window for selection of a task ' html bf$;"
    " html "" html "
    Tasks
    Task"

    html "<select size=10 id='runProg' name='runProg'>" while b$ <> ""

     b$	= left$(b$,instr(b$,"""")-1)
     b$	= strRep$(b$,"%2B","+")
     b$	= strRep$(b$,"%27","'")
     html "<option>"+b$+"</option>"
     i 	= i + 1
    
    b$ = word$(a1$,i,"
  • <a href=""/wiki/") wend html "</select>
  • "

    ' BUTTON options to Run It or Exit

       button #run, "Run It", [runProg]
       button #ex, "Exit", [quit]
    
    html "
    " ' close the drop down table and wait

    wait

    [runProg] progName$ = #request get$("runProg") print progName$ a$ = httpGet$("http://rosettacode.org/wiki/"+progName$)

    i = instr(a$,"<a href=""#Run_BASIC"">")

    a$ = mid$(a$,i-6,6) a$ = word$(a$,2,"-") a$ = word$(a$,1,"""") cls ' clear screen 'print a$ ' this is the program number used in the [RC] editor

    a$ = httpGet$("http://rosettacode.org/mw/index.php?title="+progName$+"&action=edit&section="+a$)

    a$ = word$(a$,2,"{header|Run BASIC}") i = instr(a$,">") a$ = mid$(a$,i+1) i = instr(a$,"/lang>") a$ = left$(a$,i-5) a$ = strRep$(a$,"<","<") ' this is the good program code ' place the code in the rb$ file rb$ = DefaultDir$ + "\projects\a_project\rcCode.bas" ' RC program open rb$ for output as #f print #f,a$ close #f

    print "================== Run Basic Solution ===========================" run rb$,#handle ' point RunBasic to the file with the program render #handle ' render the runned code [quit] ' that's it folks end

    ' -------------------------------- ' string replace rep str with ' -------------------------------- FUNCTION strRep$(str$,rep$,with$) ln = len(rep$) ln1 = ln - 1 i = 1 while i <= len(str$)

       if mid$(str$,i,ln) = rep$ then
           strRep$ = strRep$ + with$
           i = i + ln1
       else
           strRep$ = strRep$ + mid$(str$,i,1)
       end if
    

    i = i + 1 WEND END FUNCTION</lang>

    Tcl

    This code only includes support for running Tcl task solutions, but it can download any language's; it assumes that the first <lang…> is sufficient when it comes to task extraction (definitely not true universally, but mostly good enough).

    Library: Tcllib (Package: uri)

    <lang tcl># Code to download task contents from find-bare-lang-tags task package require Tcl 8.5 package require http package require uri

    proc getUrlWithRedirect {base args} {

       set url $base?[http::formatQuery {*}$args]
       while 1 {
    

    set t [http::geturl $url] if {[http::status $t] ne "ok"} { error "Oops: url=$url\nstatus=$s\nhttp code=[http::code $token]" } if {[string match 2?? [http::ncode $t]]} { return $t } # OK, but not 200? Must be a redirect... set url [uri::resolve $url [dict get [http::meta $t] Location]] http::cleanup $t

       }
    

    } proc getTaskContent {task} {

       set token [getUrlWithRedirect http://rosettacode.org/mw/index.php \
    

    title $task action raw]

       set content [http::data $token]
       http::cleanup $token
       return $content
    

    }

    1. Code to extract the first <lang> section for a language

    proc getTaskCodeForLanguage {task language} {

       set content [getTaskContent $task]
       set startRE {==\s*\{\{header\|@LANG@(?:\|[^{}]+)?\}\}\s*==}
       set startRE [string map [list @LANG@ $language] $startRE]
       if {![regexp -indices $startRE $content start]} {
    

    error "$language does not implement task \"$task\""

       }
       if {![regexp -indices -start [lindex $start end] \
    

    "==\\s*\\\{\\\{header" $content end]} { set end {end end}

       }
       set content [string range $content [lindex $start 1] [lindex $end 0]]
       # Extended format RE used to allow embedding within _this_ task's <lang>!
       if {![regexp {(?x)<lang .*?>(.*?)</ lang>} $content -> solution]} {
    

    error "$language solution of task \"$task\" has no useful code"

       }
       return "$solution\n"
    

    }

    1. How to download and run a Tcl task

    proc runTclTaskForLanguage {task} {

       puts "Fetching task solution..."
       set solution [getTaskCodeForLanguage $task Tcl]
       set filename rcsoln_[string map {/ _ " " _} $task].tcl
       set f [open $filename w]
       puts $f $solution
       close $f
       puts "Executing task solution with: tclsh $filename"
       exec [info nameofexecutable] $filename <@stdin >@stdout 2>@stderr
    

    } runTclTaskForLanguage {*}$argv</lang>

    UNIX Shell

    See C1R Implementation for an incomplete implementation. (only supports C)