Hello world/Line printer: Difference between revisions

Added Wren
(add line printer wisp example)
(Added Wren)
Line 1,025:
λ : printer
write "Hello World!" printer</lang>
 
=={{header|Wren}}==
It is not currently possible to communicate with the printer using Wren-cli. So we need to write a minimal embedded program (no error checking) so the C host can do this for us.
<lang ecmascript>/* hello_world_line_printer.wren */
 
class C {
foreign static lprint(s)
}
 
C.lprint("Hello World!")</lang>
<br>
We now embed this in the following C program, compile and run it.
<lang c>/* gcc hello_world_line_printer.c -o hello_world_line_printer -lwren -lm */
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "wren.h"
 
/* C <=> Wren interface functions */
 
void C_lprint(WrenVM* vm) {
const char *arg = wrenGetSlotString(vm, 1);
char command[strlen(arg) + 13];
strcpy(command, "echo \"");
strcat(command, arg);
strcat(command, "\" | lp");
system(command);
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "C") == 0) {
if (isStatic && strcmp(signature, "lprint(_)") == 0) return C_lprint;
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "hello_world_line_printer.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
wrenFreeVM(vm);
free(script);
return 0;
}</lang>
 
=={{header|X86 Assembly}}==
9,482

edits