Create an executable for a program in an interpreted language: Difference between revisions
Create an executable for a program in an interpreted language (view source)
Revision as of 20:27, 10 July 2022
, 1 year ago→{{header|Wren}}: Now embeds the Wren source code in the C program itself.
(→{{header|Wren}}: Now embeds the Wren source code in the C program itself.) |
|||
Line 351:
=={{header|Wren}}==
Wren source code is always compiled first into an intermediate bytecode using a single-pass compiler which is part of its virtual machine (VM). The VM then interprets the bytecode at runtime with respect to the underlying platform which can be Linux, MacOS, Windows or (in theory at least) anything else for which a standard C99 compiler is available.
Line 360 ⟶ 358:
Wren is designed for embedding and, technically, even ''Wren-cli'' - which enables Wren scripts to be run from the command line - is just a host application for the Wren VM which uses the cross-platform library, ''libuv'', to provide additional functionality (mainly I/O) not provided by Wren's standard library itself.
If we don't need this additional functionality, we can instead write a simple C host, link it to the Wren VM library and then use the latter to compile and run
So
<lang C>#include <stdio.h>▼
▲#include <stdio.h>
#include "wren.h"
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
char *script = "for (i in 5..0) System.print(i)"; /* Wren source code */
WrenInterpretResult result = wrenInterpret(vm, module, script);
wrenFreeVM(vm);
return 0;
}</lang>
▲we can compile and run it and obtain the expected output (on Linux) as follows:
{{out}}
<pre>
$
$./countdown
5
4
|