nomsu/core/scopes.nom
2018-10-30 23:42:36 -07:00

46 lines
1.2 KiB
Plaintext

#!/usr/bin/env nomsu -V4.8.10
#
This file contains definitions pertaining to variable scoping
use "core/metaprogramming.nom"
use "core/operators.nom"
use "core/collections.nom"
use "core/control_flow.nom"
test:
%x = "outer"
with local %x:
%x = "inner"
assume (%x == "inner")
assume (%x == "outer")
externally (foo) means "outer foo"
with local [action (foo)]:
externally (foo) means "inner foo"
assume ((foo) == "inner foo")
assume ((foo) == "outer foo")
[with local %locals %body, with local %locals do %body] all compile to:
%body_lua = (%body as lua statements)
if %locals.type is:
"Dict":
%body_lua = (..)
Lua "\
..\(what (<- %locals) compiles to)
\%body_lua"
%body_lua::declare locals ("\(%.1 as lua)" for % in %locals)
"List":
%body_lua::declare locals ("\(% as lua)" for % in %locals)
"Var" "Action":
%body_lua::declare locals ["\(%locals as lua)"]
else:
compile error at %locals "Unexpected local value"
return (..)
Lua "\
..do
\%body_lua
end"