Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,58 @@
|
|||
/* 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()
|
||||
|
|
@ -0,0 +1,144 @@
|
|||
#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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue