Create an executable for a program in an interpreted language: Difference between revisions

(→‎{{header|jq}}: whoops - out of order)
Tag: Manual revert
 
(8 intermediate revisions by 5 users not shown)
Line 9:
<br><br>
So, instead of supplying the source for the program, we just want to produce an executable for that specific program. The purpose of this task is to show how it could be done for your language.
<br>
<b>Note: This task is about creating a compiled executable.</b> Some operating systems allow a script to start with e.g. "#!" and the name of the interpreter to run it. This is <b>not</b>. what is reuired, as this would mean shipping the source.
<br><br>
One method of doing this would be to create a program in a language for which there is a compiler available (C for example) that contanss the source of the program to be interpreted, writes it to a temporary file and calls the interpreter to run it and then deletes the temporary file afterwards.
Line 354 ⟶ 356:
I think this task is a duplicate of another task. But it's also about the host operating system.
 
<syntaxhighlight lang="j">#!/usr/local/bin/jconsoleijconsole
echo 'hello world'
exit 0</syntaxhighlight>
Line 377 ⟶ 379:
 
(The "j engine" is a shared object / dylib / dll / ... the details depend on the host operating system.)
 
=={{header|jq}}==
{{incorrect||The task says "So, instead of supplying the source for the program, we just want to produce an executable for that specific program." - using #! requires the source be supplied.<br>Please look at some of the other samples.}}
This entry confines itself to the task (creating an executable) in computing environments that support the "shebang" technique.
A bash shell, for example, would suffice.
 
The trick to using jq in the shebang line is NOT to specify an argument for the -f option.
 
Assuming the jq program is on the PATH, a suitable shebang line for a script that reads from stdin would be:
 
<pre>
#!/usr/bin/env jq -f
</pre>
 
Alternatively, the full pathname can be specified, e.g. /usr/local/bin/jq -f
 
Other jq options can also be specified.
 
The file with the shebang line and program must of course be made executable (e.g. by running `chmod +x FILENAME`).
 
gojq, the Go implementation of jq, also supports the shebang technique, but in the case of gojq, the -f option should be specified last.
 
Variations and further details are given in the [https://github.com/stedolan/jq/wiki/FAQq%20FAQ jq FAQ].
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">
using StaticCompiler, StaticTools
 
hello() = println(c"Hello world!") # the c"" syntax defines a static C type string
compile_executable(hello, (), "./") # can now run this as executable "hello"
</syntaxhighlight>{{out}}
<pre>
$ ./hello
Hello world!
</pre>
 
=={{header|Nim}}==
Nim is normally a compiled language which can produce either native code using C, C++ or Objective C as intermediate language, or a JavaScript program. It is not a language designed to be interpreted, but there exists a powerful subset named Nimscript which is interpretable.
 
So we have chosen to write a Nim program which launch the “nim” program to evaluate a Nimscript program. There are two ways to ask “nim” to interpret: either using the “e” command rather than the “c” command or using the “--eval” option.
 
<syntaxhighlight lang="Nim"># Using the "e" command.
 
import std/os
 
const TempFile = "/tmp/hello.nim"
const Program = """echo "Hello World!""""
 
# Write program into temporary file.
let outFile = open(TempFile, fmWrite)
outFile.writeLine Program
outFile.close()
 
# Lauch "nim" to execute the program.
# "--hints:off" suppresses the hint messages.
discard execShellCmd("nim e --hints:off " & TempFile)
 
# Remove temporary file.
removeFile(TempFile)
</syntaxhighlight>
 
<syntaxhighlight lang="Nim"># Using the '--eval" option.
 
import std/[os, strutils]
 
const Program = """echo "Hello World!""""
 
# As the program is provided in the command line, there is no need
# to create a temporary file.
 
# Lauch "nim" to execute the program.
# "--hints:off" suppresses the hint messages.
discard execShellCmd("""nim --hints:off --eval:'$#'""" % Program)
</syntaxhighlight>
 
{{out}}
<pre>Hello World!</pre>
 
=={{header|Phix}}==
4,102

edits