Compile backtick commands as __command() calls with ${} interpolation

Backtick expressions now compile as calls to a global __command()
function instead of being treated as plain strings. Interpolation
via ${expr} is supported, with each interpolated value wrapped in
tostring() for type safety. Commands are parsed in primaryexp so
suffix operations like `.stdout` work directly.

Adds a stub __command that returns {code, stdout, stderr} for testing
until the real implementation (issue #03).
This commit is contained in:
Cormac Shannon
2026-02-19 20:00:15 +00:00
parent db6f39a2a4
commit a773398cab
4 changed files with 239 additions and 5 deletions

28
linit.c
View File

@@ -40,6 +40,33 @@ static const luaL_Reg stdlibs[] = {
};
/*
** Stub __command function for backtick command syntax.
** Returns a table {code=0, stdout=cmdstring, stderr=""}.
** The real implementation will be provided by issue #03.
*/
static int luaB_command (lua_State *L) {
const char *cmd = luaL_checkstring(L, 1);
lua_createtable(L, 0, 3);
lua_pushinteger(L, 0);
lua_setfield(L, -2, "code");
lua_pushstring(L, cmd);
lua_setfield(L, -2, "stdout");
lua_pushliteral(L, "");
lua_setfield(L, -2, "stderr");
return 1;
}
/*
** Register shell built-in globals (called after standard libraries).
*/
static void opencommand (lua_State *L) {
lua_pushcfunction(L, luaB_command);
lua_setglobal(L, "__command");
}
/*
** require and preload selected standard libraries
*/
@@ -59,5 +86,6 @@ LUALIB_API void luaL_openselectedlibs (lua_State *L, int load, int preload) {
}
lua_assert((mask >> 1) == LUA_UTF8LIBK);
lua_pop(L, 1); /* remove PRELOAD table */
opencommand(L); /* register __command global */
}