Create an executable for a program in an interpreted language: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added format directive note) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 21: | Line 21: | ||
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: |
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: |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <errno.h> |
#include <errno.h> |
||
static void w( char * line, FILE * tf ) |
static void w( char * line, FILE * tf ) |
||
Line 35: | Line 35: | ||
remove( "/tmp/t" ); |
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. |
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> |
<br> |
||
Line 52: | Line 52: | ||
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. |
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}} |
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
||
< |
<syntaxhighlight 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 # |
# a C program is created that writes the source to a temporary file and # |
||
# then calls a the Algol 68G interpreter to run it # |
# then calls a the Algol 68G interpreter to run it # |
||
Line 168: | Line 168: | ||
) |
) |
||
) |
) |
||
FI</ |
FI</syntaxhighlight> |
||
{{out}} |
{{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: |
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: | Line 183: | ||
If hw.a68 contains: |
If hw.a68 contains: |
||
< |
<syntaxhighlight lang="algol68">print( ( "Hello, World!", newline ) ) |
||
</syntaxhighlight> |
|||
</lang> |
|||
The generated _hw_a68.c would contain: |
The generated _hw_a68.c would contain: |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <errno.h> |
#include <errno.h> |
||
static void w( char * line, FILE * tf ) |
static void w( char * line, FILE * tf ) |
||
Line 200: | Line 200: | ||
system( "a68g /tmp/t" ); |
system( "a68g /tmp/t" ); |
||
remove( "/tmp/t" ); |
remove( "/tmp/t" ); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
{{Trans|ALGOL 68}} |
{{Trans|ALGOL 68}} |
||
< |
<syntaxhighlight 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 |
# a C program is created that writes the source to a temporary file and |
||
# then calls a the Awk interpreter to run it |
# then calls a the Awk interpreter to run it |
||
Line 309: | Line 309: | ||
return result; |
return result; |
||
} # readLine</ |
} # readLine</syntaxhighlight> |
||
{{out}} |
{{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: |
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: | Line 324: | ||
Assuming hw.awk contains: |
Assuming hw.awk contains: |
||
< |
<syntaxhighlight lang="awk">BEGIN \ |
||
{ |
{ |
||
printf( "Hello, World!\n" ); |
printf( "Hello, World!\n" ); |
||
} # BEGIN</ |
} # BEGIN</syntaxhighlight> |
||
_hw_awk.c would contani: |
_hw_awk.c would contani: |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <errno.h> |
#include <errno.h> |
||
static void w( char * line, FILE * tf ) |
static void w( char * line, FILE * tf ) |
||
Line 348: | Line 348: | ||
system( "awk -f /tmp/t" ); |
system( "awk -f /tmp/t" ); |
||
remove( "/tmp/t" ); |
remove( "/tmp/t" ); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|J}}== |
=={{header|J}}== |
||
Line 354: | Line 354: | ||
I think this task is a duplicate of another task. But it's also about the host operating system. |
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/jconsole |
||
echo 'hello world' |
echo 'hello world' |
||
exit 0</ |
exit 0</syntaxhighlight> |
||
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... |
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: | Line 364: | ||
Meanwhile, we could do something similar with compiled C. Here's a minimal example which builds and runs under Linux: |
Meanwhile, we could do something similar with compiled C. Here's a minimal example which builds and runs under Linux: |
||
< |
<syntaxhighlight lang="c">/* github jsoftware/jsource 903-release-b */ |
||
/* link with -ldl */ |
/* link with -ldl */ |
||
#include <dlfcn.h> |
#include <dlfcn.h> |
||
Line 374: | Line 374: | ||
jdo(jt, (C*)"('Hello, World!',10{a.) 1!:2<'/proc/self/fd/1'"); |
jdo(jt, (C*)"('Hello, World!',10{a.) 1!:2<'/proc/self/fd/1'"); |
||
exit(0); |
exit(0); |
||
}</ |
}</syntaxhighlight> |
||
(The "j engine" is a shared object / dylib / dll / ... the details depend on the host operating system.) |
(The "j engine" is a shared object / dylib / dll / ... the details depend on the host operating system.) |
||
Line 400: | Line 400: | ||
So the following C program (countdown.c) is perhaps the nearest we can get to the spirit of this task. |
So the following C program (countdown.c) is perhaps the nearest we can get to the spirit of this task. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include "wren.h" |
#include "wren.h" |
||
Line 417: | Line 417: | ||
wrenFreeVM(vm); |
wrenFreeVM(vm); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
We can now compile this code (using GCC on Linux) and run it, obtaining the expected output as follows: |
We can now compile this code (using GCC on Linux) and run it, obtaining the expected output as follows: |