|
Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
// (find-sfcfile "sfc-0.0.1-0.rockspec")
// (find-es "lua5" "lua-calls-C")
// (find-angg "peek/peek.c")
/*
* (find-angg "eev-puro/mini-lua-intro.e")
* (find-angg "DAVINCI/peek.c")
* (find-blogme3grep "grep -nH -e '\\.c$' anggmake.lua")
* (find-angg "peek/peek.c")
*/
#include <stdlib.h> // (find-man "3 malloc")
#include <string.h> // (find-man "3 memcpy")
#include <lua.h>
#include <lauxlib.h>
// (find-luamanualw3m "#luaL_checkint")
// (find-luamanualw3m "#luaL_checklstring")
// (find-luamanualw3m "#luaL_checkstring")
// (find-luamanualw3m "#lua_pushlstring")
// (find-luamanualw3m "#luaL_checklstring")
// (find-node "(libc)Function Index" "memcpy")
static int modal_interior(lua_State* L) { // algorithm: '0's propagate up
const char* arrows = luaL_checkstring(L, 1);
const char* P = luaL_checkstring(L, 2);
char Q [256];
int i, a, b;
strcpy(Q, P);
for(i=strlen(arrows)-2; i>=2; i-=3) {
a = arrows[i] - arrows[0];
b = arrows[i+1] - arrows[0];
if (Q[b]=='0')
Q[a]='0';
printf("a=%d b=%d\n", a, b);
}
lua_pushstring(L, (void *)Q);
return 1;
}
static int modal_ess(lua_State* L) { // algorithm: '1's propagate down as '2's
const char* arrows = luaL_checkstring(L, 1);
const char* P = luaL_checkstring(L, 2);
char Q [256];
int len = strlen(arrows);
int i, a, b;
strcpy(Q, P);
for(i=2; i<len; i+=3) {
a = arrows[i] - arrows[0];
b = arrows[i+1] - arrows[0];
if (Q[a]>='1')
Q[b] = '2';
// printf("a=%d b=%d\n", a, b);
}
for (i=0; Q[i]; ++i) // ...and we turn all '2's into '0's.
if (Q[i]=='2')
Q[i] = '0';
lua_pushstring(L, (void *)Q);
return 1;
}
static int modal_paint(lua_State* L) { // algorithm: '1's propagate down
const char* arrows = luaL_checkstring(L, 1);
const char* P = luaL_checkstring(L, 2);
char Q [256];
int len = strlen(arrows);
int i, a, b;
strcpy(Q, P);
for(i=2; i<len; i+=3) {
a = arrows[i] - arrows[0];
b = arrows[i+1] - arrows[0];
if (Q[a]=='1')
Q[b]='1';
// printf("a=%d b=%d\n", a, b);
}
lua_pushstring(L, (void *)Q);
return 1;
}
static int modal_and(lua_State* L) {
const char* P = luaL_checkstring(L, 1);
const char* Q = luaL_checkstring(L, 2);
char R [256];
int len = strlen(P);
int i;
strcpy(R, P);
for(i=0; i<len; ++i)
if (Q[i]=='0')
R[i] = '0';
lua_pushstring(L, (void *)R);
return 1;
}
static int modal_or(lua_State* L) {
const char* P = luaL_checkstring(L, 1);
const char* Q = luaL_checkstring(L, 2);
char R [256];
int len = strlen(P);
int i;
strcpy(R, P);
for(i=0; i<len; ++i)
if (Q[i]=='1')
R[i] = '1';
lua_pushstring(L, (void *)R);
return 1;
}
static int modal_imp(lua_State* L) {
const char* P = luaL_checkstring(L, 1);
const char* Q = luaL_checkstring(L, 2);
char R [256];
int len = strlen(P);
int i;
strcpy(R, Q);
for(i=0; i<len; ++i)
if (P[i]=='0')
R[i] = '1';
lua_pushstring(L, (void *)R);
return 1;
}
static int modal_not(lua_State* L) {
const char* P = luaL_checkstring(L, 1);
char R [256];
int len = strlen(P);
int i;
strcpy(R, P);
for(i=0; i<len; ++i)
R[i] = P[i]=='0' ? '1' : '0';
lua_pushstring(L, (void *)R);
return 1;
}
// (find-man "strcpy")
// (find-luamanualw3m "#luaL_checkint")
// (find-luamanualw3m "#lua_pushinteger")
// (find-node "(libc)Function Index" "* malloc")
// (find-node "(libc)Function Index" "* free")
// (find-luamanualw3m "#lua_topointer")
// (find-lua51grep "grep -nH -e lua_topointer src/*")
// (find-lua51srcfile "lapi.c" "lua_topointer")
// http://www.lua.org/source/5.1/lapi.c.html#lua_topointer
// (find-lua51srcfile "lua.h" "LUA_TFUNCTION")
// (find-lua51srcfile "lobject.h" "#define ttype")
// (find-luamanualw3m "#lua_register")
// (find-luamanualw3m "#pdf-package.loaders" "luaopen_")
// «init» (to ".init")
LUALIB_API int luaopen_modal(lua_State *L) {
lua_register(L, "modal_ess_", modal_ess);
lua_register(L, "modal_paint_", modal_paint);
lua_register(L, "modal_interior_", modal_interior);
lua_register(L, "modal_and_", modal_and);
lua_register(L, "modal_or_", modal_or);
lua_register(L, "modal_imp_", modal_imp);
lua_register(L, "modal_not_", modal_not);
return 0;
}
// (find-lua51file "")
// (find-lua51manualw3m "#lua_register")
// Local Variables:
// coding: raw-text-unix
// End: