Modulinos: Difference between revisions
m (added whitespace before the TOC (table of contents).) |
m (→{{header|Wren}}: Minor tidy) |
||
(35 intermediate revisions by 17 users not shown) | |||
Line 5: | Line 5: | ||
Sometimes getting the [[ScriptName]] is required in order to determine when to run main(). |
Sometimes getting the [[ScriptName]] is required in order to determine when to run main(). |
||
Care when manipulating command line arguments, due to subtle exec security constraints that may or not be enforced on implicit argv[0]. https://ryiron.wordpress.com/2013/12/16/argv-silliness/ |
|||
: ''This is still a draft task, and the current task description has caused mega confusion. See '''[[Talk:Modulinos]]''' for numerous attempts to understand the task and to rewrite the task description.'' |
: ''This is still a draft task, and the current task description has caused mega confusion. See '''[[Talk:Modulinos]]''' for numerous attempts to understand the task and to rewrite the task description.'' |
||
Line 10: | Line 12: | ||
: '''The task [[Executable library]] is written to replace this task.''' ''This task's future is in doubt as its aims are not clear enough.'' |
: '''The task [[Executable library]] is written to replace this task.''' ''This task's future is in doubt as its aims are not clear enough.'' |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
<syntaxhighlight lang="11l">// life.11l |
|||
F meaning_of_life() |
|||
R ‘*’.code |
|||
:start: |
|||
print(‘Main: The meaning of life is ’meaning_of_life())</syntaxhighlight> |
|||
<syntaxhighlight lang="11l">// death.11l |
|||
print(‘Life means ’life:meaning_of_life()‘.’) |
|||
print(‘Death means nothing.’)</syntaxhighlight> |
|||
=={{header|AppleScript}}== |
|||
AppleScript's equivalent of a main() function is a <tt>run</tt> handler, which can be either implicit or explicit: |
|||
<syntaxhighlight lang="applescript">display dialog "Hello"</syntaxhighlight> |
|||
or |
|||
<syntaxhighlight lang="applescript">on run |
|||
display dialog "Hello" |
|||
end run</syntaxhighlight> |
|||
A <tt>run</tt> handler's only executed when the script containing it is explicity ''run'', either from another script or application or as an application in its own right. It's not executed when a script's simply loaded as a library, although it can subsequently be so in the unlikely event of this being desirable. Scripts saved as applications aren't recognised by the "Libraries" system introduced in Mac OS X 10.9, but can be loaded and/or run using the older <tt>load script</tt> and <tt>run script</tt> commands. Script code can tell if it's running in its own application or being executed by an external agent by comparing its file path with that of the agent: |
|||
<syntaxhighlight lang="applescript">on run |
|||
if ((path to me) = (path to current application)) then |
|||
display dialog "I'm running in my own application." |
|||
else |
|||
display dialog "I'm being run from another script or application." |
|||
end if |
|||
end run</syntaxhighlight> |
|||
=={{header|Arturo}}== |
|||
===Library=== |
|||
<syntaxhighlight lang="rebol">; modulinos - library |
|||
meaningOfLife: function [][ |
|||
42 |
|||
] |
|||
if standalone? -> |
|||
print ~"Library: The meaning of life is |meaningOfLife|"</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Library: The meaning of life is 42</pre> |
|||
===Main=== |
|||
<syntaxhighlight lang="rebol">do.import relative "modulinos - library.art" |
|||
print ~"Life means |meaningOfLife|." |
|||
print "Death means invisible scary skeletons."</syntaxhighlight> |
|||
{{out}} |
|||
<pre>Life means 42. |
|||
Death means invisible scary skeletons.</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
Line 19: | Line 84: | ||
Example |
Example |
||
< |
<syntaxhighlight lang="sh">$ make |
||
./scriptedmain |
./scriptedmain |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
./test |
./test |
||
Test: The meaning of life is</ |
Test: The meaning of life is</syntaxhighlight> |
||
Makefile |
Makefile |
||
< |
<syntaxhighlight lang="make">all: scriptedmain test |
||
./scriptedmain |
./scriptedmain |
||
./test |
./test |
||
Line 41: | Line 106: | ||
-rm test |
-rm test |
||
-rm scriptedmain.exe |
-rm scriptedmain.exe |
||
-rm test.exe</ |
-rm test.exe</syntaxhighlight> |
||
scriptedmain.h |
scriptedmain.h |
||
<lang |
<syntaxhighlight lang="c">int meaning_of_life();</syntaxhighlight> |
||
scriptedmain.c |
scriptedmain.c |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
int meaning_of_life() { |
int meaning_of_life() { |
||
Line 63: | Line 128: | ||
} |
} |
||
#endif</ |
#endif</syntaxhighlight> |
||
test.c |
test.c |
||
< |
<syntaxhighlight lang="c">#include "scriptedmain.h" |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 75: | Line 140: | ||
printf("Test: The meaning of life is %d\n", meaning_of_life()); |
printf("Test: The meaning of life is %d\n", meaning_of_life()); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
Line 82: | Line 147: | ||
Example |
Example |
||
< |
<syntaxhighlight lang="sh">$ make |
||
./scriptedmain |
./scriptedmain |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
./test |
./test |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
Makefile |
Makefile |
||
< |
<syntaxhighlight lang="make">all: scriptedmain test |
||
./scriptedmain |
./scriptedmain |
||
./test |
./test |
||
Line 104: | Line 169: | ||
-rm test |
-rm test |
||
-rm scriptedmain.exe |
-rm scriptedmain.exe |
||
-rm test.exe</ |
-rm test.exe</syntaxhighlight> |
||
scriptedmain.h |
scriptedmain.h |
||
<lang |
<syntaxhighlight lang="cpp">int meaning_of_life();</syntaxhighlight> |
||
scriptedmain.cpp |
scriptedmain.cpp |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
using namespace std; |
using namespace std; |
||
Line 127: | Line 192: | ||
} |
} |
||
#endif</ |
#endif</syntaxhighlight> |
||
test.cpp |
test.cpp |
||
< |
<syntaxhighlight lang="cpp">#include "scriptedmain.h" |
||
#include <iostream> |
#include <iostream> |
||
Line 141: | Line 206: | ||
cout << "Test: The meaning of life is " << meaning_of_life() << endl; |
cout << "Test: The meaning of life is " << meaning_of_life() << endl; |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 147: | Line 212: | ||
scriptedmain.clj: |
scriptedmain.clj: |
||
< |
<syntaxhighlight lang="clojure">":";exec lein exec $0 ${1+"$@"} |
||
":";exit |
":";exit |
||
Line 159: | Line 224: | ||
(when (.contains (first *command-line-args*) *source-path*) |
(when (.contains (first *command-line-args*) *source-path*) |
||
(apply -main (rest *command-line-args*)))</ |
(apply -main (rest *command-line-args*)))</syntaxhighlight> |
||
test.clj: |
test.clj: |
||
< |
<syntaxhighlight lang="clojure">":";exec lein exec $0 ${1+"$@"} |
||
":";exit |
":";exit |
||
Line 174: | Line 239: | ||
(when (.contains (first *command-line-args*) *source-path*) |
(when (.contains (first *command-line-args*) *source-path*) |
||
(apply -main (rest *command-line-args*)))</ |
(apply -main (rest *command-line-args*)))</syntaxhighlight> |
||
=={{header|CoffeeScript}}== |
=={{header|CoffeeScript}}== |
||
scriptedmain.coffee: |
scriptedmain.coffee: |
||
< |
<syntaxhighlight lang="coffeescript">#!/usr/bin/env coffee |
||
meaningOfLife = () -> 42 |
meaningOfLife = () -> 42 |
||
Line 187: | Line 252: | ||
console.log "Main: The meaning of life is " + meaningOfLife() |
console.log "Main: The meaning of life is " + meaningOfLife() |
||
if not module.parent then main()</ |
if not module.parent then main()</syntaxhighlight> |
||
test.coffee: |
test.coffee: |
||
< |
<syntaxhighlight lang="coffeescript">#!/usr/bin/env coffee |
||
sm = require "./scriptedmain" |
sm = require "./scriptedmain" |
||
console.log "Test: The meaning of life is " + sm.meaningOfLife()</ |
console.log "Test: The meaning of life is " + sm.meaningOfLife()</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 203: | Line 268: | ||
~/.clisprc.lisp |
~/.clisprc.lisp |
||
< |
<syntaxhighlight lang="lisp">;;; Play nice with shebangs |
||
(set-dispatch-macro-character #\# #\! |
(set-dispatch-macro-character #\# #\! |
||
(lambda (stream character n) |
(lambda (stream character n) |
||
(declare (ignore character n)) |
(declare (ignore character n)) |
||
(read-line stream nil nil t) |
(read-line stream nil nil t) |
||
nil))</ |
nil))</syntaxhighlight> |
||
scriptedmain.lisp |
scriptedmain.lisp |
||
< |
<syntaxhighlight lang="lisp">#!/bin/sh |
||
#| |
#| |
||
exec clisp -q -q $0 $0 ${1+"$@"} |
exec clisp -q -q $0 $0 ${1+"$@"} |
||
Line 242: | Line 307: | ||
args |
args |
||
:test #'(lambda (x y) (search x y :test #'equalp))) |
:test #'(lambda (x y) (search x y :test #'equalp))) |
||
(main args)))</ |
(main args)))</syntaxhighlight> |
||
test.lisp |
test.lisp |
||
< |
<syntaxhighlight lang="lisp">#!/bin/sh |
||
#| |
#| |
||
exec clisp -q -q $0 $0 ${1+"$@"} |
exec clisp -q -q $0 $0 ${1+"$@"} |
||
Line 253: | Line 318: | ||
(load "scriptedmain.lisp") |
(load "scriptedmain.lisp") |
||
(format t "Test: The meaning of life is ~a~%" (meaning-of-life))</ |
(format t "Test: The meaning of life is ~a~%" (meaning-of-life))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
Line 261: | Line 326: | ||
scriptedmain.d: |
scriptedmain.d: |
||
< |
<syntaxhighlight lang="d">#!/usr/bin/env rdmd -version=scriptedmain |
||
module scriptedmain; |
module scriptedmain; |
||
Line 275: | Line 340: | ||
writeln("Main: The meaning of life is ", meaningOfLife()); |
writeln("Main: The meaning of life is ", meaningOfLife()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
test.d: |
test.d: |
||
< |
<syntaxhighlight lang="d">#!/usr/bin/env rdmd -version=test |
||
import scriptedmain; |
import scriptedmain; |
||
Line 288: | Line 353: | ||
writeln("Test: The meaning of life is ", meaningOfLife()); |
writeln("Test: The meaning of life is ", meaningOfLife()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ ./scriptedmain.d |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ ./test.d |
$ ./test.d |
||
Line 301: | Line 366: | ||
$ dmd test.d scriptedmain.d -version=test |
$ dmd test.d scriptedmain.d -version=test |
||
$ ./test |
$ ./test |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|Dart}}== |
=={{header|Dart}}== |
||
scriptedmain.dart: |
scriptedmain.dart: |
||
< |
<syntaxhighlight lang="dart">#!/usr/bin/env dart |
||
#library("scriptedmain"); |
#library("scriptedmain"); |
||
Line 315: | Line 380: | ||
main() { |
main() { |
||
print("Main: The meaning of life is ${meaningOfLife()}"); |
print("Main: The meaning of life is ${meaningOfLife()}"); |
||
}</ |
}</syntaxhighlight> |
||
test.dart: |
test.dart: |
||
< |
<syntaxhighlight lang="dart">#!/usr/bin/env dart |
||
#import("scriptedmain.dart", prefix: "scriptedmain"); |
#import("scriptedmain.dart", prefix: "scriptedmain"); |
||
Line 324: | Line 389: | ||
main() { |
main() { |
||
print("Test: The meaning of life is ${scriptedmain.meaningOfLife()}"); |
print("Test: The meaning of life is ${scriptedmain.meaningOfLife()}"); |
||
}</ |
}</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ ./scriptedmain.dart |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ ./test.dart |
$ ./test.dart |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|Emacs Lisp}}== |
=={{header|Emacs Lisp}}== |
||
Line 337: | Line 402: | ||
scriptedmain.el |
scriptedmain.el |
||
< |
<syntaxhighlight lang="lisp">:;exec emacs -batch -l $0 -f main $* |
||
;;; Shebang from John Swaby |
;;; Shebang from John Swaby |
||
Line 345: | Line 410: | ||
(defun main () |
(defun main () |
||
(message "Main: The meaning of life is %d" (meaning-of-life)))</ |
(message "Main: The meaning of life is %d" (meaning-of-life)))</syntaxhighlight> |
||
test.el |
test.el |
||
< |
<syntaxhighlight lang="lisp">:;exec emacs -batch -l $0 -f main $* |
||
;;; Shebang from John Swaby |
;;; Shebang from John Swaby |
||
Line 357: | Line 422: | ||
(setq load-path (cons default-directory load-path)) |
(setq load-path (cons default-directory load-path)) |
||
(load "scriptedmain.el" nil t) |
(load "scriptedmain.el" nil t) |
||
(message "Test: The meaning of life is %d" (meaning-of-life)))</ |
(message "Test: The meaning of life is %d" (meaning-of-life)))</syntaxhighlight> |
||
=={{header|EMal}}== |
|||
{{trans|Wren}} |
|||
<syntaxhighlight lang="emal"> |
|||
^|We have created a module named ModulinosPart.emal. |
|||
|^ |
|||
in Org:RosettaCode |
|||
type ModulinosPart |
|||
fun meaningOfLife = int by block do return 42 end |
|||
fun main = void by block do writeLine("The meaning of life is " + meaningOfLife() + ".") end |
|||
if Runtime.direct() do main() end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
emal.exe Org\RosettaCode\ModulinosPart.emal |
|||
The meaning of life is 42. |
|||
</pre> |
|||
<syntaxhighlight lang="emal"> |
|||
^|Then we create a new module named Modulinos.emal, |
|||
|this imports the previous module. |
|||
|^ |
|||
in Org:RosettaCode |
|||
load :ModulinosPart |
|||
type Modulinos |
|||
fun main = int by List args |
|||
writeLine("Who says the meaning of life is " + ModulinosPart.meaningOfLife() + "?") |
|||
return 0 |
|||
end |
|||
exit main(Runtime.args) |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
emal.exe Org\RosettaCode\Modulinos.emal |
|||
Who says the meaning of life is 42? |
|||
</pre> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
Line 363: | Line 463: | ||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">all: t |
||
t: scriptedmain.beam test.beam |
t: scriptedmain.beam test.beam |
||
Line 376: | Line 476: | ||
clean: |
clean: |
||
-rm *.beam</ |
-rm *.beam</syntaxhighlight> |
||
scriptedmain.erl: |
scriptedmain.erl: |
||
< |
<syntaxhighlight lang="erlang">-module(scriptedmain). |
||
-export([meaning_of_life/0, start/0]). |
-export([meaning_of_life/0, start/0]). |
||
Line 386: | Line 486: | ||
start() -> |
start() -> |
||
io:format("Main: The meaning of life is ~w~n", [meaning_of_life()]), |
io:format("Main: The meaning of life is ~w~n", [meaning_of_life()]), |
||
init:stop().</ |
init:stop().</syntaxhighlight> |
||
test.erl: |
test.erl: |
||
< |
<syntaxhighlight lang="erlang">-module(test). |
||
-export([start/0]). |
-export([start/0]). |
||
-import(scriptedmain, [meaning_of_life/0]). |
-import(scriptedmain, [meaning_of_life/0]). |
||
Line 395: | Line 495: | ||
start() -> |
start() -> |
||
io:format("Test: The meaning of life is ~w~n", [meaning_of_life()]), |
io:format("Test: The meaning of life is ~w~n", [meaning_of_life()]), |
||
init:stop().</ |
init:stop().</syntaxhighlight> |
||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
Line 409: | Line 509: | ||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ make |
||
fsharpc --out:scriptedmain.exe ScriptedMain.fs |
fsharpc --out:scriptedmain.exe ScriptedMain.fs |
||
fsharpc --out:test.exe ScriptedMain.fs Test.fs |
fsharpc --out:test.exe ScriptedMain.fs Test.fs |
||
Line 415: | Line 515: | ||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ mono test.exe |
$ mono test.exe |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">all: scriptedmain.exe test.exe |
||
scriptedmain.exe: ScriptedMain.fs |
scriptedmain.exe: ScriptedMain.fs |
||
Line 428: | Line 528: | ||
clean: |
clean: |
||
-rm *.exe</ |
-rm *.exe</syntaxhighlight> |
||
ScriptedMain.fs: |
ScriptedMain.fs: |
||
< |
<syntaxhighlight lang="fsharp">namespace ScriptedMain |
||
module ScriptedMain = |
module ScriptedMain = |
||
Line 438: | Line 538: | ||
let main = |
let main = |
||
printfn "Main: The meaning of life is %d" meaningOfLife</ |
printfn "Main: The meaning of life is %d" meaningOfLife</syntaxhighlight> |
||
Test.fs: |
Test.fs: |
||
< |
<syntaxhighlight lang="fsharp">module Test = |
||
open ScriptedMain |
open ScriptedMain |
||
let main = |
let main = |
||
printfn "Test: The meaning of life is %d" ScriptedMain.meaningOfLife</ |
printfn "Test: The meaning of life is %d" ScriptedMain.meaningOfLife</syntaxhighlight> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 453: | Line 553: | ||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ ./scriptedmain.factor |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ ./test.factor |
$ ./test.factor |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
~/.factor-rc: |
~/.factor-rc: |
||
< |
<syntaxhighlight lang="factor">! INCLUDING macro that imports source code files in the current directory |
||
USING: kernel vocabs.loader parser sequences lexer vocabs.parser ; |
USING: kernel vocabs.loader parser sequences lexer vocabs.parser ; |
||
Line 467: | Line 567: | ||
: include-vocab ( vocab -- ) dup ".factor" append parse-file append use-vocab ; |
: include-vocab ( vocab -- ) dup ".factor" append parse-file append use-vocab ; |
||
SYNTAX: INCLUDING: ";" [ include-vocab ] each-token ;</ |
SYNTAX: INCLUDING: ";" [ include-vocab ] each-token ;</syntaxhighlight> |
||
scriptedmain.factor: |
scriptedmain.factor: |
||
< |
<syntaxhighlight lang="factor">#! /usr/bin/env factor |
||
USING: io math.parser ; |
USING: io math.parser ; |
||
Line 480: | Line 580: | ||
: main ( -- ) meaning-of-life "Main: The meaning of life is " write number>string print ; |
: main ( -- ) meaning-of-life "Main: The meaning of life is " write number>string print ; |
||
MAIN: main</ |
MAIN: main</syntaxhighlight> |
||
test.factor: |
test.factor: |
||
< |
<syntaxhighlight lang="factor">#! /usr/bin/env factor |
||
INCLUDING: scriptedmain ; |
INCLUDING: scriptedmain ; |
||
Line 492: | Line 592: | ||
: main ( -- ) meaning-of-life "Test: The meaning of life is " write number>string print ; |
: main ( -- ) meaning-of-life "Test: The meaning of life is " write number>string print ; |
||
MAIN: main</ |
MAIN: main</syntaxhighlight> |
||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
Line 498: | Line 598: | ||
Given this awful running reference: |
Given this awful running reference: |
||
< |
<syntaxhighlight lang="forth">42 constant Douglas-Adams |
||
: go ( -- ) |
: go ( -- ) |
||
." The meaning of life is " Douglas-Adams . cr ;</ |
." The meaning of life is " Douglas-Adams . cr ;</syntaxhighlight> |
||
The bulk of Forth systems provide a way to generate an executable that enters GO (ar any word) on start. |
The bulk of Forth systems provide a way to generate an executable that enters GO (ar any word) on start. |
||
Line 507: | Line 607: | ||
{{works with|SwiftForth|SwiftForth|4.0}} |
{{works with|SwiftForth|SwiftForth|4.0}} |
||
< |
<syntaxhighlight lang="forth">' go 'MAIN ! |
||
program douglas-adams</ |
program douglas-adams</syntaxhighlight> |
||
Which creates a file named 'douglas-adams' that you can then run. If this is all in the same file, you can load the file, test parts of it, and then exit (or shell out) to run the executable. |
Which creates a file named 'douglas-adams' that you can then run. If this is all in the same file, you can load the file, test parts of it, and then exit (or shell out) to run the executable. |
||
Line 516: | Line 616: | ||
{{works with|gforth}} |
{{works with|gforth}} |
||
< |
<syntaxhighlight lang="forth">#! /usr/bin/env gforth |
||
42 constant Douglas-Adams |
42 constant Douglas-Adams |
||
.( The meaning of life is ) Douglas-Adams . cr bye</ |
.( The meaning of life is ) Douglas-Adams . cr bye</syntaxhighlight> |
||
Adding #! as a comment, as gforth does, is trivial. For a means by which this script could distinguish between 'scripted execution' and otherwise, a symlink like 'forthscript' could easily be used, and the zeroth OS argument tested for, but there's no convention. |
Adding #! as a comment, as gforth does, is trivial. For a means by which this script could distinguish between 'scripted execution' and otherwise, a symlink like 'forthscript' could easily be used, and the zeroth OS argument tested for, but there's no convention. |
||
Line 525: | Line 625: | ||
{{works with|gforth}} |
{{works with|gforth}} |
||
< |
<syntaxhighlight lang="forth">#! /usr/bin/env forthscript |
||
42 constant Douglas-Adams |
42 constant Douglas-Adams |
||
Line 533: | Line 633: | ||
[THEN] |
[THEN] |
||
cr .( Why aren't you running this as a script? It only provides a constant.)</ |
cr .( Why aren't you running this as a script? It only provides a constant.)</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
|||
{{trans|Ring}} |
|||
<syntaxhighlight lang="freebasic"> |
|||
Function meaningoflife() As Byte |
|||
Dim As Byte y = 42 |
|||
Return y |
|||
End Function |
|||
Sub main() |
|||
Print "Main: The meaning of life is "; meaningoflife() |
|||
End Sub |
|||
main() |
|||
Sleep |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Main: The meaning of life is 42 |
|||
</pre> |
|||
=={{header|Go}}== |
|||
Go doesn't support scripted main directly. |
|||
Although the [https://github.com/mcandre/modulinos examples] linked to above include an example for Go, this is only a work around, not an emulation. To emulate a modulino, we need to proceed as in the [[https://rosettacode.org/wiki/Executable_library#Go Executable library]] task and split the 'main' package into two. |
|||
First create these two files in the 'modulino' directory: |
|||
<syntaxhighlight lang="go">// modulino.go |
|||
package main |
|||
import "fmt" |
|||
func MeaningOfLife() int { |
|||
return 42 |
|||
} |
|||
func libMain() { |
|||
fmt.Println("The meaning of life is", MeaningOfLife()) |
|||
}</syntaxhighlight> |
|||
<syntaxhighlight lang="go">// modulino_main.go |
|||
package main |
|||
func main() { |
|||
libMain() |
|||
}</syntaxhighlight> |
|||
To emulate a modulino: |
|||
{{out}} |
|||
<pre> |
|||
$ go run modulino |
|||
The meaning of life is 42 |
|||
</pre> |
|||
Now create this file in the 'mol' directory: |
|||
<syntaxhighlight lang="go">// mol.go |
|||
package main |
|||
import "fmt" |
|||
func main() { |
|||
fmt.Println("The meaning of life is still", MeaningOfLife()) |
|||
}</syntaxhighlight> |
|||
and copy modulino.go to the 'mol' directory. The library can then be used in the 'normal' way: |
|||
{{out}} |
|||
<pre> |
|||
$ go run mol |
|||
The meaning of life is still 42 |
|||
</pre> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Line 539: | Line 712: | ||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ ./ScriptedMain.groovy |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ ./Test.groovy |
$ ./Test.groovy |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
ScriptedMain.groovy: |
ScriptedMain.groovy: |
||
< |
<syntaxhighlight lang="groovy">#!/usr/bin/env groovy |
||
class ScriptedMain { |
class ScriptedMain { |
||
Line 554: | Line 727: | ||
println "Main: The meaning of life is " + meaningOfLife |
println "Main: The meaning of life is " + meaningOfLife |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Test.groovy: |
Test.groovy: |
||
< |
<syntaxhighlight lang="groovy">#!/usr/bin/env groovy |
||
println "Test: The meaning of life is " + ScriptedMain.meaningOfLife</ |
println "Test: The meaning of life is " + ScriptedMain.meaningOfLife</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Haskell has scripted main, but getting scripted main to work with compiled scripts is tricky. |
Haskell has scripted main, but getting scripted main to work with compiled scripts is tricky. |
||
< |
<syntaxhighlight lang="sh">$ runhaskell scriptedmain.hs |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ runhaskell test.hs |
$ runhaskell test.hs |
||
Line 574: | Line 747: | ||
$ ghc -fforce-recomp -o test -main-is Test test.hs scriptedmain.hs |
$ ghc -fforce-recomp -o test -main-is Test test.hs scriptedmain.hs |
||
$ ./test |
$ ./test |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
scriptedmain.hs |
scriptedmain.hs |
||
< |
<syntaxhighlight lang="haskell">#!/usr/bin/env runhaskell |
||
-- Compile: |
-- Compile: |
||
Line 590: | Line 763: | ||
main :: IO () |
main :: IO () |
||
main = putStrLn $ "Main: The meaning of life is " ++ show meaningOfLife</ |
main = putStrLn $ "Main: The meaning of life is " ++ show meaningOfLife</syntaxhighlight> |
||
test.hs |
test.hs |
||
< |
<syntaxhighlight lang="haskell">#!/usr/bin/env runhaskell |
||
-- Compile: |
-- Compile: |
||
Line 605: | Line 778: | ||
main :: IO () |
main :: IO () |
||
main = putStrLn $ "Test: The meaning of life is " ++ show meaningOfLife</ |
main = putStrLn $ "Test: The meaning of life is " ++ show meaningOfLife</syntaxhighlight> |
||
=={{header|Io}}== |
=={{header|Io}}== |
||
Line 611: | Line 784: | ||
ScriptedMain.io: |
ScriptedMain.io: |
||
< |
<syntaxhighlight lang="io">#!/usr/bin/env io |
||
ScriptedMain := Object clone |
ScriptedMain := Object clone |
||
Line 618: | Line 791: | ||
if( isLaunchScript, |
if( isLaunchScript, |
||
"Main: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println |
"Main: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println |
||
)</ |
)</syntaxhighlight> |
||
test.io: |
test.io: |
||
< |
<syntaxhighlight lang="io">#!/usr/bin/env io |
||
"Test: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println</ |
"Test: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println</syntaxhighlight> |
||
< |
<syntaxhighlight lang="sh">$ ./ScriptedMain.io |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ ./test.io |
$ ./test.io |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 635: | Line 808: | ||
modulinos.ijs: |
modulinos.ijs: |
||
< |
<syntaxhighlight lang="j">#!/usr/bin/env ijconsole |
||
meaningOfLife =: 42 |
meaningOfLife =: 42 |
||
Line 650: | Line 823: | ||
) |
) |
||
shouldrun 0</ |
shouldrun 0</syntaxhighlight> |
||
test.j: |
test.j: |
||
< |
<syntaxhighlight lang="j">#!/usr/bin/env jconsole |
||
load 'modulinos.ijs' |
load 'modulinos.ijs' |
||
Line 660: | Line 833: | ||
echo 'Test: The meaning of life is ',": meaningOfLife |
echo 'Test: The meaning of life is ',": meaningOfLife |
||
exit ''</ |
exit ''</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ ./modulinos.ijs |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ ./test.j |
$ ./test.j |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Line 674: | Line 847: | ||
ScriptedMain.java |
ScriptedMain.java |
||
< |
<syntaxhighlight lang="java">public class ScriptedMain { |
||
public static int meaningOfLife() { |
public static int meaningOfLife() { |
||
return 42; |
return 42; |
||
Line 682: | Line 855: | ||
System.out.println("Main: The meaning of life is " + meaningOfLife()); |
System.out.println("Main: The meaning of life is " + meaningOfLife()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Test.java |
Test.java |
||
< |
<syntaxhighlight lang="java">public class Test { |
||
public static void main(String[] args) { |
public static void main(String[] args) { |
||
System.out.println("Test: The meaning of life is " + ScriptedMain.meaningOfLife()); |
System.out.println("Test: The meaning of life is " + ScriptedMain.meaningOfLife()); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 697: | Line 870: | ||
scriptedmain.js |
scriptedmain.js |
||
< |
<syntaxhighlight lang="javascript">#!/usr/bin/env node |
||
function meaningOfLife() { return 42; } |
function meaningOfLife() { return 42; } |
||
Line 707: | Line 880: | ||
} |
} |
||
if (!module.parent) { main(); }</ |
if (!module.parent) { main(); }</syntaxhighlight> |
||
test.js |
test.js |
||
< |
<syntaxhighlight lang="javascript">#!/usr/bin/env node |
||
var sm = require("./scriptedmain"); |
var sm = require("./scriptedmain"); |
||
console.log("Test: The meaning of life is " + sm.meaningOfLife());</ |
console.log("Test: The meaning of life is " + sm.meaningOfLife());</syntaxhighlight> |
||
=={{header|Julia}}== |
|||
Julia does not use scripted main by default, but can be set to run as such. Modules generally use a /test unit test subdirectory instead. |
|||
<br /> |
|||
In module file Divisors.jl: |
|||
<syntaxhighlight lang="julia">module Divisors |
|||
using Primes |
|||
export properdivisors |
|||
function properdivisors(n::T) where T <: Integer |
|||
0 < n || throw(ArgumentError("number to be factored must be ≥ 0, got $n")) |
|||
1 < n || return T[] |
|||
!isprime(n) || return T[one(T), n] |
|||
f = factor(n) |
|||
d = T[one(T)] |
|||
for (k, v) in f |
|||
c = T[k^i for i in 0:v] |
|||
d = d*c' |
|||
d = reshape(d, length(d)) |
|||
end |
|||
sort!(d) |
|||
return d[1:end-1] |
|||
end |
|||
function interactiveDivisors() |
|||
println("\nFind proper divisors between two numbers.\nFirst number: ") |
|||
lo = (x = tryparse(Int64, readline())) == nothing ? 0 : x |
|||
println("\nSecond number: ") |
|||
hi = (x = tryparse(Int64, readline())) == nothing ? 10 : x |
|||
lo, hi = lo > hi ? (hi, lo) : (lo, hi) |
|||
println("Listing the proper divisors for $lo through $hi.") |
|||
for i in lo:hi |
|||
println(lpad(i, 7), " => ", rpad(properdivisors(i), 10)) |
|||
end |
|||
end |
|||
end |
|||
# some testing code |
|||
if occursin(r"divisors.jl"i, Base.PROGRAM_FILE) |
|||
println("This module is running as main.\n") |
|||
Divisors.interactiveDivisors() |
|||
end |
|||
</syntaxhighlight> |
|||
In a user file getdivisors.jl: |
|||
<syntaxhighlight lang="julia">include("divisors.jl") |
|||
using .Divisors |
|||
n = 708245926330 |
|||
println("The proper divisors of $n are ", properdivisors(n)) |
|||
</syntaxhighlight> |
|||
=={{header|LLVM}}== |
=={{header|LLVM}}== |
||
LLVM can have scripted main a la C, using the weak attribute. |
LLVM can have scripted main a la C, using the weak attribute. |
||
< |
<syntaxhighlight lang="sh">$ make |
||
llvm-as scriptedmain.ll |
llvm-as scriptedmain.ll |
||
llc scriptedmain.bc |
llc scriptedmain.bc |
||
Line 729: | Line 957: | ||
gcc -o test test.s scriptedmain.s |
gcc -o test test.s scriptedmain.s |
||
./test |
./test |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
Makefile |
Makefile |
||
< |
<syntaxhighlight lang="make">EXECUTABLE_SM=scriptedmain |
||
EXECUTABLE_TEST=test |
EXECUTABLE_TEST=test |
||
Line 754: | Line 982: | ||
-rm test.bc |
-rm test.bc |
||
-rm scriptedmain.s |
-rm scriptedmain.s |
||
-rm scriptedmain.bc</ |
-rm scriptedmain.bc</syntaxhighlight> |
||
scriptedmain.ll |
scriptedmain.ll |
||
< |
<syntaxhighlight lang="llvm">@msg_main = internal constant [33 x i8] c"Main: The meaning of life is %d\0A\00" |
||
declare i32 @printf(i8* noalias nocapture, ...) |
declare i32 @printf(i8* noalias nocapture, ...) |
||
Line 772: | Line 1,000: | ||
ret i32 0 |
ret i32 0 |
||
}</ |
}</syntaxhighlight> |
||
test.ll |
test.ll |
||
< |
<syntaxhighlight lang="llvm">@msg_test = internal constant [33 x i8] c"Test: The meaning of life is %d\0A\00" |
||
declare i32 @printf(i8* noalias nocapture, ...) |
declare i32 @printf(i8* noalias nocapture, ...) |
||
Line 788: | Line 1,016: | ||
ret i32 0 |
ret i32 0 |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 795: | Line 1,023: | ||
scriptedmain.lua |
scriptedmain.lua |
||
< |
<syntaxhighlight lang="lua">#!/usr/bin/env lua |
||
function meaningoflife() |
function meaningoflife() |
||
Line 809: | Line 1,037: | ||
else |
else |
||
module(..., package.seeall) |
module(..., package.seeall) |
||
end</ |
end</syntaxhighlight> |
||
test.lua |
test.lua |
||
< |
<syntaxhighlight lang="lua">#!/usr/bin/env lua |
||
sm = require("scriptedmain") |
sm = require("scriptedmain") |
||
print("Test: The meaning of life is " .. sm.meaningoflife())</ |
print("Test: The meaning of life is " .. sm.meaningoflife())</syntaxhighlight> |
||
=={{header|Make}}== |
=={{header|Make}}== |
||
Example |
Example |
||
< |
<syntaxhighlight lang="sh">$ make -f scriptedmain.mf |
||
The meaning of life is 42 |
The meaning of life is 42 |
||
(Main) |
(Main) |
||
$ make -f test.mf |
$ make -f test.mf |
||
The meaning of life is 42 |
The meaning of life is 42 |
||
(Test)</ |
(Test)</syntaxhighlight> |
||
scriptedmain.mf |
scriptedmain.mf |
||
< |
<syntaxhighlight lang="make">all: scriptedmain |
||
meaning-of-life: |
meaning-of-life: |
||
Line 835: | Line 1,063: | ||
scriptedmain: meaning-of-life |
scriptedmain: meaning-of-life |
||
@echo "(Main)" |
@echo "(Main)" |
||
</syntaxhighlight> |
|||
</lang> |
|||
test.mf |
test.mf |
||
< |
<syntaxhighlight lang="make">all: test |
||
test: |
test: |
||
@make -f scriptedmain.mf meaning-of-life |
@make -f scriptedmain.mf meaning-of-life |
||
@echo "(Test)" |
@echo "(Test)" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
scriptedmain.ma |
scriptedmain.ma |
||
< |
<syntaxhighlight lang="mathematica">#!/usr/bin/env MathKernel -script |
||
MeaningOfLife[] = 42 |
MeaningOfLife[] = 42 |
||
Line 863: | Line 1,091: | ||
If[StringMatchQ[Program, ".*scriptedmain.*"], |
If[StringMatchQ[Program, ".*scriptedmain.*"], |
||
Print["Main: The meaning of life is " <> ToString[MeaningOfLife[]]] |
Print["Main: The meaning of life is " <> ToString[MeaningOfLife[]]] |
||
]</ |
]</syntaxhighlight> |
||
test.ma: |
test.ma: |
||
< |
<syntaxhighlight lang="mathematica">#!/usr/bin/env MathKernel -script |
||
Get["scriptedmain.ma"] |
Get["scriptedmain.ma"] |
||
Print["Test: The meaning of life is " <> ToString[MeaningOfLife[]]]</ |
Print["Test: The meaning of life is " <> ToString[MeaningOfLife[]]]</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ ./scriptedmain.ma |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ ./test.ma |
$ ./test.ma |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
In Mac and Windows, the output will be surrounded by spurious quotes. |
In Mac and Windows, the output will be surrounded by spurious quotes. |
||
Line 882: | Line 1,110: | ||
=={{header|Mozart/Oz}}== |
=={{header|Mozart/Oz}}== |
||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">all: run |
||
run: scriptedmain test |
run: scriptedmain test |
||
Line 902: | Line 1,130: | ||
-rm *.ozf |
-rm *.ozf |
||
-rm *.exe |
-rm *.exe |
||
</syntaxhighlight> |
|||
</lang> |
|||
scriptedmain.oz: |
scriptedmain.oz: |
||
< |
<syntaxhighlight lang="oz">functor |
||
export |
export |
||
meaningOfLife: MeaningOfLife |
meaningOfLife: MeaningOfLife |
||
Line 923: | Line 1,151: | ||
end |
end |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
test.oz: |
test.oz: |
||
< |
<syntaxhighlight lang="oz">functor |
||
import |
import |
||
ScriptedMain |
ScriptedMain |
||
Line 940: | Line 1,168: | ||
end |
end |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|newLISP}}== |
=={{header|newLISP}}== |
||
Line 947: | Line 1,175: | ||
scriptedmain.lsp |
scriptedmain.lsp |
||
< |
<syntaxhighlight lang="lisp">#!/usr/bin/env newlisp |
||
(context 'SM) |
(context 'SM) |
||
Line 959: | Line 1,187: | ||
(if (find "scriptedmain" (main-args 1)) (main)) |
(if (find "scriptedmain" (main-args 1)) (main)) |
||
(context MAIN)</ |
(context MAIN)</syntaxhighlight> |
||
test.lsp |
test.lsp |
||
< |
<syntaxhighlight lang="lisp">#!/usr/bin/env newlisp |
||
(load "scriptedmain.lsp") |
(load "scriptedmain.lsp") |
||
(println (format "Test: The meaning of life is %d" (SM:meaning-of-life))) |
(println (format "Test: The meaning of life is %d" (SM:meaning-of-life))) |
||
(exit)</ |
(exit)</syntaxhighlight> |
||
=={{header|Nim}}== |
|||
Nim provides the predicate <code>isMainModule</code> to use with conditional compilation. Here is an example: |
|||
<syntaxhighlight lang="Nim">proc p*() = |
|||
## Some exported procedure. |
|||
echo "Executing procedure" |
|||
# Some code to execute to initialize the module. |
|||
echo "Initializing the module" |
|||
when isMainModule: |
|||
# Some code to execute if the module is run directly, for instance code to test the module. |
|||
echo "Running tests" |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
When run directly, the result of execution is: |
|||
<pre>Initializing the module |
|||
Running tests |
|||
</pre> |
|||
If we call “p” from another module, we get: |
|||
<pre>Initializing the module |
|||
Executing procedure |
|||
</pre> |
|||
=={{header|Objective-C}}== |
=={{header|Objective-C}}== |
||
Line 973: | Line 1,226: | ||
scriptedmain.h: |
scriptedmain.h: |
||
< |
<syntaxhighlight lang="objc">#import <objc/Object.h> |
||
@interface ScriptedMain: Object {} |
@interface ScriptedMain: Object {} |
||
Line 979: | Line 1,232: | ||
+ (int)meaningOfLife; |
+ (int)meaningOfLife; |
||
@end</ |
@end</syntaxhighlight> |
||
scriptedmain.m: |
scriptedmain.m: |
||
< |
<syntaxhighlight lang="objc">#import "scriptedmain.h" |
||
#import <Foundation/Foundation.h> |
#import <Foundation/Foundation.h> |
||
Line 1,002: | Line 1,255: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
test.m: |
test.m: |
||
< |
<syntaxhighlight lang="objc">#import "scriptedmain.h" |
||
#import <Foundation/Foundation.h> |
#import <Foundation/Foundation.h> |
||
Line 1,017: | Line 1,270: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="sh">$ gcc -o scriptedmain -lobjc -framework foundation scriptedmain.m |
||
$ gcc -o test -lobjc -framework foundation test.m scriptedmain.m |
$ gcc -o test -lobjc -framework foundation test.m scriptedmain.m |
||
$ ./scriptedmain |
$ ./scriptedmain |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ ./test |
$ ./test |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
Line 1,030: | Line 1,283: | ||
scriptedmain.ml |
scriptedmain.ml |
||
<lang |
<syntaxhighlight lang="ocaml">let meaning_of_life = 42 |
||
"exec" "ocaml" "str.cma" "$0" "$@" + let fi = 0 and exit _ _ = 0 in if false |
|||
then exit |
|||
fi |
|||
true else 0 |
|||
end;; |
|||
let main () = |
|||
(* |
|||
Printf.printf "Main: The meaning of life is %d\n" |
|||
meaning_of_life |
|||
let () = |
|||
Interpret: |
|||
if not !Sys.interactive then |
|||
main ()</syntaxhighlight> |
|||
Invoked as a script: |
|||
./scriptedmain.ml |
|||
<syntaxhighlight lang="sh">$ ocaml scriptedmain.ml |
|||
Compile: |
|||
Main: The meaning of life is 42</syntaxhighlight> |
|||
ocamlc -o scriptedmain -linkall str.cma scriptedmain.ml |
|||
Run: |
|||
./scriptedmain |
|||
*) |
|||
let meaning_of_life : int = 42 |
|||
let main () = print_endline ("Main: The meaning of life is " ^ string_of_int meaning_of_life) |
|||
let _ = |
|||
let program = Sys.argv.(0) |
|||
and re = Str.regexp "scriptedmain" in |
|||
try let _ = Str.search_forward re program 0 in |
|||
main () |
|||
with Not_found -> ()</lang> |
|||
test.ml |
|||
<lang ocaml>if true then ignore begin let kkkk _ _ _ _ _ _ = 0 in kkkk |
|||
"exec" "ocaml" "str.cma" "scriptedmain.cmo" "$0" "$@" + let fi = 0 and exit _ _ = 0 in if false |
|||
then exit |
|||
fi |
|||
true else 0 |
|||
end;; |
|||
(* |
|||
Compile: |
|||
ocamlc -o test -linkall str.cma scriptedmain.ml test.ml |
|||
Interpret: |
|||
./test.ml |
|||
Run: |
|||
./test |
|||
Loaded into an ocaml toplevel/utop: |
|||
*) |
|||
<syntaxhighlight lang="text">$ ocaml |
|||
let main () = print_endline ("Test: The meaning of life is " ^ string_of_int Scriptedmain.meaning_of_life) |
|||
... |
|||
# #use "scriptedmain.ml";; |
|||
val meaning_of_life : int = 42 |
|||
val main : unit -> unit = <fun> |
|||
# meaning_of_life;; |
|||
- : int = 42 |
|||
# </syntaxhighlight> |
|||
The limit of this technique is "avoiding running something when loading a script interactively". It's not applicable to other uses, like adding an example script to a file normally used as a library, as that code will also fire when users of the library are run. |
|||
let _ = |
|||
let program = Sys.argv.(0) |
|||
and re = Str.regexp "test" in |
|||
try let _ = Str.search_forward re program 0 in |
|||
main () |
|||
with Not_found -> ()</lang> |
|||
=={{header|Octave}}/{{header|MATLAB}}== |
=={{header|Octave}}/{{header|MATLAB}}== |
||
Line 1,103: | Line 1,316: | ||
meaningoflife.m |
meaningoflife.m |
||
< |
<syntaxhighlight lang="matlab">#!/usr/bin/env octave -qf |
||
function y = meaningoflife() |
function y = meaningoflife() |
||
Line 1,113: | Line 1,326: | ||
endfunction |
endfunction |
||
main();</ |
main();</syntaxhighlight> |
||
test.m |
test.m |
||
< |
<syntaxhighlight lang="matlab">#!/usr/bin/env octave -qf |
||
printf("Test: The meaning of life is %d", meaningoflife());</ |
printf("Test: The meaning of life is %d", meaningoflife());</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 1,125: | Line 1,338: | ||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">all: scriptedmain |
||
scriptedmain: scriptedmain.pas |
scriptedmain: scriptedmain.pas |
||
Line 1,137: | Line 1,350: | ||
-rm scriptedmain |
-rm scriptedmain |
||
-rm *.o |
-rm *.o |
||
-rm *.ppu</ |
-rm *.ppu</syntaxhighlight> |
||
scriptedmain.pas: |
scriptedmain.pas: |
||
< |
<syntaxhighlight lang="pascal">{$IFDEF scriptedmain} |
||
program ScriptedMain; |
program ScriptedMain; |
||
{$ELSE} |
{$ELSE} |
||
Line 1,158: | Line 1,371: | ||
writeln(MeaningOfLife()) |
writeln(MeaningOfLife()) |
||
{$ENDIF} |
{$ENDIF} |
||
end.</ |
end.</syntaxhighlight> |
||
test.pas: |
test.pas: |
||
< |
<syntaxhighlight lang="pascal">program Test; |
||
uses |
uses |
||
ScriptedMain; |
ScriptedMain; |
||
Line 1,168: | Line 1,381: | ||
write('Test: The meaning of life is: '); |
write('Test: The meaning of life is: '); |
||
writeln(MeaningOfLife()) |
writeln(MeaningOfLife()) |
||
end.</ |
end.</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ make |
||
$ ./scriptedmain |
$ ./scriptedmain |
||
Main: The meaning of life is: 42 |
Main: The meaning of life is: 42 |
||
$ make test |
$ make test |
||
$ ./test |
$ ./test |
||
Test: The meaning of life is: 42</ |
Test: The meaning of life is: 42</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Perl has scripted main. The code inside <tt>unless(caller) { ... }</tt> only runs when <tt>Life.pm</tt> is the main program. |
Perl has scripted main. The code inside <tt>unless(caller) { ... }</tt> only runs when <tt>Life.pm</tt> is the main program. |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/env perl |
||
# Life.pm |
# Life.pm |
||
Line 1,196: | Line 1,409: | ||
unless(caller) { |
unless(caller) { |
||
print "Main: The meaning of life is " . meaning_of_life() . "\n"; |
print "Main: The meaning of life is " . meaning_of_life() . "\n"; |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/env perl |
||
# death.pl |
# death.pl |
||
Line 1,207: | Line 1,420: | ||
print "Life means " . Life::meaning_of_life . ".\n"; |
print "Life means " . Life::meaning_of_life . ".\n"; |
||
print "Death means invisible scary skeletons.\n";</ |
print "Death means invisible scary skeletons.\n";</syntaxhighlight> |
||
=={{header|Perl 6}}== |
|||
Perl 6 automatically calls MAIN on direct invocation, but this may be a multi dispatch, so a library may have multiple "scripted mains". |
|||
<lang perl6>class LUE { |
|||
has $.answer = 42; |
|||
} |
|||
=={{header|Phix}}== |
|||
multi MAIN ('test') { |
|||
There is a builtin for this, which can even be asked to skip an arbitrary number of stack frames and that way find out exactly where it was effectively called from. |
|||
say "ok" if LUE.new.answer == 42; |
|||
<!--<syntaxhighlight lang="phix">(notonline)--> |
|||
} |
|||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (includefile)</span> |
|||
<span style="color: #004080;">string</span> <span style="color: #000000;">mori</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">include_file</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"main"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"an include"</span><span style="color: #0000FF;">)</span> |
|||
multi MAIN ('methods') { |
|||
<!--</syntaxhighlight>--> |
|||
say ~LUE.^methods; |
|||
}</lang> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
Line 1,227: | Line 1,434: | ||
scriptedmain.php |
scriptedmain.php |
||
< |
<syntaxhighlight lang="php"><?php |
||
function meaning_of_life() { |
function meaning_of_life() { |
||
return 42; |
return 42; |
||
Line 1,239: | Line 1,446: | ||
main($argv); |
main($argv); |
||
} |
} |
||
?></ |
?></syntaxhighlight> |
||
test.php |
test.php |
||
< |
<syntaxhighlight lang="php"><?php |
||
require_once("scriptedmain.php"); |
require_once("scriptedmain.php"); |
||
echo "Test: The meaning of life is " . meaning_of_life() . "\n"; |
echo "Test: The meaning of life is " . meaning_of_life() . "\n"; |
||
?></ |
?></syntaxhighlight> |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
PicoLisp normally does it the other way round: It calls main from the command line with the '-' syntax if desired. Create an executable file (chmod +x) "life.l": |
PicoLisp normally does it the other way round: It calls main from the command line with the '-' syntax if desired. Create an executable file (chmod +x) "life.l": |
||
< |
<syntaxhighlight lang="picolisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l |
||
(de meaningOfLife () |
(de meaningOfLife () |
||
Line 1,257: | Line 1,464: | ||
(de lifemain () |
(de lifemain () |
||
(prinl "Main: The meaning of life is " (meaningOfLife)) |
(prinl "Main: The meaning of life is " (meaningOfLife)) |
||
(bye) )</ |
(bye) )</syntaxhighlight> |
||
and an executable file (chmod +x) "test.l": |
and an executable file (chmod +x) "test.l": |
||
< |
<syntaxhighlight lang="picolisp">#!/usr/bin/picolisp /usr/lib/picolisp/lib.l |
||
(load "life.l") |
(load "life.l") |
||
(prinl "Test: The meaning of life is " (meaningOfLife)) |
(prinl "Test: The meaning of life is " (meaningOfLife)) |
||
(bye)</ |
(bye)</syntaxhighlight> |
||
Test: |
Test: |
||
<pre>$ ./life.l -lifemain |
<pre>$ ./life.l -lifemain |
||
Line 1,275: | Line 1,482: | ||
Python has scripted main. |
Python has scripted main. |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/env python |
||
# life.py |
# life.py |
||
Line 1,283: | Line 1,490: | ||
if __name__ == "__main__": |
if __name__ == "__main__": |
||
print("Main: The meaning of life is %s" % meaning_of_life())</ |
print("Main: The meaning of life is %s" % meaning_of_life())</syntaxhighlight> |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/env python |
||
# death.py |
# death.py |
||
Line 1,292: | Line 1,499: | ||
print("Life means %s." % meaning_of_life()) |
print("Life means %s." % meaning_of_life()) |
||
print("Death means invisible scary skeletons.")</ |
print("Death means invisible scary skeletons.")</syntaxhighlight> |
||
=={{header|R}}== |
=={{header|R}}== |
||
A way to check if code is running at "top level" is to check <code>length(sys.frames())</code>. This value will be zero for a file being run with <code>Rscript</code>, the <code>--file=</code> argument, or at the command line, and will be greater than 0 in all other conditions (such as package loading or code being sourced from another file.) |
A way to check if code is running at "top level" is to check <code>length(sys.frames())</code>. This value will be zero for a file being run with <code>Rscript</code>, the <code>--file=</code> argument, or at the command line, and will be greater than 0 in all other conditions (such as package loading or code being sourced from another file.) |
||
< |
<syntaxhighlight lang="r">#!/usr/bin/env Rscript |
||
meaningOfLife <- function() { |
meaningOfLife <- function() { |
||
Line 1,310: | Line 1,518: | ||
main(args) |
main(args) |
||
q("no") |
q("no") |
||
}</ |
}</syntaxhighlight> |
||
test.R |
test.R |
||
< |
<syntaxhighlight lang="r">#!/usr/bin/env Rscript |
||
source("scriptedmain.R") |
source("scriptedmain.R") |
||
Line 1,320: | Line 1,528: | ||
cat("Test: The meaning of life is", meaningOfLife(), "\n") |
cat("Test: The meaning of life is", meaningOfLife(), "\n") |
||
q("no")</ |
q("no")</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
scriptedmain.rkt: |
scriptedmain.rkt: |
||
< |
<syntaxhighlight lang="racket">#!/usr/bin/env racket |
||
#lang racket |
#lang racket |
||
Line 1,331: | Line 1,539: | ||
(define (meaning-of-life) 42) |
(define (meaning-of-life) 42) |
||
(module+ main (printf "Main: The meaning of life is ~a\n" (meaning-of-life)))</ |
(module+ main (printf "Main: The meaning of life is ~a\n" (meaning-of-life)))</syntaxhighlight> |
||
test.rkt: |
test.rkt: |
||
< |
<syntaxhighlight lang="racket">#!/usr/bin/env racket |
||
#lang racket |
#lang racket |
||
(module+ main |
(module+ main |
||
(require "scriptedmain.rkt") |
(require "scriptedmain.rkt") |
||
(printf "Test: The meaning of life is ~a\n" (meaning-of-life)))</ |
(printf "Test: The meaning of life is ~a\n" (meaning-of-life)))</syntaxhighlight> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
Raku automatically calls MAIN on direct invocation, but this may be a multi dispatch, so a library may have multiple "scripted mains". |
|||
<syntaxhighlight lang="raku" line>class LUE { |
|||
has $.answer = 42; |
|||
} |
|||
multi MAIN ('test') { |
|||
say "ok" if LUE.new.answer == 42; |
|||
} |
|||
multi MAIN ('methods') { |
|||
say ~LUE.^methods; |
|||
}</syntaxhighlight> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program detects whether or not it is a "scripted main" program. */ |
||
parse source . howInvoked |
parse source . howInvoked @fn /*query REXX how this pgm got invoked. */ |
||
if howInvoked \== "COMMAND" then exit 0 /*if not a COMMAND, just exit.*/ |
|||
say 'This program ('@fn") was invoked as a: " howInvoked |
|||
/* ╔════════════════════════════════════════════════════════════╗ |
|||
║ At this point, we know that this program was invoked via ║ |
|||
║ the "command line" and not via another program. ║ |
|||
╚════════════════════════════════════════════════════════════╝ */ |
|||
if howInvoked\=='COMMAND' then do |
|||
say 'and away we go ···'</lang> |
|||
say 'This program ('@fn") wasn't invoked via a command." |
|||
Note: Actually another program can invoke the above (ma.rex) as a command: |
|||
exit 12 |
|||
<lang rexx>'rexx ma'</lang> |
|||
end |
|||
/*╔════════════════════════════════════════════════════════════════════════════════╗ |
|||
║ At this point, we know that this program was invoked via the "command line" ║ |
|||
║ or a program using the "command interface" and not via another program. ║ |
|||
╚════════════════════════════════════════════════════════════════════════════════╝*/ |
|||
/*────────────────────────────── The main code follows here ... ────────────────────────*/ |
|||
say |
|||
say '(from' @fn"): and away we go ···"</syntaxhighlight> <br><br> |
|||
=={{header|Ring}}== |
|||
<syntaxhighlight lang="ring"> |
|||
# Project : Modulinos |
|||
func meaningoflife() |
|||
y = 42 |
|||
return y |
|||
func main() |
|||
see "Main: The meaning of life is " + meaningoflife() + nl |
|||
</syntaxhighlight> |
|||
Output: |
|||
<pre> |
|||
Main: The meaning of life is 42 |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Ruby has scripted main. |
Ruby has scripted main. |
||
< |
<syntaxhighlight lang="ruby"># life.rb |
||
def meaning_of_life |
def meaning_of_life |
||
Line 1,366: | Line 1,611: | ||
if __FILE__ == $0 |
if __FILE__ == $0 |
||
puts "Main: The meaning of life is #{meaning_of_life}" |
puts "Main: The meaning of life is #{meaning_of_life}" |
||
end</ |
end</syntaxhighlight> |
||
< |
<syntaxhighlight lang="ruby"># death.rb |
||
require 'life' |
require 'life' |
||
puts "Life means #{meaning_of_life}." |
puts "Life means #{meaning_of_life}." |
||
puts "Death means invisible scary skeletons."</ |
puts "Death means invisible scary skeletons."</syntaxhighlight> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Line 1,379: | Line 1,624: | ||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">all: scriptedmain |
||
scriptedmain: scriptedmain.rs |
scriptedmain: scriptedmain.rs |
||
Line 1,392: | Line 1,637: | ||
-rm -rf *.dylib |
-rm -rf *.dylib |
||
-rm scriptedmain |
-rm scriptedmain |
||
-rm -rf *.dSYM</ |
-rm -rf *.dSYM</syntaxhighlight> |
||
scriptedmain.rs: |
scriptedmain.rs: |
||
< |
<syntaxhighlight lang="rust">#[link(name = "scriptedmain")]; |
||
use std; |
use std; |
||
Line 1,405: | Line 1,650: | ||
fn main() { |
fn main() { |
||
std::io::println("Main: The meaning of life is " + core::int::to_str(meaning_of_life(), 10u)); |
std::io::println("Main: The meaning of life is " + core::int::to_str(meaning_of_life(), 10u)); |
||
}</ |
}</syntaxhighlight> |
||
test.rs: |
test.rs: |
||
< |
<syntaxhighlight lang="rust">use scriptedmain; |
||
use std; |
use std; |
||
fn main() { |
fn main() { |
||
std::io::println("Test: The meaning of life is " + core::int::to_str(scriptedmain::meaning_of_life(), 10u)); |
std::io::println("Test: The meaning of life is " + core::int::to_str(scriptedmain::meaning_of_life(), 10u)); |
||
}</ |
}</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ make |
||
$ make test |
$ make test |
||
$ ./scriptedmain |
$ ./scriptedmain |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ ./test |
$ ./test |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|SAC}}== |
=={{header|SAC}}== |
||
Makefile: |
Makefile: |
||
< |
<syntaxhighlight lang="make">all: scriptedmain |
||
scriptedmain: ScriptedMain.sac |
scriptedmain: ScriptedMain.sac |
||
Line 1,441: | Line 1,686: | ||
-rm libScriptedMainMod.a |
-rm libScriptedMainMod.a |
||
-rm scriptedmain |
-rm scriptedmain |
||
-rm scriptedmain.c</ |
-rm scriptedmain.c</syntaxhighlight> |
||
ScriptedMain.sac: |
ScriptedMain.sac: |
||
< |
<syntaxhighlight lang="c">#ifndef scriptedmain |
||
module ScriptedMain; |
module ScriptedMain; |
||
#endif |
#endif |
||
Line 1,461: | Line 1,706: | ||
return(0); |
return(0); |
||
} |
} |
||
#endif</ |
#endif</syntaxhighlight> |
||
test.sac: |
test.sac: |
||
< |
<syntaxhighlight lang="c">use StdIO: all; |
||
use Array: all; |
use Array: all; |
||
use ScriptedMain: all; |
use ScriptedMain: all; |
||
Line 1,471: | Line 1,716: | ||
printf("Test: The meaning of life is %d\n", meaning_of_life()); |
printf("Test: The meaning of life is %d\n", meaning_of_life()); |
||
return(0); |
return(0); |
||
}</ |
}</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="sh">$ make |
||
$ make test |
$ make test |
||
$ ./scriptedmain |
$ ./scriptedmain |
||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
$ ./test |
$ ./test |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|Scala}} |
{{libheader|Scala}} |
||
Line 1,486: | Line 1,732: | ||
===Unix shell script=== |
===Unix shell script=== |
||
This code must be stored as a shell script. |
This code must be stored as a shell script. |
||
< |
<syntaxhighlight lang="bash">#!/bin/sh |
||
exec scala "$0" "$@" |
exec scala "$0" "$@" |
||
!# |
!# |
||
Line 1,496: | Line 1,742: | ||
println(s"Use the routine to show that the hailstone sequence for the number: $nr.") |
println(s"Use the routine to show that the hailstone sequence for the number: $nr.") |
||
println(collatz.toList) |
println(collatz.toList) |
||
println(s"It has ${collatz.length} elements.")</ |
println(s"It has ${collatz.length} elements.")</syntaxhighlight> |
||
===Windows Command Script=== |
===Windows Command Script=== |
||
This code must be stored as a Windows Command Script e.g. Hailstone.cmd |
This code must be stored as a Windows Command Script e.g. Hailstone.cmd |
||
< |
<syntaxhighlight lang="winbatch">::#! |
||
@echo off |
@echo off |
||
call scala %0 %* |
call scala %0 %* |
||
Line 1,514: | Line 1,760: | ||
println(collatz.toList) |
println(collatz.toList) |
||
println(s"It has ${collatz.length} elements.") |
println(s"It has ${collatz.length} elements.") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>C:\>Hailstone.cmd 42 |
<pre>C:\>Hailstone.cmd 42 |
||
Line 1,527: | Line 1,773: | ||
scriptedmain.scm |
scriptedmain.scm |
||
< |
<syntaxhighlight lang="scheme">#!/bin/sh |
||
#| |
#| |
||
exec csi -ss $0 ${1+"$@"} |
exec csi -ss $0 ${1+"$@"} |
||
Line 1,551: | Line 1,797: | ||
(if (equal? (car (program)) 'compiled) |
(if (equal? (car (program)) 'compiled) |
||
(main (cdr (argv))))</ |
(main (cdr (argv))))</syntaxhighlight> |
||
test.scm |
test.scm |
||
< |
<syntaxhighlight lang="scheme">#!/bin/sh |
||
#| |
#| |
||
exec csi -ss $0 ${1+"$@"} |
exec csi -ss $0 ${1+"$@"} |
||
Line 1,563: | Line 1,809: | ||
(load "scriptedmain.scm") |
(load "scriptedmain.scm") |
||
(display (format "Test: The meaning of life is ~a\n" (meaning-of-life))) |
(display (format "Test: The meaning of life is ~a\n" (meaning-of-life))) |
||
(exit))</ |
(exit))</syntaxhighlight> |
||
=={{header|Sidef}}== |
|||
<syntaxhighlight lang="ruby"># Life.sm |
|||
func meaning_of_life { |
|||
42 |
|||
} |
|||
if (__FILE__ == __MAIN__) { |
|||
say "Main: The meaning of life is #{meaning_of_life()}" |
|||
}</syntaxhighlight> |
|||
<syntaxhighlight lang="ruby"># test.sf |
|||
include Life |
|||
say "Test: The meaning of life is #{Life::meaning_of_life()}."</syntaxhighlight> |
|||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
Line 1,571: | Line 1,834: | ||
Example |
Example |
||
< |
<syntaxhighlight lang="shell">$ gst-package -t ~/.st package.xml &>/dev/null |
||
$ ./scriptedmain.st |
$ ./scriptedmain.st |
||
Line 1,577: | Line 1,840: | ||
$ ./test.st |
$ ./test.st |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
package.xml |
package.xml |
||
< |
<syntaxhighlight lang="xml"><packages> |
||
<package> |
<package> |
||
<name>ScriptedMain</name> |
<name>ScriptedMain</name> |
||
Line 1,587: | Line 1,850: | ||
<file>scriptedmain.st</file> |
<file>scriptedmain.st</file> |
||
</package> |
</package> |
||
</packages></ |
</packages></syntaxhighlight> |
||
scriptedmain.st |
scriptedmain.st |
||
< |
<syntaxhighlight lang="smalltalk">"exec" "gst" "-f" "$0" "$0" "$@" |
||
"exit" |
"exit" |
||
Line 1,606: | Line 1,869: | ||
(((Smalltalk getArgc) > 0) and: [ ((Smalltalk getArgv: 1) endsWith: 'scriptedmain.st') ]) ifTrue: [ |
(((Smalltalk getArgc) > 0) and: [ ((Smalltalk getArgv: 1) endsWith: 'scriptedmain.st') ]) ifTrue: [ |
||
main value. |
main value. |
||
].</ |
].</syntaxhighlight> |
||
test.st |
test.st |
||
< |
<syntaxhighlight lang="smalltalk">"exec" "gst" "-f" "$0" "$0" "$@" |
||
"exit" |
"exit" |
||
Line 1,616: | Line 1,879: | ||
PackageLoader fileInPackage: 'ScriptedMain'. |
PackageLoader fileInPackage: 'ScriptedMain'. |
||
Transcript show: 'Test: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.</ |
Transcript show: 'Test: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
Line 1,625: | Line 1,887: | ||
Example |
Example |
||
< |
<syntaxhighlight lang="shell">$ make |
||
mkdir -p bin/ |
mkdir -p bin/ |
||
swiftc -D SCRIPTEDMAIN -o bin/ScriptedMain ScriptedMain.swift |
swiftc -D SCRIPTEDMAIN -o bin/ScriptedMain ScriptedMain.swift |
||
Line 1,634: | Line 1,896: | ||
Main: The meaning of life is 42 |
Main: The meaning of life is 42 |
||
bin/Test |
bin/Test |
||
Test: The meaning of life is 42</ |
Test: The meaning of life is 42</syntaxhighlight> |
||
Makefile |
Makefile |
||
< |
<syntaxhighlight lang="make">all: bin/ScriptedMain bin/Test |
||
bin/ScriptedMain |
bin/ScriptedMain |
||
bin/Test |
bin/Test |
||
Line 1,658: | Line 1,920: | ||
-rm *.swiftdoc |
-rm *.swiftdoc |
||
-rm *.dylib |
-rm *.dylib |
||
</syntaxhighlight> |
|||
</lang> |
|||
ScriptedMain.swift |
ScriptedMain.swift |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
public class ScriptedMain { |
public class ScriptedMain { |
||
Line 1,683: | Line 1,945: | ||
} |
} |
||
#endif |
#endif |
||
</syntaxhighlight> |
|||
</lang> |
|||
Test.swift |
Test.swift |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
import ScriptedMain |
import ScriptedMain |
||
Line 1,705: | Line 1,967: | ||
} |
} |
||
#endif |
#endif |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc main {args} { |
||
puts "Directory: [pwd]" |
puts "Directory: [pwd]" |
||
puts "Program: $::argv0" |
puts "Program: $::argv0" |
||
Line 1,717: | Line 1,979: | ||
if {$::argv0 eq [info script]} { |
if {$::argv0 eq [info script]} { |
||
main {*}$::argv |
main {*}$::argv |
||
}</ |
}</syntaxhighlight> |
||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
Line 1,724: | Line 1,986: | ||
scriptedmain.sh |
scriptedmain.sh |
||
< |
<syntaxhighlight lang="sh">#!/usr/bin/env sh |
||
meaning_of_life() { |
meaning_of_life() { |
||
Line 1,738: | Line 2,000: | ||
then |
then |
||
main |
main |
||
fi</ |
fi</syntaxhighlight> |
||
test.sh |
test.sh |
||
< |
<syntaxhighlight lang="sh">#!/bin/bash |
||
path=$(dirname -- "$0") |
path=$(dirname -- "$0") |
||
Line 1,749: | Line 2,011: | ||
meaning_of_life |
meaning_of_life |
||
echo "Test: The meaning of life is $?" |
echo "Test: The meaning of life is $?" |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Wren}}== |
|||
As far as Wren is concerned, a modulino and an executable library seem to be different names for the same thing. This therefore uses the same technique as the [[Executable_library#Wren]] task to create a simple modulino. |
|||
Note that Wren doesn't need or normally use a ''main()'' function to start a script, though we use one here to make the example clearer. |
|||
First we create a module for our modulino: |
|||
<syntaxhighlight lang="wren">/* Modulinos.wren */ |
|||
var MeaningOfLife = Fn.new { 42 } |
|||
var main = Fn.new { |
|||
System.print("The meaning of life is %(MeaningOfLife.call()).") |
|||
} |
|||
// Check if it's being used as a library or not. |
|||
import "os" for Process |
|||
if (Process.allArguments[1] == "Modulinos.wren") { // if true, not a library |
|||
main.call() |
|||
}</syntaxhighlight> |
|||
and run it to make sure it works OK when run directly: |
|||
{{output}} |
|||
<pre> |
|||
The meaning of life is 42. |
|||
</pre> |
|||
Next we create another module which imports the modulino: |
|||
<syntaxhighlight lang="wren">/* Modulinos_main.wren */ |
|||
import "./Modulinos" for MeaningOfLife |
|||
var main = Fn.new { |
|||
System.print("Who says the meaning of life is %(MeaningOfLife.call())?") |
|||
} |
|||
main.call()</syntaxhighlight> |
|||
and run this to make sure the modulino's ''main()'' function doesn't run: |
|||
{{output}} |
|||
<pre> |
|||
Who says the meaning of life is 42? |
|||
</pre> |
|||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
Line 1,755: | Line 2,060: | ||
On the ZX Spectrum, there is no main function as such, however a saved program can be made to start running from a particular line number by providing the line number as a parameter to save command. If the program is being merged as a module, then it does not run automatically. The following example will save the program in memory so that it starts running from line 500: |
On the ZX Spectrum, there is no main function as such, however a saved program can be made to start running from a particular line number by providing the line number as a parameter to save command. If the program is being merged as a module, then it does not run automatically. The following example will save the program in memory so that it starts running from line 500: |
||
< |
<syntaxhighlight lang="zxbasic">SAVE "MYPROG" LINE 500: REM For a program with main code starting at line 500</syntaxhighlight> |
||
{{omit from|Ada}} |
{{omit from|Ada}} |
||
{{omit from|Go}} |
Latest revision as of 10:33, 4 January 2024
It is useful to be able to execute a main() function only when a program is run directly. This is a central feature in programming scripts. A script that behaves this way is called a modulino.
Examples from https://github.com/mcandre/modulinos
Sometimes getting the ScriptName is required in order to determine when to run main().
Care when manipulating command line arguments, due to subtle exec security constraints that may or not be enforced on implicit argv[0]. https://ryiron.wordpress.com/2013/12/16/argv-silliness/
- This is still a draft task, and the current task description has caused mega confusion. See Talk:Modulinos for numerous attempts to understand the task and to rewrite the task description.
- The task Executable library is written to replace this task. This task's future is in doubt as its aims are not clear enough.
11l
// life.11l
F meaning_of_life()
R ‘*’.code
:start:
print(‘Main: The meaning of life is ’meaning_of_life())
// death.11l
print(‘Life means ’life:meaning_of_life()‘.’)
print(‘Death means nothing.’)
AppleScript
AppleScript's equivalent of a main() function is a run handler, which can be either implicit or explicit:
display dialog "Hello"
or
on run
display dialog "Hello"
end run
A run handler's only executed when the script containing it is explicity run, either from another script or application or as an application in its own right. It's not executed when a script's simply loaded as a library, although it can subsequently be so in the unlikely event of this being desirable. Scripts saved as applications aren't recognised by the "Libraries" system introduced in Mac OS X 10.9, but can be loaded and/or run using the older load script and run script commands. Script code can tell if it's running in its own application or being executed by an external agent by comparing its file path with that of the agent:
on run
if ((path to me) = (path to current application)) then
display dialog "I'm running in my own application."
else
display dialog "I'm being run from another script or application."
end if
end run
Arturo
Library
; modulinos - library
meaningOfLife: function [][
42
]
if standalone? ->
print ~"Library: The meaning of life is |meaningOfLife|"
- Output:
Library: The meaning of life is 42
Main
do.import relative "modulinos - library.art"
print ~"Life means |meaningOfLife|."
print "Death means invisible scary skeletons."
- Output:
Life means 42. Death means invisible scary skeletons.
C
C programs cannot normally do scripted main, because main() is implicitly included by another program, test.c, even though scriptedmain.h omits any main() prototype. However, preprocessor instructions can hide main unless a compiler flag is explicitly set.
Example
$ make
./scriptedmain
Main: The meaning of life is 42
./test
Test: The meaning of life is
Makefile
all: scriptedmain test
./scriptedmain
./test
scriptedmain: scriptedmain.c scriptedmain.h
gcc -o scriptedmain -DSCRIPTEDMAIN scriptedmain.c scriptedmain.h
test: test.c scriptedmain.h scriptedmain.c
gcc -o test test.c scriptedmain.c scriptedmain.h
clean:
-rm scriptedmain
-rm test
-rm scriptedmain.exe
-rm test.exe
scriptedmain.h
int meaning_of_life();
scriptedmain.c
#include <stdio.h>
int meaning_of_life() {
return 42;
}
#ifdef SCRIPTEDMAIN
int main() {
printf("Main: The meaning of life is %d\n", meaning_of_life());
return 0;
}
#endif
test.c
#include "scriptedmain.h"
#include <stdio.h>
extern int meaning_of_life();
int main(int argc, char **argv) {
printf("Test: The meaning of life is %d\n", meaning_of_life());
return 0;
}
C++
C++ programs cannot normally do scripted main, because main() is implicitly included by another program, test.c, even though scriptedmain.h omits any main() prototype. Preprocessor instructions can hide main() unless a compiler flat is explicitly set.
Example
$ make
./scriptedmain
Main: The meaning of life is 42
./test
Test: The meaning of life is 42
Makefile
all: scriptedmain test
./scriptedmain
./test
scriptedmain: scriptedmain.cpp scriptedmain.h
g++ -o scriptedmain -static-libgcc -static-libstdc++ -DSCRIPTEDMAIN scriptedmain.cpp scriptedmain.h
test: test.cpp scriptedmain.h scriptedmain.cpp
g++ -o test -static-libgcc -static-libstdc++ test.cpp scriptedmain.cpp scriptedmain.h
clean:
-rm scriptedmain
-rm test
-rm scriptedmain.exe
-rm test.exe
scriptedmain.h
int meaning_of_life();
scriptedmain.cpp
#include <iostream>
using namespace std;
int meaning_of_life() {
return 42;
}
#ifdef SCRIPTEDMAIN
int main() {
cout << "Main: The meaning of life is " << meaning_of_life() << endl;
return 0;
}
#endif
test.cpp
#include "scriptedmain.h"
#include <iostream>
using namespace std;
extern int meaning_of_life();
int main() {
cout << "Test: The meaning of life is " << meaning_of_life() << endl;
return 0;
}
Clojure
Uses lein-exec.
scriptedmain.clj:
":";exec lein exec $0 ${1+"$@"}
":";exit
(ns scriptedmain
(:gen-class))
(defn meaning-of-life [] 42)
(defn -main [& args]
(println "Main: The meaning of life is" (meaning-of-life)))
(when (.contains (first *command-line-args*) *source-path*)
(apply -main (rest *command-line-args*)))
test.clj:
":";exec lein exec $0 ${1+"$@"}
":";exit
(ns test
(:gen-class))
(load-string (slurp "scriptedmain.clj"))
(defn -main [& args]
(println "Test: The meaning of life is" (scriptedmain/meaning-of-life)))
(when (.contains (first *command-line-args*) *source-path*)
(apply -main (rest *command-line-args*)))
CoffeeScript
scriptedmain.coffee:
#!/usr/bin/env coffee
meaningOfLife = () -> 42
exports.meaningOfLife = meaningOfLife
main = () ->
console.log "Main: The meaning of life is " + meaningOfLife()
if not module.parent then main()
test.coffee:
#!/usr/bin/env coffee
sm = require "./scriptedmain"
console.log "Test: The meaning of life is " + sm.meaningOfLife()
Common Lisp
Common Lisp has few standards for POSIX operation. Shebangs and command line arguments are hacks.
In CLISP, this code only works for ./scriptedmain.lisp.
~/.clisprc.lisp
;;; Play nice with shebangs
(set-dispatch-macro-character #\# #\!
(lambda (stream character n)
(declare (ignore character n))
(read-line stream nil nil t)
nil))
scriptedmain.lisp
#!/bin/sh
#|
exec clisp -q -q $0 $0 ${1+"$@"}
exit
|#
;;; Usage: ./scriptedmain.lisp
(defun meaning-of-life () 42)
(defun main (args)
(format t "Main: The meaning of life is ~a~%" (meaning-of-life))
(quit))
;;; With help from Francois-Rene Rideau
;;; http://tinyurl.com/cli-args
(let ((args
#+clisp ext:*args*
#+sbcl sb-ext:*posix-argv*
#+clozure (ccl::command-line-arguments)
#+gcl si:*command-args*
#+ecl (loop for i from 0 below (si:argc) collect (si:argv i))
#+cmu extensions:*command-line-strings*
#+allegro (sys:command-line-arguments)
#+lispworks sys:*line-arguments-list*
))
(if (member (pathname-name *load-truename*)
args
:test #'(lambda (x y) (search x y :test #'equalp)))
(main args)))
test.lisp
#!/bin/sh
#|
exec clisp -q -q $0 $0 ${1+"$@"}
exit
|#
(load "scriptedmain.lisp")
(format t "Test: The meaning of life is ~a~%" (meaning-of-life))
D
D manages to implement scriptedmain through the use of version directives, which require special options to rdmd and dmd.
scriptedmain.d:
#!/usr/bin/env rdmd -version=scriptedmain
module scriptedmain;
import std.stdio;
int meaningOfLife() {
return 42;
}
version (scriptedmain) {
void main(string[] args) {
writeln("Main: The meaning of life is ", meaningOfLife());
}
}
test.d:
#!/usr/bin/env rdmd -version=test
import scriptedmain;
import std.stdio;
version (test) {
void main(string[] args) {
writeln("Test: The meaning of life is ", meaningOfLife());
}
}
Example:
$ ./scriptedmain.d
Main: The meaning of life is 42
$ ./test.d
Test: The meaning of life is 42
$ dmd scriptedmain.d -version=scriptedmain
$ ./scriptedmain
Main: The meaning of life is 42
$ dmd test.d scriptedmain.d -version=test
$ ./test
Test: The meaning of life is 42
Dart
scriptedmain.dart:
#!/usr/bin/env dart
#library("scriptedmain");
meaningOfLife() {
return 42;
}
main() {
print("Main: The meaning of life is ${meaningOfLife()}");
}
test.dart:
#!/usr/bin/env dart
#import("scriptedmain.dart", prefix: "scriptedmain");
main() {
print("Test: The meaning of life is ${scriptedmain.meaningOfLife()}");
}
Example:
$ ./scriptedmain.dart
Main: The meaning of life is 42
$ ./test.dart
Test: The meaning of life is 42
Emacs Lisp
Emacs has scripted main, though older versions require an obscure shebang syntax.
scriptedmain.el
:;exec emacs -batch -l $0 -f main $*
;;; Shebang from John Swaby
;;; http://www.emacswiki.org/emacs/EmacsScripts
(defun meaning-of-life () 42)
(defun main ()
(message "Main: The meaning of life is %d" (meaning-of-life)))
test.el
:;exec emacs -batch -l $0 -f main $*
;;; Shebang from John Swaby
;;; http://www.emacswiki.org/emacs/EmacsScripts
(defun main ()
(setq load-path (cons default-directory load-path))
(load "scriptedmain.el" nil t)
(message "Test: The meaning of life is %d" (meaning-of-life)))
EMal
^|We have created a module named ModulinosPart.emal.
|^
in Org:RosettaCode
type ModulinosPart
fun meaningOfLife = int by block do return 42 end
fun main = void by block do writeLine("The meaning of life is " + meaningOfLife() + ".") end
if Runtime.direct() do main() end
- Output:
emal.exe Org\RosettaCode\ModulinosPart.emal The meaning of life is 42.
^|Then we create a new module named Modulinos.emal,
|this imports the previous module.
|^
in Org:RosettaCode
load :ModulinosPart
type Modulinos
fun main = int by List args
writeLine("Who says the meaning of life is " + ModulinosPart.meaningOfLife() + "?")
return 0
end
exit main(Runtime.args)
- Output:
emal.exe Org\RosettaCode\Modulinos.emal Who says the meaning of life is 42?
Erlang
Erlang has scripted main by default. scriptedmain.erl must be compiled before test.erl can access its functions.
Makefile:
all: t
t: scriptedmain.beam test.beam
erl -noshell -s scriptedmain
erl -noshell -s test
scriptedmain.beam: scriptedmain.erl
erlc scriptedmain.erl
test.beam: test.erl
erlc test.erl
clean:
-rm *.beam
scriptedmain.erl:
-module(scriptedmain).
-export([meaning_of_life/0, start/0]).
meaning_of_life() -> 42.
start() ->
io:format("Main: The meaning of life is ~w~n", [meaning_of_life()]),
init:stop().
test.erl:
-module(test).
-export([start/0]).
-import(scriptedmain, [meaning_of_life/0]).
start() ->
io:format("Test: The meaning of life is ~w~n", [meaning_of_life()]),
init:stop().
F#
Note 1: F# supports the scriptedmain behavior, but F# does not support hybrid script-compiled code files. The following programs work provided that they are compiled and then run, as .fs files, not interpreted or dotslashed as .fsx files.
Note 2: fsharpc has a backwards file ordering: Specify any dependencies BEFORE the code that depends on them.
Note 3: fsharpc also has that unpredictable DOS-flavored command line flag syntax, so the --out requires a colon between it and its value, and -h only generates an error; use --help instead.
Note 4: In Unix, mono is required to run F# executables. In Windows, mono is not required for execution.
Example:
$ make
fsharpc --out:scriptedmain.exe ScriptedMain.fs
fsharpc --out:test.exe ScriptedMain.fs Test.fs
$ mono scriptedmain.exe
Main: The meaning of life is 42
$ mono test.exe
Test: The meaning of life is 42
Makefile:
all: scriptedmain.exe test.exe
scriptedmain.exe: ScriptedMain.fs
fsharpc --nologo --out:scriptedmain.exe ScriptedMain.fs
test.exe: Test.fs ScriptedMain.fs
fsharpc --nologo --out:test.exe ScriptedMain.fs Test.fs
clean:
-rm *.exe
ScriptedMain.fs:
namespace ScriptedMain
module ScriptedMain =
let meaningOfLife = 42
let main =
printfn "Main: The meaning of life is %d" meaningOfLife
Test.fs:
module Test =
open ScriptedMain
let main =
printfn "Test: The meaning of life is %d" ScriptedMain.meaningOfLife
Factor
Note: The INCLUDE/INCLUDING macros must be added to the ~/.factor-rc configuration file.
Example:
$ ./scriptedmain.factor
Main: The meaning of life is 42
$ ./test.factor
Test: The meaning of life is 42
~/.factor-rc:
! INCLUDING macro that imports source code files in the current directory
USING: kernel vocabs.loader parser sequences lexer vocabs.parser ;
IN: syntax
: include-vocab ( vocab -- ) dup ".factor" append parse-file append use-vocab ;
SYNTAX: INCLUDING: ";" [ include-vocab ] each-token ;
scriptedmain.factor:
#! /usr/bin/env factor
USING: io math.parser ;
IN: scriptedmain
: meaning-of-life ( -- n ) 42 ;
: main ( -- ) meaning-of-life "Main: The meaning of life is " write number>string print ;
MAIN: main
test.factor:
#! /usr/bin/env factor
INCLUDING: scriptedmain ;
USING: io math.parser ;
IN: test
: main ( -- ) meaning-of-life "Test: The meaning of life is " write number>string print ;
MAIN: main
Forth
Given this awful running reference:
42 constant Douglas-Adams
: go ( -- )
." The meaning of life is " Douglas-Adams . cr ;
The bulk of Forth systems provide a way to generate an executable that enters GO (ar any word) on start.
' go 'MAIN !
program douglas-adams
Which creates a file named 'douglas-adams' that you can then run. If this is all in the same file, you can load the file, test parts of it, and then exit (or shell out) to run the executable.
A unix script requires that '#!' be a comment and that the system have some #!-compatible arguments.
#! /usr/bin/env gforth
42 constant Douglas-Adams
.( The meaning of life is ) Douglas-Adams . cr bye
Adding #! as a comment, as gforth does, is trivial. For a means by which this script could distinguish between 'scripted execution' and otherwise, a symlink like 'forthscript' could easily be used, and the zeroth OS argument tested for, but there's no convention.
#! /usr/bin/env forthscript
42 constant Douglas-Adams
s" forthscript" 0 arg compare 0= [IF]
.( The meaning of life is ) Douglas-Adams . cr bye
[THEN]
cr .( Why aren't you running this as a script? It only provides a constant.)
FreeBASIC
Function meaningoflife() As Byte
Dim As Byte y = 42
Return y
End Function
Sub main()
Print "Main: The meaning of life is "; meaningoflife()
End Sub
main()
Sleep
- Output:
Main: The meaning of life is 42
Go
Go doesn't support scripted main directly.
Although the examples linked to above include an example for Go, this is only a work around, not an emulation. To emulate a modulino, we need to proceed as in the [Executable library] task and split the 'main' package into two.
First create these two files in the 'modulino' directory:
// modulino.go
package main
import "fmt"
func MeaningOfLife() int {
return 42
}
func libMain() {
fmt.Println("The meaning of life is", MeaningOfLife())
}
// modulino_main.go
package main
func main() {
libMain()
}
To emulate a modulino:
- Output:
$ go run modulino The meaning of life is 42
Now create this file in the 'mol' directory:
// mol.go
package main
import "fmt"
func main() {
fmt.Println("The meaning of life is still", MeaningOfLife())
}
and copy modulino.go to the 'mol' directory. The library can then be used in the 'normal' way:
- Output:
$ go run mol The meaning of life is still 42
Groovy
Example:
$ ./ScriptedMain.groovy
Main: The meaning of life is 42
$ ./Test.groovy
Test: The meaning of life is 42
ScriptedMain.groovy:
#!/usr/bin/env groovy
class ScriptedMain {
static def meaningOfLife = 42
static main(args) {
println "Main: The meaning of life is " + meaningOfLife
}
}
Test.groovy:
#!/usr/bin/env groovy
println "Test: The meaning of life is " + ScriptedMain.meaningOfLife
Haskell
Haskell has scripted main, but getting scripted main to work with compiled scripts is tricky.
$ runhaskell scriptedmain.hs
Main: The meaning of life is 42
$ runhaskell test.hs
Test: The meaning of life is 42
$ ghc -fforce-recomp -o scriptedmain -main-is ScriptedMain scriptedmain.hs
$ ./scriptedmain
Main: The meaning of life is 42
$ ghc -fforce-recomp -o test -main-is Test test.hs scriptedmain.hs
$ ./test
Test: The meaning of life is 42
scriptedmain.hs
#!/usr/bin/env runhaskell
-- Compile:
--
-- ghc -fforce-recomp -o scriptedmain -main-is ScriptedMain scriptedmain.hs
module ScriptedMain where
meaningOfLife :: Int
meaningOfLife = 42
main :: IO ()
main = putStrLn $ "Main: The meaning of life is " ++ show meaningOfLife
test.hs
#!/usr/bin/env runhaskell
-- Compile:
--
-- ghc -fforce-recomp -o test -main-is Test test.hs scriptedmain.hs
module Test where
import ScriptedMain hiding (main)
main :: IO ()
main = putStrLn $ "Test: The meaning of life is " ++ show meaningOfLife
Io
ScriptedMain.io:
#!/usr/bin/env io
ScriptedMain := Object clone
ScriptedMain meaningOfLife := 42
if( isLaunchScript,
"Main: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println
)
test.io:
#!/usr/bin/env io
"Test: The meaning of life is #{ScriptedMain meaningOfLife}" interpolate println
$ ./ScriptedMain.io
Main: The meaning of life is 42
$ ./test.io
Test: The meaning of life is 42
J
modulinos.ijs:
#!/usr/bin/env ijconsole
meaningOfLife =: 42
main =: monad define
echo 'Main: The meaning of life is ',": meaningOfLife
exit ''
)
shouldrun =: monad define
if. 1 e. 'modulinos.ijs' E. ;ARGV do.
main 0
end.
)
shouldrun 0
test.j:
#!/usr/bin/env jconsole
load 'modulinos.ijs'
echo 'Test: The meaning of life is ',": meaningOfLife
exit ''
Example:
$ ./modulinos.ijs
Main: The meaning of life is 42
$ ./test.j
Test: The meaning of life is 42
Java
Java has scripted main by default.
ScriptedMain.java
public class ScriptedMain {
public static int meaningOfLife() {
return 42;
}
public static void main(String[] args) {
System.out.println("Main: The meaning of life is " + meaningOfLife());
}
}
Test.java
public class Test {
public static void main(String[] args) {
System.out.println("Test: The meaning of life is " + ScriptedMain.meaningOfLife());
}
}
JavaScript
Node.js has scripted main.
scriptedmain.js
#!/usr/bin/env node
function meaningOfLife() { return 42; }
exports.meaningOfLife = meaningOfLife;
function main() {
console.log("Main: The meaning of life is " + meaningOfLife());
}
if (!module.parent) { main(); }
test.js
#!/usr/bin/env node
var sm = require("./scriptedmain");
console.log("Test: The meaning of life is " + sm.meaningOfLife());
Julia
Julia does not use scripted main by default, but can be set to run as such. Modules generally use a /test unit test subdirectory instead.
In module file Divisors.jl:
module Divisors
using Primes
export properdivisors
function properdivisors(n::T) where T <: Integer
0 < n || throw(ArgumentError("number to be factored must be ≥ 0, got $n"))
1 < n || return T[]
!isprime(n) || return T[one(T), n]
f = factor(n)
d = T[one(T)]
for (k, v) in f
c = T[k^i for i in 0:v]
d = d*c'
d = reshape(d, length(d))
end
sort!(d)
return d[1:end-1]
end
function interactiveDivisors()
println("\nFind proper divisors between two numbers.\nFirst number: ")
lo = (x = tryparse(Int64, readline())) == nothing ? 0 : x
println("\nSecond number: ")
hi = (x = tryparse(Int64, readline())) == nothing ? 10 : x
lo, hi = lo > hi ? (hi, lo) : (lo, hi)
println("Listing the proper divisors for $lo through $hi.")
for i in lo:hi
println(lpad(i, 7), " => ", rpad(properdivisors(i), 10))
end
end
end
# some testing code
if occursin(r"divisors.jl"i, Base.PROGRAM_FILE)
println("This module is running as main.\n")
Divisors.interactiveDivisors()
end
In a user file getdivisors.jl:
include("divisors.jl")
using .Divisors
n = 708245926330
println("The proper divisors of $n are ", properdivisors(n))
LLVM
LLVM can have scripted main a la C, using the weak attribute.
$ make
llvm-as scriptedmain.ll
llc scriptedmain.bc
gcc -o scriptedmain scriptedmain.s
./scriptedmain
Main: The meaning of life is 42
llvm-as test.ll
llc test.bc
gcc -o test test.s scriptedmain.s
./test
Test: The meaning of life is 42
Makefile
EXECUTABLE_SM=scriptedmain
EXECUTABLE_TEST=test
all: test.ll scriptedmain.s
llvm-as test.ll
llc test.bc
gcc -o $(EXECUTABLE_TEST) test.s scriptedmain.s
./$(EXECUTABLE_TEST)
scriptedmain.s: scriptedmain.ll
llvm-as scriptedmain.ll
llc scriptedmain.bc
gcc -o $(EXECUTABLE_SM) scriptedmain.s
./$(EXECUTABLE_SM)
clean:
-rm $(EXECUTABLE_TEST)
-rm $(EXECUTABLE_SM)
-rm test.s
-rm test.bc
-rm scriptedmain.s
-rm scriptedmain.bc
scriptedmain.ll
@msg_main = internal constant [33 x i8] c"Main: The meaning of life is %d\0A\00"
declare i32 @printf(i8* noalias nocapture, ...)
define i32 @meaning_of_life() {
ret i32 42
}
define weak i32 @main(i32 %argc, i8** %argv) {
%meaning = call i32 @meaning_of_life()
call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([33 x i8]* @msg_main, i32 0, i32 0), i32 %meaning)
ret i32 0
}
test.ll
@msg_test = internal constant [33 x i8] c"Test: The meaning of life is %d\0A\00"
declare i32 @printf(i8* noalias nocapture, ...)
declare i32 @meaning_of_life()
define i32 @main(i32 %argc, i8** %argv) {
%meaning = call i32 @meaning_of_life()
call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([33 x i8]* @msg_test, i32 0, i32 0), i32 %meaning)
ret i32 0
}
Lua
Lua has scripted main by default because files are largely indistinguishable from functions semantically (they compile to Lua functions.) Ellipses is Lua's var-arg syntax for functions, and, therefore, for files as well.
scriptedmain.lua
#!/usr/bin/env lua
function meaningoflife()
return 42
end
function main(arg)
print("Main: The meaning of life is " .. meaningoflife())
end
if type(package.loaded[(...)]) ~= "userdata" then
main(arg)
else
module(..., package.seeall)
end
test.lua
#!/usr/bin/env lua
sm = require("scriptedmain")
print("Test: The meaning of life is " .. sm.meaningoflife())
Make
Example
$ make -f scriptedmain.mf
The meaning of life is 42
(Main)
$ make -f test.mf
The meaning of life is 42
(Test)
scriptedmain.mf
all: scriptedmain
meaning-of-life:
@echo "The meaning of life is 42"
scriptedmain: meaning-of-life
@echo "(Main)"
test.mf
all: test
test:
@make -f scriptedmain.mf meaning-of-life
@echo "(Test)"
Mathematica/Wolfram Language
scriptedmain.ma
#!/usr/bin/env MathKernel -script
MeaningOfLife[] = 42
ScriptName[] = Piecewise[
{
{"Interpreted", Position[$CommandLine, "-script", 1] == {}}
},
$CommandLine[[Position[$CommandLine, "-script", 1][[1,1]] + 1]]
]
Program = ScriptName[];
If[StringMatchQ[Program, ".*scriptedmain.*"],
Print["Main: The meaning of life is " <> ToString[MeaningOfLife[]]]
]
test.ma:
#!/usr/bin/env MathKernel -script
Get["scriptedmain.ma"]
Print["Test: The meaning of life is " <> ToString[MeaningOfLife[]]]
Example:
$ ./scriptedmain.ma
Main: The meaning of life is 42
$ ./test.ma
Test: The meaning of life is 42
In Mac and Windows, the output will be surrounded by spurious quotes.
Mozart/Oz
Makefile:
all: run
run: scriptedmain test
./scriptedmain
./test
scriptedmain: scriptedmain.oz
ozc -x scriptedmain.oz
scriptedmain.ozf: scriptedmain.oz
ozc -c scriptedmain.oz
test: scriptedmain.ozf test.oz
ozc -x test.oz
clean:
-rm test
-rm scriptedmain
-rm *.ozf
-rm *.exe
scriptedmain.oz:
functor
export
meaningOfLife: MeaningOfLife
import
System
Application
Property
Regex at 'x-oz://contrib/regex'
define
fun {MeaningOfLife} 42 end
local ScriptName = {Property.get 'application.url'} in
if {Regex.search "scriptedmain" ScriptName} \= false then
{System.printInfo "Main: The meaning of life is "#{Int.toString {MeaningOfLife}}#"\n"}
{Application.exit 0}
end
end
end
test.oz:
functor
import
ScriptedMain
System
Application
Property
Regex at 'x-oz://contrib/regex'
define
local ScriptName = {Property.get 'application.url'} in
if {Regex.search "test" ScriptName} \= false then
{System.printInfo "Test: The meaning of life is "#{Int.toString {ScriptedMain.meaningOfLife}}#"\n"}
{Application.exit 0}
end
end
end
newLISP
newLISP lacks scripted main, but the feature is easily added.
scriptedmain.lsp
#!/usr/bin/env newlisp
(context 'SM)
(define (SM:meaning-of-life) 42)
(define (main)
(println (format "Main: The meaning of life is %d" (meaning-of-life)))
(exit))
(if (find "scriptedmain" (main-args 1)) (main))
(context MAIN)
test.lsp
#!/usr/bin/env newlisp
(load "scriptedmain.lsp")
(println (format "Test: The meaning of life is %d" (SM:meaning-of-life)))
(exit)
Nim
Nim provides the predicate isMainModule
to use with conditional compilation. Here is an example:
proc p*() =
## Some exported procedure.
echo "Executing procedure"
# Some code to execute to initialize the module.
echo "Initializing the module"
when isMainModule:
# Some code to execute if the module is run directly, for instance code to test the module.
echo "Running tests"
- Output:
When run directly, the result of execution is:
Initializing the module Running tests
If we call “p” from another module, we get:
Initializing the module Executing procedure
Objective-C
scriptedmain.h:
#import <objc/Object.h>
@interface ScriptedMain: Object {}
+ (int)meaningOfLife;
@end
scriptedmain.m:
#import "scriptedmain.h"
#import <Foundation/Foundation.h>
@implementation ScriptedMain
+ (int)meaningOfLife {
return 42;
}
@end
int __attribute__((weak)) main(int argc, char **argv) {
@autoreleasepool {
printf("Main: The meaning of life is %d\n", [ScriptedMain meaningOfLife]);
}
return 0;
}
test.m:
#import "scriptedmain.h"
#import <Foundation/Foundation.h>
int main(int argc, char **argv) {
@autoreleasepool {
printf("Test: The meaning of life is %d\n", [ScriptedMain meaningOfLife]);
}
return 0;
}
$ gcc -o scriptedmain -lobjc -framework foundation scriptedmain.m
$ gcc -o test -lobjc -framework foundation test.m scriptedmain.m
$ ./scriptedmain
Main: The meaning of life is 42
$ ./test
Test: The meaning of life is 42
OCaml
scriptedmain.ml
let meaning_of_life = 42
let main () =
Printf.printf "Main: The meaning of life is %d\n"
meaning_of_life
let () =
if not !Sys.interactive then
main ()
Invoked as a script:
$ ocaml scriptedmain.ml
Main: The meaning of life is 42
Loaded into an ocaml toplevel/utop:
$ ocaml
...
# #use "scriptedmain.ml";;
val meaning_of_life : int = 42
val main : unit -> unit = <fun>
# meaning_of_life;;
- : int = 42
#
The limit of this technique is "avoiding running something when loading a script interactively". It's not applicable to other uses, like adding an example script to a file normally used as a library, as that code will also fire when users of the library are run.
Octave/MATLAB
Octave and MATLAB have scripted main by default, because only the first function listed in a program are importable by other programs.
meaningoflife.m
#!/usr/bin/env octave -qf
function y = meaningoflife()
y = 42;
endfunction
function main()
printf("Main: The meaning of life is %d", meaningoflife());
endfunction
main();
test.m
#!/usr/bin/env octave -qf
printf("Test: The meaning of life is %d", meaningoflife());
Pascal
Makefile:
all: scriptedmain
scriptedmain: scriptedmain.pas
fpc -dscriptedmain scriptedmain.pas
test: test.pas scriptedmain
fpc test.pas
clean:
-rm test
-rm scriptedmain
-rm *.o
-rm *.ppu
scriptedmain.pas:
{$IFDEF scriptedmain}
program ScriptedMain;
{$ELSE}
unit ScriptedMain;
interface
function MeaningOfLife () : integer;
implementation
{$ENDIF}
function MeaningOfLife () : integer;
begin
MeaningOfLife := 42
end;
{$IFDEF scriptedmain}
begin
write('Main: The meaning of life is: ');
writeln(MeaningOfLife())
{$ENDIF}
end.
test.pas:
program Test;
uses
ScriptedMain;
begin
write('Test: The meaning of life is: ');
writeln(MeaningOfLife())
end.
Example:
$ make
$ ./scriptedmain
Main: The meaning of life is: 42
$ make test
$ ./test
Test: The meaning of life is: 42
Perl
Perl has scripted main. The code inside unless(caller) { ... } only runs when Life.pm is the main program.
#!/usr/bin/env perl
# Life.pm
package Life;
use strict;
use warnings;
sub meaning_of_life {
return 42;
}
unless(caller) {
print "Main: The meaning of life is " . meaning_of_life() . "\n";
}
#!/usr/bin/env perl
# death.pl
use strict;
use warnings;
use Life;
print "Life means " . Life::meaning_of_life . ".\n";
print "Death means invisible scary skeletons.\n";
Phix
There is a builtin for this, which can even be asked to skip an arbitrary number of stack frames and that way find out exactly where it was effectively called from.
without js -- (includefile) string mori = iff(include_file()=1?"main":"an include")
PHP
PHP does not have scripted main, but the feature is easily added with a regular expression.
scriptedmain.php
<?php
function meaning_of_life() {
return 42;
}
function main($args) {
echo "Main: The meaning of life is " . meaning_of_life() . "\n";
}
if (preg_match("/scriptedmain/", $_SERVER["SCRIPT_NAME"])) {
main($argv);
}
?>
test.php
<?php
require_once("scriptedmain.php");
echo "Test: The meaning of life is " . meaning_of_life() . "\n";
?>
PicoLisp
PicoLisp normally does it the other way round: It calls main from the command line with the '-' syntax if desired. Create an executable file (chmod +x) "life.l":
#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
(de meaningOfLife ()
42 )
(de lifemain ()
(prinl "Main: The meaning of life is " (meaningOfLife))
(bye) )
and an executable file (chmod +x) "test.l":
#!/usr/bin/picolisp /usr/lib/picolisp/lib.l
(load "life.l")
(prinl "Test: The meaning of life is " (meaningOfLife))
(bye)
Test:
$ ./life.l -lifemain Main: The meaning of life is 42 $ ./test.l Test: The meaning of life is 42
Python
Python has scripted main.
#!/usr/bin/env python
# life.py
def meaning_of_life():
return 42
if __name__ == "__main__":
print("Main: The meaning of life is %s" % meaning_of_life())
#!/usr/bin/env python
# death.py
from life import meaning_of_life
print("Life means %s." % meaning_of_life())
print("Death means invisible scary skeletons.")
R
A way to check if code is running at "top level" is to check length(sys.frames())
. This value will be zero for a file being run with Rscript
, the --file=
argument, or at the command line, and will be greater than 0 in all other conditions (such as package loading or code being sourced from another file.)
#!/usr/bin/env Rscript
meaningOfLife <- function() {
42
}
main <- function(args) {
cat("Main: The meaning of life is", meaningOfLife(), "\n")
}
if (length(sys.frames()) > 0) {
args <- commandArgs(trailingOnly = FALSE)
main(args)
q("no")
}
test.R
#!/usr/bin/env Rscript
source("scriptedmain.R")
cat("Test: The meaning of life is", meaningOfLife(), "\n")
q("no")
Racket
scriptedmain.rkt:
#!/usr/bin/env racket
#lang racket
(provide meaning-of-life)
(define (meaning-of-life) 42)
(module+ main (printf "Main: The meaning of life is ~a\n" (meaning-of-life)))
test.rkt:
#!/usr/bin/env racket
#lang racket
(module+ main
(require "scriptedmain.rkt")
(printf "Test: The meaning of life is ~a\n" (meaning-of-life)))
Raku
(formerly Perl 6) Raku automatically calls MAIN on direct invocation, but this may be a multi dispatch, so a library may have multiple "scripted mains".
class LUE {
has $.answer = 42;
}
multi MAIN ('test') {
say "ok" if LUE.new.answer == 42;
}
multi MAIN ('methods') {
say ~LUE.^methods;
}
REXX
/*REXX program detects whether or not it is a "scripted main" program. */
parse source . howInvoked @fn /*query REXX how this pgm got invoked. */
say 'This program ('@fn") was invoked as a: " howInvoked
if howInvoked\=='COMMAND' then do
say 'This program ('@fn") wasn't invoked via a command."
exit 12
end
/*╔════════════════════════════════════════════════════════════════════════════════╗
║ At this point, we know that this program was invoked via the "command line" ║
║ or a program using the "command interface" and not via another program. ║
╚════════════════════════════════════════════════════════════════════════════════╝*/
/*────────────────────────────── The main code follows here ... ────────────────────────*/
say
say '(from' @fn"): and away we go ···"
Ring
# Project : Modulinos
func meaningoflife()
y = 42
return y
func main()
see "Main: The meaning of life is " + meaningoflife() + nl
Output:
Main: The meaning of life is 42
Ruby
Ruby has scripted main.
# life.rb
def meaning_of_life
42
end
if __FILE__ == $0
puts "Main: The meaning of life is #{meaning_of_life}"
end
# death.rb
require 'life'
puts "Life means #{meaning_of_life}."
puts "Death means invisible scary skeletons."
Rust
Note: this code is deprecated, and does not compile with Rust 1.0.0 and newer.
Makefile:
all: scriptedmain
scriptedmain: scriptedmain.rs
rustc scriptedmain.rs
test: test.rs scriptedmain.rs
rustc --lib scriptedmain.rs
rustc test.rs -L .
clean:
-rm test
-rm -rf *.dylib
-rm scriptedmain
-rm -rf *.dSYM
scriptedmain.rs:
#[link(name = "scriptedmain")];
use std;
fn meaning_of_life() -> int {
ret 42;
}
fn main() {
std::io::println("Main: The meaning of life is " + core::int::to_str(meaning_of_life(), 10u));
}
test.rs:
use scriptedmain;
use std;
fn main() {
std::io::println("Test: The meaning of life is " + core::int::to_str(scriptedmain::meaning_of_life(), 10u));
}
Example:
$ make
$ make test
$ ./scriptedmain
Main: The meaning of life is 42
$ ./test
Test: The meaning of life is 42
SAC
Makefile:
all: scriptedmain
scriptedmain: ScriptedMain.sac
sac2c -o scriptedmain ScriptedMain.sac -Dscriptedmain
test: test.sac ScriptedMain.sac
sac2c ScriptedMain.sac
sac2c -o test test.sac
clean:
-rm test
-rm test.c
-rm libScriptedMainTree.so
-rm libScriptedMainMod.so
-rm libScriptedMainMod.a
-rm scriptedmain
-rm scriptedmain.c
ScriptedMain.sac:
#ifndef scriptedmain
module ScriptedMain;
#endif
use StdIO: all;
use Array: all;
export all;
int meaning_of_life() {
return(42);
}
#ifdef scriptedmain
int main() {
printf("Main: The meaning of life is %d\n", meaning_of_life());
return(0);
}
#endif
test.sac:
use StdIO: all;
use Array: all;
use ScriptedMain: all;
int main() {
printf("Test: The meaning of life is %d\n", meaning_of_life());
return(0);
}
Example:
$ make
$ make test
$ ./scriptedmain
Main: The meaning of life is 42
$ ./test
Test: The meaning of life is 42
Scala
Unix shell script
This code must be stored as a shell script.
#!/bin/sh
exec scala "$0" "$@"
!#
def hailstone(n: Int): Stream[Int] =
n #:: (if (n == 1) Stream.empty else hailstone(if (n % 2 == 0) n / 2 else n * 3 + 1))
val nr = argv.headOption.map(_.toInt).getOrElse(27)
val collatz = hailstone(nr)
println(s"Use the routine to show that the hailstone sequence for the number: $nr.")
println(collatz.toList)
println(s"It has ${collatz.length} elements.")
Windows Command Script
This code must be stored as a Windows Command Script e.g. Hailstone.cmd
::#!
@echo off
call scala %0 %*
pause
goto :eof
::!#
def hailstone(n: Int): Stream[Int] =
n #:: (if (n == 1) Stream.empty else hailstone(if (n % 2 == 0) n / 2 else n * 3 + 1))
val nr = argv.headOption.map(_.toInt).getOrElse(27)
val collatz = hailstone(nr)
println(s"Use the routine to show that the hailstone sequence for the number: $nr.")
println(collatz.toList)
println(s"It has ${collatz.length} elements.")
- Output:
C:\>Hailstone.cmd 42 Use the routine to show that the hailstone sequence for the number: 42. List(42, 21, 64, 32, 16, 8, 4, 2, 1) It has 9 elements.
Scheme
Chicken Scheme has the {{{ -ss }}} flag for the interpreter, but compiled Chicken Scheme programs do not have scripted main unless the behavior is added manually to the code.
scriptedmain.scm
#!/bin/sh
#|
exec csi -ss $0 ${1+"$@"}
exit
|#
(use posix)
(require-extension srfi-1) ; lists
(define (meaning-of-life) 42)
(define (main args)
(display (format "Main: The meaning of life is ~a\n" (meaning-of-life)))
(exit))
(define (program)
(if (string=? (car (argv)) "csi")
(let ((s-index (list-index (lambda (x) (string-contains x "-s")) (argv))))
(if (number? s-index)
(cons 'interpreted (list-ref (argv) (+ 1 s-index)))
(cons 'unknown "")))
(cons 'compiled (car (argv)))))
(if (equal? (car (program)) 'compiled)
(main (cdr (argv))))
test.scm
#!/bin/sh
#|
exec csi -ss $0 ${1+"$@"}
exit
|#
(define (main args)
(load "scriptedmain.scm")
(display (format "Test: The meaning of life is ~a\n" (meaning-of-life)))
(exit))
Sidef
# Life.sm
func meaning_of_life {
42
}
if (__FILE__ == __MAIN__) {
say "Main: The meaning of life is #{meaning_of_life()}"
}
# test.sf
include Life
say "Test: The meaning of life is #{Life::meaning_of_life()}."
Smalltalk
Note that the ScriptedMain package must be installed in order for test.st to access code from scriptedmain.st.
Example
$ gst-package -t ~/.st package.xml &>/dev/null
$ ./scriptedmain.st
Main: The meaning of life is 42
$ ./test.st
Test: The meaning of life is 42
package.xml
<packages>
<package>
<name>ScriptedMain</name>
<filein>scriptedmain.st</filein>
<file>scriptedmain.st</file>
</package>
</packages>
scriptedmain.st
"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
Object subclass: ScriptedMain [
ScriptedMain class >> meaningOfLife [ ^42 ]
]
| main |
main := [
Transcript show: 'Main: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.
].
(((Smalltalk getArgc) > 0) and: [ ((Smalltalk getArgv: 1) endsWith: 'scriptedmain.st') ]) ifTrue: [
main value.
].
test.st
"exec" "gst" "-f" "$0" "$0" "$@"
"exit"
"
PackageLoader fileInPackage: 'ScriptedMain'.
Transcript show: 'Test: The meaning of life is ', ((ScriptedMain meaningOfLife) printString); cr.
Swift
Swift requires a number of hacks and boilerplate, but it is possible to write a modulino nevertheless.
Example
$ make
mkdir -p bin/
swiftc -D SCRIPTEDMAIN -o bin/ScriptedMain ScriptedMain.swift
swiftc -emit-library -module-name ScriptedMain -emit-module ScriptedMain.swift
mkdir -p bin/
swiftc -D TEST -o bin/Test Test.swift -I "." -L "." -lScriptedMain -module-link-name ScriptedMain
bin/ScriptedMain
Main: The meaning of life is 42
bin/Test
Test: The meaning of life is 42
Makefile
all: bin/ScriptedMain bin/Test
bin/ScriptedMain
bin/Test
bin/ScriptedMain: ScriptedMain.swift
mkdir -p bin/
swiftc -D SCRIPTEDMAIN -o bin/ScriptedMain ScriptedMain.swift
ScriptedMain.swiftmodule: ScriptedMain.swift
swiftc -emit-library -module-name ScriptedMain -emit-module ScriptedMain.swift
bin/Test: Test.swift ScriptedMain.swiftmodule
mkdir -p bin/
swiftc -D TEST -o bin/Test Test.swift -I "." -L "." -lScriptedMain -module-link-name ScriptedMain
clean:
-rm -rf bin/
-rm *.swiftmodule
-rm *.swiftdoc
-rm *.dylib
ScriptedMain.swift
import Foundation
public class ScriptedMain {
public var meaningOfLife = 42
public init() {}
public class func main() {
var meaning = ScriptedMain().meaningOfLife
println("Main: The meaning of life is \(meaning)")
}
}
#if SCRIPTEDMAIN
@objc class ScriptedMainAutoload {
@objc class func load() {
ScriptedMain.main()
}
}
#endif
Test.swift
import Foundation
import ScriptedMain
public class Test {
public class func main() {
var meaning = ScriptedMain().meaningOfLife
println("Test: The meaning of life is \(meaning)")
}
}
#if TEST
@objc class TestAutoload {
@objc class func load() {
Test.main()
}
}
#endif
Tcl
proc main {args} {
puts "Directory: [pwd]"
puts "Program: $::argv0"
puts "Number of args: [llength $args]"
foreach arg $args {puts "Arg: $arg"}
}
if {$::argv0 eq [info script]} {
main {*}$::argv
}
UNIX Shell
Bash has scripted main.
scriptedmain.sh
#!/usr/bin/env sh
meaning_of_life() {
return 42
}
main() {
meaning_of_life
echo "Main: The meaning of life is $?"
}
if [[ "$BASH_SOURCE" == "$0" ]]
then
main
fi
test.sh
#!/bin/bash
path=$(dirname -- "$0")
source "$path/scriptedmain"
meaning_of_life
echo "Test: The meaning of life is $?"
Wren
As far as Wren is concerned, a modulino and an executable library seem to be different names for the same thing. This therefore uses the same technique as the Executable_library#Wren task to create a simple modulino.
Note that Wren doesn't need or normally use a main() function to start a script, though we use one here to make the example clearer.
First we create a module for our modulino:
/* Modulinos.wren */
var MeaningOfLife = Fn.new { 42 }
var main = Fn.new {
System.print("The meaning of life is %(MeaningOfLife.call()).")
}
// Check if it's being used as a library or not.
import "os" for Process
if (Process.allArguments[1] == "Modulinos.wren") { // if true, not a library
main.call()
}
and run it to make sure it works OK when run directly:
- Output:
The meaning of life is 42.
Next we create another module which imports the modulino:
/* Modulinos_main.wren */
import "./Modulinos" for MeaningOfLife
var main = Fn.new {
System.print("Who says the meaning of life is %(MeaningOfLife.call())?")
}
main.call()
and run this to make sure the modulino's main() function doesn't run:
- Output:
Who says the meaning of life is 42?
ZX Spectrum Basic
On the ZX Spectrum, there is no main function as such, however a saved program can be made to start running from a particular line number by providing the line number as a parameter to save command. If the program is being merged as a module, then it does not run automatically. The following example will save the program in memory so that it starts running from line 500:
SAVE "MYPROG" LINE 500: REM For a program with main code starting at line 500
- Draft Programming Tasks
- Basic language learning
- 11l
- AppleScript
- Arturo
- C
- C++
- Clojure
- CoffeeScript
- Common Lisp
- D
- Dart
- Emacs Lisp
- EMal
- Erlang
- F Sharp
- Factor
- Forth
- FreeBASIC
- Go
- Groovy
- Haskell
- Io
- J
- Java
- JavaScript
- Julia
- LLVM
- Lua
- Make
- Mathematica
- Wolfram Language
- Mozart/Oz
- NewLISP
- Nim
- Objective-C
- OCaml
- Octave
- MATLAB
- Pascal
- Perl
- Phix
- PHP
- PicoLisp
- Python
- R
- Racket
- Raku
- REXX
- Ring
- Ruby
- Rust
- SAC
- Scala
- Scheme
- Sidef
- Smalltalk
- Swift
- Tcl
- UNIX Shell
- Wren
- ZX Spectrum Basic
- Ada/Omit
- Pages with too many expensive parser function calls