Also allow lcase global keyword

Closes #221
This commit is contained in:
Jason Turner
2016-01-31 19:15:32 -07:00
parent 03ef44f415
commit b104b26f11
4 changed files with 26 additions and 5 deletions

View File

@@ -229,11 +229,13 @@ var k = 5; // initialized to 5 (integer)
var l := k; // reference to k var l := k; // reference to k
auto &m = 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 = 5; // creates a global variable. If global already exists, it is not re-added
GLOBAL g = 2; // global 'g' now equals 2 global g = 2; // global 'g' now equals 2
GLOBAL g2; global g2;
if (g2.is_var_undef()) { g2 = 4; } // only initialize g2 once, if GLOBAL decl hit more than once 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 ## Built in Types

View File

@@ -342,6 +342,7 @@ namespace chaiscript
m_engine.add_reserved_word("class"); m_engine.add_reserved_word("class");
m_engine.add_reserved_word("attr"); m_engine.add_reserved_word("attr");
m_engine.add_reserved_word("var"); m_engine.add_reserved_word("var");
m_engine.add_reserved_word("global");
m_engine.add_reserved_word("GLOBAL"); m_engine.add_reserved_word("GLOBAL");
m_engine.add_reserved_word("_"); m_engine.add_reserved_word("_");

View File

@@ -2065,7 +2065,7 @@ namespace chaiscript
} }
build_match<eval::Var_Decl_AST_Node>(prev_stack_top); build_match<eval::Var_Decl_AST_Node>(prev_stack_top);
} else if (Keyword("GLOBAL")) { } else if (Keyword("GLOBAL") || Keyword("global")) {
retval = true; retval = true;
if (!(Reference() || Id())) { if (!(Reference() || Id())) {

View File

@@ -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();