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

m (→‎{{header|Phix}}: added format directive note)
 
(11 intermediate revisions by 6 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 21 ⟶ 23:
 
then (assuming the I interpreter is invoked with the command "InterpretI" and a suitable file for the temporary source is /tmp/t) a suitable C source might be:
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <errno.h>
static void w( char * line, FILE * tf )
Line 35 ⟶ 37:
remove( "/tmp/t" );
}
</syntaxhighlight>
</lang>
If your language has other ways of doing this e.g., the interpreter provides an API that would let you specify the code via a string instead of storing it in a file, demonstrate how this would be done.
<br>
Line 52 ⟶ 54:
There have been several implementations of Algol 68, both compiled and interpreted. A popular implementation currently available for Windows and Linux is ALGOL 68G which is an interpreter (though under Linux a hybrid part compilation, part interpreted mode is available). This example follows the suggestion in the task description and creates a C source that will write the Algol 68 source and run the interpreter.
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<langsyntaxhighlight lang="algol68">IF # create an executable to run a program with Algol 68G #
# a C program is created that writes the source to a temporary file and #
# then calls a the Algol 68G interpreter to run it #
Line 168 ⟶ 170:
)
)
FI</langsyntaxhighlight>
{{out}}
Assuming /tmp/t is a temporary file that could be written to by the generated C program and that the command to run ALGOL 68G is a68g, a sample run of the program might be:
Line 183 ⟶ 185:
 
If hw.a68 contains:
<langsyntaxhighlight lang="algol68">print( ( "Hello, World!", newline ) )
</syntaxhighlight>
</lang>
 
The generated _hw_a68.c would contain:
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <errno.h>
static void w( char * line, FILE * tf )
Line 200 ⟶ 202:
system( "a68g /tmp/t" );
remove( "/tmp/t" );
}</langsyntaxhighlight>
 
=={{header|AWK}}==
{{Trans|ALGOL 68}}
<langsyntaxhighlight lang="awk"># create an executable to run a program with awk
# a C program is created that writes the source to a temporary file and
# then calls a the Awk interpreter to run it
Line 309 ⟶ 311:
 
return result;
} # readLine</langsyntaxhighlight>
{{out}}
Assuming /tmp/t is a temporary file that could be written to by the generated C program and that the command to run the awk interpregter is <code>awk -f</code>, a sample run of the program might be:
Line 324 ⟶ 326:
 
Assuming hw.awk contains:
<langsyntaxhighlight lang="awk">BEGIN \
{
 
printf( "Hello, World!\n" );
 
} # BEGIN</langsyntaxhighlight>
 
_hw_awk.c would contani:
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <errno.h>
static void w( char * line, FILE * tf )
Line 348 ⟶ 350:
system( "awk -f /tmp/t" );
remove( "/tmp/t" );
}</langsyntaxhighlight>
 
=={{header|J}}==
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</langsyntaxhighlight>
 
This is a basic example of how to implement a J shell script. This example assumes that J's jconsole was installed at /usr/local/bin/jconsole (note that java also has a jconsole which might be installed in /usr/bin/ -- so on debian and ubuntu which avoid such conflicts but which also do not install to /usr/local/bin the J implementation installs /usr/bin/ijconsole which is what would be needed in the hashbang line). Also, many J installs do not put j executables in a directory in $PATH. So the usual trick of using /usr/bin/env tends to not be useful both because the executable names and locations may vary. Conceptually this means that if you want to distribute a J program you probably need to also distribute a copy of J along with it. Fortunately, J is gpl'd and reasonably small, so that should not be too much of a burden. Also, unix executables must be made executable before they can run...
Line 364 ⟶ 366:
Meanwhile, we could do something similar with compiled C. Here's a minimal example which builds and runs under Linux:
 
<langsyntaxhighlight Clang="c">/* github jsoftware/jsource 903-release-b */
/* link with -ldl */
#include <dlfcn.h>
Line 374 ⟶ 376:
jdo(jt, (C*)"('Hello, World!',10{a.) 1!:2<'/proc/self/fd/1'");
exit(0);
}</langsyntaxhighlight>
 
(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
</langpre>
 
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!
</langpre>
 
=={{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}}==
Line 400 ⟶ 479:
So the following C program (countdown.c) is perhaps the nearest we can get to the spirit of this task.
 
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include "wren.h"
 
Line 417 ⟶ 496:
wrenFreeVM(vm);
return 0;
}</langsyntaxhighlight>
 
We can now compile this code (using GCC on Linux) and run it, obtaining the expected output as follows:
4,102

edits