Adding an optional “global” keyword to lua 5.2 source

后端 未结 2 1204
闹比i
闹比i 2021-01-24 23:21

I\'d like to modify the lua 5.2 source code to allow for an optional \"global\" keyword to precede global variable declarations. Has any done this or does anyone know how to do

相关标签:
2条回答
  • 2021-01-24 23:27

    See this discussion for details and a proposed patch (against 5.3): http://lua.2524044.n2.nabble.com/Say-No-to-global-by-default-summary-of-the-discussion-td7683658.html. It uses a different (non-keyword based) approach, but should be a good starting point.

    0 讨论(0)
  • 2021-01-24 23:34

    Figured it out. First I appended a new token TK_GLOBAL to the end of the RESERVED enum.

    Then in luaX_init() I added...

    ts = luaS_new(L, "global");
    luaS_fix(ts);
    ts->tsv.reserved = cast_byte(TK_GLOBAL+1-FIRST_RESERVED);
    

    And finally in the statement() function I added...

    case TK_GLOBAL:
       luaX_next(ls);
    break;
    

    As far as I can tell it works. Hopefully it's safe.

    0 讨论(0)
提交回复
热议问题