issue #98 rename functions and start to change the api to construct AST as a userdata object

pull/167/head
Dibyendu Majumdar 6 years ago
parent 24ac6f29f3
commit c465af3465

@ -310,7 +310,7 @@ LUALIB_API void *raviL_checkudata(lua_State *L, int arg_index, const void *meta_
#define raviL_checkudata(L, arg_index, meta_key) luaL_checkudata(L, arg_index, meta_key)
#endif
LUALIB_API int (raviL_loadbufferx) (lua_State *L, const char *buff, size_t size,
LUALIB_API int (raviL_build_ast_from_buffer) (lua_State *L, const char *buff, size_t size,
const char *name, const char *mode);
LUALIB_API int (raviL_dumpast) (lua_State *L);

@ -56,7 +56,7 @@ LUAI_FUNC l_noret luaD_throw (lua_State *L, int errcode);
LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud);
LUAI_FUNC int raviD_protectedparser (lua_State *L, ZIO *z, const char *name,
LUAI_FUNC int raviD_protected_ast_builder (lua_State *L, ZIO *z, const char *name,
const char *mode);
#endif

@ -240,10 +240,9 @@ typedef struct FuncState {
LUAI_FUNC LClosure *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
Dyndata *dyd, const char *name, int firstchar);
LUAI_FUNC LClosure *raviY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
Dyndata *dyd, const char *name, int firstchar);
/** RAVI extensions **/
LUAI_FUNC int raviY_parse_to_ast(lua_State *L, ZIO *z, Mbuffer *buff,
const char *name, int firstchar);
LUAI_FUNC const char *raviY_typename(ravitype_t tt);
/* Special printf that recognises following conversions:

@ -531,7 +531,7 @@ LUA_API void *ravi_get_debugger_data(lua_State *L);
** Experimental (wip) implementation of new
** parser and code generator
*/
LUA_API int (ravi_load) (lua_State *L, lua_Reader reader, void *dt,
LUA_API int (ravi_build_ast_from_buffer) (lua_State *L, lua_Reader reader, void *dt,
const char *chunkname, const char *mode);

@ -1333,28 +1333,23 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data,
}
/*
** Experimental (wip) implementation of new
**
** Builds an Abstract Syntax Tree (encapsulated in userdata type) from the given
** input buffer. This function returns 0 if all OK
* - On success a userdata object representing the tree,
* else an error message will be pushed on to the stack
**
** This is part of the new experimental (wip) implementation of new
** parser and code generator
*/
LUA_API int (ravi_load) (lua_State *L, lua_Reader reader, void *data,
LUA_API int (ravi_build_ast_from_buffer) (lua_State *L, lua_Reader reader, void *data,
const char *chunkname, const char *mode) {
ZIO z;
int status;
lua_lock(L);
if (!chunkname) chunkname = "?";
luaZ_init(L, &z, reader, data);
status = raviD_protectedparser(L, &z, chunkname, mode);
if (status == LUA_OK) { /* no errors? */
LClosure *f = clLvalue(L->top - 1); /* get newly created function */
if (f->nupvalues >= 1) { /* does it have an upvalue? */
/* get global table from registry */
Table *reg = hvalue(&G(L)->l_registry);
const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
/* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
setobj(L, f->upvals[0]->v, gt);
luaC_upvalbarrier(L, f->upvals[0]);
}
}
status = raviD_protected_ast_builder(L, &z, chunkname, mode);
lua_unlock(L);
return status;
}

@ -760,12 +760,18 @@ LUALIB_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t size,
return lua_load(L, getS, &ls, name, mode);
}
LUALIB_API int raviL_loadbufferx (lua_State *L, const char *buff, size_t size,
/*
* Builds an Abstract Syntax Tree (encapsulated in userdata type) from the given
* input buffer. This function returns 0 if all OK
* - On success a userdata object representing the tree,
* else an error message will be pushed on to the stack
*/
LUALIB_API int raviL_build_ast_from_buffer (lua_State *L, const char *buff, size_t size,
const char *name, const char *mode) {
LoadS ls;
ls.s = buff;
ls.size = size;
return ravi_load(L, getS, &ls, name, mode);
return ravi_build_ast_from_buffer(L, getS, &ls, name, mode);
}
LUALIB_API int luaL_loadstring (lua_State *L, const char *s) {

@ -363,13 +363,13 @@ static int raviB_load(lua_State *L) {
int env = (!lua_isnone(L, 4) ? 4 : 0); /* 'env' index or 0 if no 'env' */
if (s != NULL) { /* loading a string? */
const char *chunkname = luaL_optstring(L, 2, s);
status = raviL_loadbufferx(L, s, l, chunkname, mode);
status = raviL_build_ast_from_buffer(L, s, l, chunkname, mode);
}
else { /* loading from a reader function */
const char *chunkname = luaL_optstring(L, 2, "=(load)");
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_settop(L, RESERVEDSLOT); /* create reserved slot */
status = ravi_load(L, generic_reader, NULL, chunkname, mode);
status = ravi_build_ast_from_buffer(L, generic_reader, NULL, chunkname, mode);
}
return load_aux(L, status, env);
}

@ -886,24 +886,15 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
return status;
}
static void ravi_f_parser (lua_State *L, void *ud) {
static int ravi_f_parser (lua_State *L, void *ud) {
LClosure *cl;
struct SParser *p = cast(struct SParser *, ud);
int c = zgetc(p->z); /* read first character */
if (c == LUA_SIGNATURE[0]) {
checkmode(L, p->mode, "binary");
cl = luaU_undump(L, p->z, p->name);
}
else {
checkmode(L, p->mode, "text");
cl = raviY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
}
lua_assert(cl->nupvalues == cl->p->sizeupvalues);
luaF_initupvals(L, cl);
checkmode(L, p->mode, "text");
return raviY_parse_to_ast(L, p->z, &p->buff, p->name, c);
}
int raviD_protectedparser (lua_State *L, ZIO *z, const char *name,
int raviD_protected_ast_builder (lua_State *L, ZIO *z, const char *name,
const char *mode) {
struct SParser p;
int status;

@ -1073,34 +1073,29 @@ static void parse_chunk(LexState *ls) {
check(ls, TK_EOS);
}
LClosure *raviY_parser(lua_State *L, ZIO *z, Mbuffer *buff, Dyndata *dyd,
/*
** Parse the given source 'chunk' and build an abstract
** syntax tree; return 0 on success / non-zero return code on
** failure
** On success will push a userdata object containing the abstract
** syntax tree.
** On failure push an error message.
*/
int raviY_parse_to_ast(lua_State *L, ZIO *z, Mbuffer *buff,
const char *name, int firstchar) {
LexState lexstate;
// Mbuffer mbuff;
// FuncState funcstate;
// LClosure *cl = luaF_newLclosure(L, 1); /* create main closure */
// setclLvalue(L, L->top, cl); /* anchor it (to avoid being collected) */
// luaD_inctop(L);
lexstate.h = luaH_new(L); /* create table for scanner */
sethvalue(L, L->top, lexstate.h); /* anchor it */
luaD_inctop(L);
TString *src = luaS_new(L, name); /* create and anchor TString */
setsvalue(L, L->top, src);
luaD_inctop(L);
// funcstate.f = cl->p = luaF_newproto(L);
// funcstate.f->source = luaS_new(L, name); /* create and anchor TString */
// lua_assert(iswhite(funcstate.f)); /* do not need barrier here */
lexstate.buff = buff;
lexstate.dyd = dyd;
dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
lexstate.dyd = NULL; /* Unlike standard Lua parser / code generator we do not use this */
luaX_setinput(L, &lexstate, z, src, firstchar);
// mainfunc(&lexstate, &funcstate);
parse_chunk(&lexstate);
// lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
/* all scopes should be correctly finished */
lua_assert(dyd->actvar.n == 0 && dyd->gt.n == 0 && dyd->label.n == 0);
L->top--; /* remove source name */
L->top--; /* remove scanner's table */
// return cl; /* closure is on the stack, too */
return luaY_parser(L, z, buff, dyd, name, firstchar);
// TODO Push the constructed AST
return 0;
}

@ -17,7 +17,7 @@ static int test_buildast(const char *code)
int rc = 0;
lua_State *L;
L = luaL_newstate();
if (raviL_loadbufferx(L, code, strlen(code), "testcode", NULL) != 0) {
if (raviL_build_ast_from_buffer(L, code, strlen(code), "testcode", NULL) != 0) {
rc = 1;
fprintf(stderr, "%s\n", lua_tostring(L, -1));
lua_pop(L, 1); /* pop error message from the stack */

Loading…
Cancel
Save