summaryrefslogtreecommitdiff
path: root/Tools/LuaMacro/macro/with.lua
blob: de5159082c9f74a8f0828adb0dbf7f281aedfd17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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)