Terminal control/Positional read: Difference between revisions

Added Wren
(Added Wren)
Line 536:
* An <code>ptr-out</code> to an <code>array</code> of 1 <code>DWORD</code> is used for the number of characters out parameter. The FFI type <code>(ptr-out DWORD)</code> cannot work as a function argument, because integer objects are not mutable, and there isn't any concept of taking the address of a variable. A vector of 1 integer is mutable, and by making such a vector correspond with the FFI type <code>(array 1 DWORD)</code>, the necessary semantics is achieved.
* The quasiquote expression <code>^#S(COORD X ,(+ 2 cinfo.srWindow.Left) Y ,(+ 5 cinfo.srWindow.Top))</code> is equivalent to <code>(new COORD X (+ 2 cinfo.srWindow.Left) Y (+ 5 cinfo.srWindow.Top))</code>. It is done this way to demonstrate support for structure quasiquoting.
 
=={{header|Wren}}==
{{trans|Raku}}
{{libheader|ncurses}}
An embedded program so we can ask the C host to communicate with ncurses for us.
<lang ecmascript>/* terminal_control_positional_read.wren */
 
import "random" for Random
 
foreign class Window {
construct initscr() {}
 
foreign addstr(str)
 
foreign inch(y, x)
 
foreign move(y, x)
 
foreign refresh()
 
foreign getch()
 
foreign delwin()
}
 
class Ncurses {
foreign static endwin()
}
 
// initialize curses window
var win = Window.initscr()
if (win == 0) {
System.print("Failed to initialize ncurses.")
return
}
 
// print random text in a 10x10 grid
var rand = Random.new()
for (row in 0..9) {
var line = (0..9).map{ |d| String.fromByte(rand.int(41, 91)) }.join()
win.addstr(line + "\n")
}
 
// read
var col = 3 - 1
var row = 6 - 1
var ch = win.inch(row, col)
 
// show result
win.move(row, col + 10)
win.addstr("Character at column 3, row 6 = %(ch)")
win.move(11, 0)
win.addstr("Press any key to exit...")
 
// refresh
win.refresh()
 
// wait for a keypress
win.getch()
 
// clean-up
win.delwin()
Ncurses.endwin()</lang>
<br>
We now embed this in the following C program, compile and run it.
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include "wren.h"
 
/* C <=> Wren interface functions */
 
void C_windowAllocate(WrenVM* vm) {
WINDOW** pwin = (WINDOW**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(WINDOW*));
*pwin = initscr();
}
 
void C_addstr(WrenVM* vm) {
WINDOW* win = *(WINDOW**)wrenGetSlotForeign(vm, 0);
const char *str = wrenGetSlotString(vm, 1);
waddstr(win, str);
}
 
void C_inch(WrenVM* vm) {
WINDOW* win = *(WINDOW**)wrenGetSlotForeign(vm, 0);
int y = (int)wrenGetSlotDouble(vm, 1);
int x = (int)wrenGetSlotDouble(vm, 2);
char c = (char)mvwinch(win, y, x);
char s[2] = "\0";
sprintf(s, "%c", c);
wrenSetSlotString(vm, 0, s);
}
 
void C_move(WrenVM* vm) {
WINDOW* win = *(WINDOW**)wrenGetSlotForeign(vm, 0);
int y = (int)wrenGetSlotDouble(vm, 1);
int x = (int)wrenGetSlotDouble(vm, 2);
wmove(win, y, x);
}
 
void C_refresh(WrenVM* vm) {
WINDOW* win = *(WINDOW**)wrenGetSlotForeign(vm, 0);
wrefresh(win);
}
 
void C_getch(WrenVM* vm) {
WINDOW* win = *(WINDOW**)wrenGetSlotForeign(vm, 0);
wgetch(win);
}
 
void C_delwin(WrenVM* vm) {
WINDOW* win = *(WINDOW**)wrenGetSlotForeign(vm, 0);
delwin(win);
}
 
void C_endwin(WrenVM* vm) {
endwin();
}
 
WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
WrenForeignClassMethods methods;
methods.allocate = NULL;
methods.finalize = NULL;
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Window") == 0) {
methods.allocate = C_windowAllocate;
}
}
return methods;
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Window") == 0) {
if (!isStatic && strcmp(signature, "addstr(_)") == 0) return C_addstr;
if (!isStatic && strcmp(signature, "inch(_,_)") == 0) return C_inch;
if (!isStatic && strcmp(signature, "move(_,_)") == 0) return C_move;
if (!isStatic && strcmp(signature, "refresh()") == 0) return C_refresh;
if (!isStatic && strcmp(signature, "getch()") == 0) return C_getch;
if (!isStatic && strcmp(signature, "delwin()") == 0) return C_delwin;
} else if (strcmp(className, "Ncurses") == 0) {
if ( isStatic && strcmp(signature, "endwin()") == 0) return C_endwin;
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
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.errorFn = &errorFn;
config.bindForeignClassFn = &bindForeignClass;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "terminal_control_positional_read.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</lang>
 
{{out}}
Sample output:
<pre>
86.Z=B>)0I
R/X,HX=6RE
>*12?I-G0+
D*8S-2A.)3
9)+=89UNXW
YQN4L8NC4W Character at column 3, row 6 = N
6EQC@=))B/
XWOSZ4/CR@
LU->=2@RW1
ZKFRC9EOT0
 
Press any key to exit...
</pre>
 
=={{header|XPL0}}==
9,479

edits