diff --git a/cheatsheet.md b/cheatsheet.md index 9b51f2a..ce8e04a 100644 --- a/cheatsheet.md +++ b/cheatsheet.md @@ -229,11 +229,13 @@ var k = 5; // initialized to 5 (integer) var l := k; // reference to k auto &m = k; // reference to k -GLOBAL g = 5; // creates a global variable. If global already exists, it is not re-added -GLOBAL g = 2; // global 'g' now equals 2 +global g = 5; // creates a global variable. If global already exists, it is not re-added +global g = 2; // global 'g' now equals 2 -GLOBAL g2; -if (g2.is_var_undef()) { g2 = 4; } // only initialize g2 once, if GLOBAL decl hit more than once +global g2; +if (g2.is_var_undef()) { g2 = 4; } // only initialize g2 once, if global decl hit more than once + +GLOBAL g3; // all upper case version also accepted ``` ## Built in Types diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index fba4f8f..9824bd2 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -342,6 +342,7 @@ namespace chaiscript m_engine.add_reserved_word("class"); m_engine.add_reserved_word("attr"); m_engine.add_reserved_word("var"); + m_engine.add_reserved_word("global"); m_engine.add_reserved_word("GLOBAL"); m_engine.add_reserved_word("_"); diff --git a/include/chaiscript/language/chaiscript_parser.hpp b/include/chaiscript/language/chaiscript_parser.hpp index cdc23f6..82d81f6 100644 --- a/include/chaiscript/language/chaiscript_parser.hpp +++ b/include/chaiscript/language/chaiscript_parser.hpp @@ -2065,7 +2065,7 @@ namespace chaiscript } build_match(prev_stack_top); - } else if (Keyword("GLOBAL")) { + } else if (Keyword("GLOBAL") || Keyword("global")) { retval = true; if (!(Reference() || Id())) { diff --git a/unittests/global_lcase.chai b/unittests/global_lcase.chai new file mode 100644 index 0000000..7f8959a --- /dev/null +++ b/unittests/global_lcase.chai @@ -0,0 +1,18 @@ +// Test global + +global g = 3; +assert_true(g == 3); + +var v := g; +assert_true(v == 3); + +global g = 2; +assert_true(g == 2); +assert_true(v == 2); + +def f() { + assert_true(g == 2); +} + +f(); +