@@ -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
|
||||
|
@@ -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("_");
|
||||
|
||||
|
@@ -2065,7 +2065,7 @@ namespace chaiscript
|
||||
}
|
||||
|
||||
build_match<eval::Var_Decl_AST_Node>(prev_stack_top);
|
||||
} else if (Keyword("GLOBAL")) {
|
||||
} else if (Keyword("GLOBAL") || Keyword("global")) {
|
||||
retval = true;
|
||||
|
||||
if (!(Reference() || Id())) {
|
||||
|
18
unittests/global_lcase.chai
Normal file
18
unittests/global_lcase.chai
Normal 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();
|
||||
|
Reference in New Issue
Block a user