summaryrefslogtreecommitdiff
path: root/Tools/LuaMacro/tests/cexport.lua
blob: cb20bd0db8ff2ac1a3bae42329c6a1dd73437e9f (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
local M = require 'macro'

local cf,copy,null
if package.config:sub(1,1) == '\\' then
    cf = 'fc'; copy = 'copy'; null = ' > null'
else
    cf = 'diff'; copy = 'cp'; null = ' > /dev/null'
end

local f,hname,mname

M.keyword_handler('BEGIN',function()
    hname = M.filename:gsub('%.%a+$','')..'.h'
    mname = hname:gsub('%.','_'):upper()
    f = io.open(M.filename..'.h','w')
    f:write('#ifndef ',mname,'\n')
    f:write('#define ',mname,'\n')
end)

M.keyword_handler ('END',function()
    f:write('#endif\n')
    f:close()
    local tmpf = M.filename..'.h'
    if os.execute(cf..' '..hname..' '..tmpf..null) ~= 0 then
        os.execute(copy..' '..tmpf..' '..hname..null)
    end
end)

M.define('export',function(get)
    local t,v = get:next()
    local decl,out
    if v == '{' then -- block!
        decl = tostring(get:upto '}')
        decl = M.substitute_tostring(decl)
        f:write(decl,'\n')
    else
        decl = v .. ' ' .. tostring(get:upto '{')
        decl = M.substitute_tostring(decl)
        f:write(decl,';\n')
        out = decl .. '{'
    end
    return out
end)


--[[

Example of a with-statement:

    with(MyType *,bonzo) {
        .x = 2;
        .y = 3;
        with(SubType *,.data) {
            .name = "hello";
            .ids = my.ids;
            printf("count %d\n",.count);
        }
    }


M.define('with',function(get)
  get:expecting '('
  local args = get:list()
  local T, expr = args[1],args[2]
  get:expecting '{'
  M.define_scoped('.',function()
    local lt,lv = get:peek(-1,true) --  peek before the period...
    if lt ~= 'iden' then
      return '_var->'
    else
      return nil,true -- pass through
    end
  end)
  return '{ ' .. tostring(T) .. ' _var = '..tostring(expr)..'; '
end)

]]