From db4dc1010088918f9c9ccb3176adf64efe08cd57 Mon Sep 17 00:00:00 2001
From: James R <justsomejames2@gmail.com>
Date: Fri, 17 Jan 2020 06:06:29 -0800
Subject: [PATCH] Lua 'global' keyword, only for functions for now because I'm
 tired

---
 src/blua/llex.c    |  2 +-
 src/blua/llex.h    |  2 +-
 src/blua/lparser.c | 16 ++++++++++++++++
 3 files changed, 18 insertions(+), 2 deletions(-)

diff --git a/src/blua/llex.c b/src/blua/llex.c
index 9e3dc2929..742319b0c 100644
--- a/src/blua/llex.c
+++ b/src/blua/llex.c
@@ -38,7 +38,7 @@
 const char *const luaX_tokens [] = {
     "and", "break", "continue", "do", "else", "elseif",
     "end", "false", "for", "function", "if",
-    "in", "local", "nil", "not", "or", "repeat",
+    "in", "local", "global", "nil", "not", "or", "repeat",
     "return", "then", "true", "until", "while",
     "..", "...", "==", ">=", "<=", "~=",
     "<number>", "<name>", "<string>", "<eof>",
diff --git a/src/blua/llex.h b/src/blua/llex.h
index 3f55c75cd..4da3fe9f3 100644
--- a/src/blua/llex.h
+++ b/src/blua/llex.h
@@ -25,7 +25,7 @@ enum RESERVED {
   /* terminal symbols denoted by reserved words */
   TK_AND = FIRST_RESERVED, TK_BREAK, TK_CONTINUE,
   TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
-  TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
+  TK_IF, TK_IN, TK_LOCAL, TK_GLOBAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
   TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
   /* other terminal symbols */
   TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_NUMBER,
diff --git a/src/blua/lparser.c b/src/blua/lparser.c
index 4b1d2ffb9..48b70d2ab 100644
--- a/src/blua/lparser.c
+++ b/src/blua/lparser.c
@@ -1349,6 +1349,16 @@ static void localfunc (LexState *ls) {
 }
 
 
+static void globalfunc (LexState *ls) {
+  expdesc v, b;
+  FuncState *fs = ls->fs;
+  init_exp(&v, VGLOBAL, NO_REG);
+  v.u.s.info = luaK_stringK(fs, str_checkname(ls));
+  body(ls, &b, 0, ls->linenumber);
+  luaK_storevar(fs, &v, &b);
+}
+
+
 static void localstat (LexState *ls) {
   /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
   int nvars = 0;
@@ -1485,6 +1495,12 @@ static int statement (LexState *ls) {
         localstat(ls);
       return 0;
     }
+    case TK_GLOBAL: {  /* stat -> globalstat */
+      luaX_next(ls);   /* skip GLOBAL */
+      if (testnext(ls, TK_FUNCTION))
+        globalfunc(ls);
+      return 0;
+    }
     case TK_RETURN: {  /* stat -> retstat */
       retstat(ls);
       return 1;  /* must be last statement */
-- 
GitLab