summaryrefslogtreecommitdiff
path: root/Tools/LuaMacro/macro/with.lua
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-11-17 23:03:07 +0800
committerchai <chaifix@163.com>2021-11-17 23:03:07 +0800
commit27d6efb5f5a076f825fe2da1875e0cabaf02b4e7 (patch)
tree44f301110bc2ea742908ed92a78eba0803cd3b60 /Tools/LuaMacro/macro/with.lua
parentb34310c631989551054d456eb47aaab5ded266a4 (diff)
+ LuaMacro
Diffstat (limited to 'Tools/LuaMacro/macro/with.lua')
-rw-r--r--Tools/LuaMacro/macro/with.lua31
1 files changed, 31 insertions, 0 deletions
diff --git a/Tools/LuaMacro/macro/with.lua b/Tools/LuaMacro/macro/with.lua
new file mode 100644
index 0000000..de51590
--- /dev/null
+++ b/Tools/LuaMacro/macro/with.lua
@@ -0,0 +1,31 @@
+--[[--
+A `with` statement. This works more like the Visual Basic statement than the
+Pascal one; fields have an explicit period to indicate that they are special.
+This makes variable scoping explcit.
+
+ aLongTableName = {}
+ with aLongTableName do
+ .a = 1
+ .b = {{x=1},{x=2}}
+ .c = {f = 2}
+ print(.a,.c.f,.b[1].x)
+ end
+
+Fields that follow an identifier or a `}` are passed as-is.
+
+@module macro.with
+]]
+local M = require 'macro'
+
+M.define('with',function(get,put)
+ M.define_scoped('.',function()
+ local lt,lv = get:peek(-1,true) -- peek before the period...
+ if lt ~= 'iden' and lt ~= ']' then
+ return '_var.'
+ else
+ return nil,true -- pass through
+ end
+ end)
+ local expr = get:upto 'do'
+ return 'do local _var = '..tostring(expr)..'; '
+end)