diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-05 16:24:59 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2025-05-05 16:24:59 -0300 |
| commit | be8120906304a8658fab998587b969e0e42f5650 (patch) | |
| tree | 81cf2d38522b10468d09763fc25d261486008197 /testes/goto.lua | |
| parent | e05590591410a5e007a1e3f1691f6c1cf9d8fe45 (diff) | |
| download | lua-be8120906304a8658fab998587b969e0e42f5650.tar.gz lua-be8120906304a8658fab998587b969e0e42f5650.tar.bz2 lua-be8120906304a8658fab998587b969e0e42f5650.zip | |
First implementation of global declarations
Diffstat (limited to 'testes/goto.lua')
| -rw-r--r-- | testes/goto.lua | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/testes/goto.lua b/testes/goto.lua index eca68516..fdfddb85 100644 --- a/testes/goto.lua +++ b/testes/goto.lua | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | -- $Id: testes/goto.lua $ | 1 | -- $Id: testes/goto.lua $ |
| 2 | -- See Copyright Notice in file lua.h | 2 | -- See Copyright Notice in file lua.h |
| 3 | 3 | ||
| 4 | print("testing goto and global declarations") | ||
| 5 | |||
| 4 | collectgarbage() | 6 | collectgarbage() |
| 5 | 7 | ||
| 6 | local function errmsg (code, m) | 8 | local function errmsg (code, m) |
| @@ -280,7 +282,47 @@ end | |||
| 280 | 282 | ||
| 281 | 283 | ||
| 282 | foo() | 284 | foo() |
| 283 | -------------------------------------------------------------------------------- | 285 | -------------------------------------------------------------------------- |
| 284 | 286 | ||
| 287 | do | ||
| 288 | global print, load, T<const>; global assert<const> | ||
| 289 | global string | ||
| 290 | |||
| 291 | local function checkerr (code, err) | ||
| 292 | local st, msg = load(code) | ||
| 293 | assert(not st and string.find(msg, err)) | ||
| 294 | end | ||
| 295 | |||
| 296 | -- globals must be declared after a global declaration | ||
| 297 | checkerr("global none; X = 1", "variable 'X'") | ||
| 298 | |||
| 299 | -- global variables cannot be to-be-closed | ||
| 300 | checkerr("global X<close>", "cannot be") | ||
| 301 | |||
| 302 | do | ||
| 303 | local X = 10 | ||
| 304 | do global X; X = 20 end | ||
| 305 | assert(X == 10) -- local X | ||
| 306 | end | ||
| 307 | assert(_ENV.X == 20) -- global X | ||
| 308 | |||
| 309 | -- '_ENV' cannot be global | ||
| 310 | checkerr("global _ENV, a; a = 10", "variable 'a'") | ||
| 311 | |||
| 312 | -- global declarations inside functions | ||
| 313 | checkerr([[ | ||
| 314 | global none | ||
| 315 | local function foo () XXX = 1 end --< ERROR]], "variable 'XXX'") | ||
| 316 | |||
| 317 | if not T then -- when not in "test mode", "global" isn't reserved | ||
| 318 | assert(load("global = 1; return global")() == 1) | ||
| 319 | print " ('global' is not a reserved word)" | ||
| 320 | else | ||
| 321 | -- "global" reserved, cannot be used as a variable | ||
| 322 | assert(not load("global = 1; return global")) | ||
| 323 | end | ||
| 324 | |||
| 325 | end | ||
| 285 | 326 | ||
| 286 | print'OK' | 327 | print'OK' |
| 328 | |||
