简体   繁体   English

向lua 5.2源添加可选的“全局”关键字

[英]Adding an optional “global” keyword to lua 5.2 source

I'd like to modify the lua 5.2 source code to allow for an optional "global" keyword to precede global variable declarations. 我想修改lua 5.2源代码,以允许在全局变量声明之前使用一个可选的“ global”关键字。 Has any done this or does anyone know how to do this (safely)? 有没有做到这一点,或者有人知道如何安全地做到这一点? And yes I am aware that variables are global by default and that this would be purely syntactic sugar. 是的,我知道默认情况下变量是全局变量,这纯粹是语法糖。

To be clear, adding custom keywords of existing types is straight forward. 明确地说,直接添加现有类型的自定义关键字。 The part I'm at a loss for is how to safely edit the parser (via the 5.2 C source code) so that it discards or ignores the new "global" keyword. 我不知所措的部分是如何安全地编辑解析器(通过5.2 C源代码),以便它丢弃或忽略新的“ global”关键字。

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 . 有关详细信息和建议的补丁程序(针对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. 它使用了不同的方法(基于非关键字),但是应该是一个很好的起点。

Figured it out. 弄清楚了。 First I appended a new token TK_GLOBAL to the end of the RESERVED enum. 首先,我在RESERVED枚举的末尾附加了一个新令牌TK_GLOBAL。

Then in luaX_init() I added... 然后在luaX_init()中我添加了...

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... 最后,我在statement()函数中添加了...

case TK_GLOBAL:
   luaX_next(ls);
break;

As far as I can tell it works. 据我所知,它的工作原理。 Hopefully it's safe. 希望它是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM