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. 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.
c lua
add a comment |
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 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.
c lua
Why do you want to do this?
– Henri Menke
Nov 22 '18 at 22:36
To make global declarations explicit and easier to spot as well as to guard against accidental declarations.
– Edwin Skeevers
Nov 22 '18 at 23:40
1
Why not just do _G.variable = value? There really isn't such thing as a global in lua. _G is just the default environment (lookup table) which can be changed with setfenv. Try not to think of lua as a C-like language. You are just in various scopes within tables when you access variables, where each have explicit instructions set by metamethods on what to do exactly. _G just happens to be the default when a value cannot be found in that scope.
– Alundaio
Nov 23 '18 at 8:05
Give an example of a short complete code that has this declaration.
– lhf
Nov 23 '18 at 10:45
@Alundaio, thanks but that's all obvious and not at all pertinent to my original question.
– Edwin Skeevers
Nov 24 '18 at 22:42
add a comment |
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 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.
c lua
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 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.
c lua
c lua
edited Nov 24 '18 at 17:51
Edwin Skeevers
asked Nov 22 '18 at 22:18
Edwin SkeeversEdwin Skeevers
585
585
Why do you want to do this?
– Henri Menke
Nov 22 '18 at 22:36
To make global declarations explicit and easier to spot as well as to guard against accidental declarations.
– Edwin Skeevers
Nov 22 '18 at 23:40
1
Why not just do _G.variable = value? There really isn't such thing as a global in lua. _G is just the default environment (lookup table) which can be changed with setfenv. Try not to think of lua as a C-like language. You are just in various scopes within tables when you access variables, where each have explicit instructions set by metamethods on what to do exactly. _G just happens to be the default when a value cannot be found in that scope.
– Alundaio
Nov 23 '18 at 8:05
Give an example of a short complete code that has this declaration.
– lhf
Nov 23 '18 at 10:45
@Alundaio, thanks but that's all obvious and not at all pertinent to my original question.
– Edwin Skeevers
Nov 24 '18 at 22:42
add a comment |
Why do you want to do this?
– Henri Menke
Nov 22 '18 at 22:36
To make global declarations explicit and easier to spot as well as to guard against accidental declarations.
– Edwin Skeevers
Nov 22 '18 at 23:40
1
Why not just do _G.variable = value? There really isn't such thing as a global in lua. _G is just the default environment (lookup table) which can be changed with setfenv. Try not to think of lua as a C-like language. You are just in various scopes within tables when you access variables, where each have explicit instructions set by metamethods on what to do exactly. _G just happens to be the default when a value cannot be found in that scope.
– Alundaio
Nov 23 '18 at 8:05
Give an example of a short complete code that has this declaration.
– lhf
Nov 23 '18 at 10:45
@Alundaio, thanks but that's all obvious and not at all pertinent to my original question.
– Edwin Skeevers
Nov 24 '18 at 22:42
Why do you want to do this?
– Henri Menke
Nov 22 '18 at 22:36
Why do you want to do this?
– Henri Menke
Nov 22 '18 at 22:36
To make global declarations explicit and easier to spot as well as to guard against accidental declarations.
– Edwin Skeevers
Nov 22 '18 at 23:40
To make global declarations explicit and easier to spot as well as to guard against accidental declarations.
– Edwin Skeevers
Nov 22 '18 at 23:40
1
1
Why not just do _G.variable = value? There really isn't such thing as a global in lua. _G is just the default environment (lookup table) which can be changed with setfenv. Try not to think of lua as a C-like language. You are just in various scopes within tables when you access variables, where each have explicit instructions set by metamethods on what to do exactly. _G just happens to be the default when a value cannot be found in that scope.
– Alundaio
Nov 23 '18 at 8:05
Why not just do _G.variable = value? There really isn't such thing as a global in lua. _G is just the default environment (lookup table) which can be changed with setfenv. Try not to think of lua as a C-like language. You are just in various scopes within tables when you access variables, where each have explicit instructions set by metamethods on what to do exactly. _G just happens to be the default when a value cannot be found in that scope.
– Alundaio
Nov 23 '18 at 8:05
Give an example of a short complete code that has this declaration.
– lhf
Nov 23 '18 at 10:45
Give an example of a short complete code that has this declaration.
– lhf
Nov 23 '18 at 10:45
@Alundaio, thanks but that's all obvious and not at all pertinent to my original question.
– Edwin Skeevers
Nov 24 '18 at 22:42
@Alundaio, thanks but that's all obvious and not at all pertinent to my original question.
– Edwin Skeevers
Nov 24 '18 at 22:42
add a comment |
2 Answers
2
active
oldest
votes
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.
I'd prefer a quick and dirty solution, something along the lines of the "global" keyword gets interpreted as whitespace or is somehow otherwise stripped away and discarded by the parser.
– Edwin Skeevers
Nov 23 '18 at 0:21
@user2418731, it's very easy to write a token filter for that. Try ltokenp.
– lhf
Nov 23 '18 at 10:42
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438585%2fadding-an-optional-global-keyword-to-lua-5-2-source%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
I'd prefer a quick and dirty solution, something along the lines of the "global" keyword gets interpreted as whitespace or is somehow otherwise stripped away and discarded by the parser.
– Edwin Skeevers
Nov 23 '18 at 0:21
@user2418731, it's very easy to write a token filter for that. Try ltokenp.
– lhf
Nov 23 '18 at 10:42
add a comment |
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.
I'd prefer a quick and dirty solution, something along the lines of the "global" keyword gets interpreted as whitespace or is somehow otherwise stripped away and discarded by the parser.
– Edwin Skeevers
Nov 23 '18 at 0:21
@user2418731, it's very easy to write a token filter for that. Try ltokenp.
– lhf
Nov 23 '18 at 10:42
add a comment |
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.
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.
answered Nov 22 '18 at 23:45
Paul KulchenkoPaul Kulchenko
19.5k22541
19.5k22541
I'd prefer a quick and dirty solution, something along the lines of the "global" keyword gets interpreted as whitespace or is somehow otherwise stripped away and discarded by the parser.
– Edwin Skeevers
Nov 23 '18 at 0:21
@user2418731, it's very easy to write a token filter for that. Try ltokenp.
– lhf
Nov 23 '18 at 10:42
add a comment |
I'd prefer a quick and dirty solution, something along the lines of the "global" keyword gets interpreted as whitespace or is somehow otherwise stripped away and discarded by the parser.
– Edwin Skeevers
Nov 23 '18 at 0:21
@user2418731, it's very easy to write a token filter for that. Try ltokenp.
– lhf
Nov 23 '18 at 10:42
I'd prefer a quick and dirty solution, something along the lines of the "global" keyword gets interpreted as whitespace or is somehow otherwise stripped away and discarded by the parser.
– Edwin Skeevers
Nov 23 '18 at 0:21
I'd prefer a quick and dirty solution, something along the lines of the "global" keyword gets interpreted as whitespace or is somehow otherwise stripped away and discarded by the parser.
– Edwin Skeevers
Nov 23 '18 at 0:21
@user2418731, it's very easy to write a token filter for that. Try ltokenp.
– lhf
Nov 23 '18 at 10:42
@user2418731, it's very easy to write a token filter for that. Try ltokenp.
– lhf
Nov 23 '18 at 10:42
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 24 '18 at 22:34
Edwin SkeeversEdwin Skeevers
585
585
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438585%2fadding-an-optional-global-keyword-to-lua-5-2-source%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Why do you want to do this?
– Henri Menke
Nov 22 '18 at 22:36
To make global declarations explicit and easier to spot as well as to guard against accidental declarations.
– Edwin Skeevers
Nov 22 '18 at 23:40
1
Why not just do _G.variable = value? There really isn't such thing as a global in lua. _G is just the default environment (lookup table) which can be changed with setfenv. Try not to think of lua as a C-like language. You are just in various scopes within tables when you access variables, where each have explicit instructions set by metamethods on what to do exactly. _G just happens to be the default when a value cannot be found in that scope.
– Alundaio
Nov 23 '18 at 8:05
Give an example of a short complete code that has this declaration.
– lhf
Nov 23 '18 at 10:45
@Alundaio, thanks but that's all obvious and not at all pertinent to my original question.
– Edwin Skeevers
Nov 24 '18 at 22:42