Robots/Wren: Difference between revisions

m
Minor tidy
m (→‎Code: Added libheaders.)
m (Minor tidy)
 
(2 intermediate revisions by the same user not shown)
Line 1:
===Code===
{{trans|Go}}
{{libheader|ncurses}}
Line 7:
{{works with|Ubuntu 20.04}}
An embedded script so we can use the ncurses library.
<langsyntaxhighlight ecmascriptlang="wren">/* robotsRobots.wren */
 
import "./dynamic" for Struct
Line 17:
class NC {
foreign static initScr()
foreign static useDefaultColors()
foreign static startColor()
 
Line 225 ⟶ 226:
var play = Fn.new {
NC.initScr()
NC.useDefaultColors()
NC.startColor()
for (i in 0...colors.count) {
NC.initPair(i+1, colors[i], 0-1)
NC.initPair(i+11, colors[i], 2)
}
Line 296 ⟶ 298:
NC.echo()
NC.endwin()
NC.cursSet(1)</langsyntaxhighlight>
<br>
We now embed this in the following C program, build and run it.
<langsyntaxhighlight lang="c">/* gcc robotsRobots.c -o robotsRobots -lncurses -lwren -lm */
 
#include <stdio.h>
Line 312 ⟶ 314:
void C_initScr(WrenVM* vm) {
initscr();
}
 
void C_useDefaultColors(WrenVM* vm) {
use_default_colors();
}
 
Line 415 ⟶ 421:
if (strcmp(module, "main") == 0) {
if (strcmp(className, "NC") == 0) {
if (isStatic && strcmp(signature, "initScr()") == 0) return C_initScr;
if (isStatic && strcmp(signature, "startColoruseDefaultColors()") == 0) return C_startColorC_useDefaultColors;
if (isStatic && strcmp(signature, "cbreakstartColor()") == 0) return C_cbreakC_startColor;
if (isStatic && strcmp(signature, "nocbreakcbreak()") == 0) return C_nocbreakC_cbreak;
if (isStatic && strcmp(signature, "echonocbreak()") == 0) return C_echoC_nocbreak;
if (isStatic && strcmp(signature, "noechoecho()") == 0) return C_noechoC_echo;
if (isStatic && strcmp(signature, "cursSetnoecho(_)") == 0) return C_cursSetC_noecho;
if (isStatic && strcmp(signature, "initPaircursSet(_,_,_)") == 0) return C_initPairC_cursSet;
if (isStatic && strcmp(signature, "attroninitPair(_,_,_)") == 0) return C_attronC_initPair;
if (isStatic && strcmp(signature, "attroffattron(_)") == 0) return C_attroffC_attron;
if (isStatic && strcmp(signature, "moveattroff(_,_)") == 0) return C_moveC_attroff;
if (isStatic && strcmp(signature, "mvaddStrmove(_,_,_)") == 0) return C_mvaddStrC_move;
if (isStatic && strcmp(signature, "getchmvaddStr(_,_,_)") == 0) return C_getchC_mvaddStr;
if (isStatic && strcmp(signature, "refreshgetch()") == 0) return C_refreshC_getch;
if (isStatic && strcmp(signature, "napmsrefresh(_)") == 0) return C_napmsC_refresh;
if (isStatic && strcmp(signature, "endwinnapms(_)") == 0) return C_endwinC_napms;
if (isStatic && strcmp(signature, "endwin()") == 0) return C_endwin;
}
}
Line 491 ⟶ 498:
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "robotsRobots.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
Line 512 ⟶ 519:
free(script);
return 0;
}</langsyntaxhighlight>
9,476

edits