]> git.lizzy.rs Git - hydra-dragonfire.git/blob - tolua/generate.lua
Migrate to gopher-lua
[hydra-dragonfire.git] / tolua / generate.lua
1 #!/usr/bin/env lua
2 dofile("../parse_spec.lua")
3
4 local funcs = ""
5
6 for name, fields in spairs(parse_spec("client/enum")) do
7         local camel = camel_case(name)
8         funcs = funcs .. "func " .. camel .. "(l *lua.LState, val mt." .. camel .. ") lua.LValue {\n\tswitch val {\n"
9
10         for _, var in ipairs(fields) do
11                 funcs = funcs .. "\tcase mt."
12                         .. (fields.prefix or "") .. camel_case(var) .. (fields.postfix or "")
13                         .. ":\n\t\t" .. (var == "no" and "return lua.LNil" or "return lua.LString(\"" .. var .. "\")") .. "\n"
14         end
15
16         funcs = funcs .. "\t}\n\tpanic(\"impossible\")\n\treturn lua.LNil\n}\n\n"
17 end
18
19 for name, fields in spairs(parse_spec("client/flag")) do
20         local camel = camel_case(name)
21         funcs = funcs .. "func " .. camel .. "(l *lua.LState, val mt." .. camel .. ") lua.LValue {\n\ttbl := l.NewTable()\n"
22
23         for _, var in ipairs(fields) do
24                 funcs = funcs .. "\tif val&mt."
25                         .. (fields.prefix or "") .. camel_case(var) .. (fields.postfix or "")
26                         .. " != 0 {\n\t\tl.SetField(tbl, \"" .. var  .. "\", lua.LTrue)\n\t}\n"
27         end
28
29         funcs = funcs .. "\treturn tbl\n}\n\n"
30 end
31
32 local to_lua = {
33         string = "lua.LString(string(VAL))",
34         fixed_string = "lua.LString(string(VAL[:]))",
35         boolean = "lua.LBool(VAL)",
36         number = "lua.LNumber(VAL)",
37         vec2 = "Vec2(l, [2]lua.LNumber{lua.LNumber(VAL[0]), lua.LNumber(VAL[1])})",
38         vec3 = "Vec3(l, [3]lua.LNumber{lua.LNumber(VAL[0]), lua.LNumber(VAL[1]), lua.LNumber(VAL[2])})",
39         box1 = "Box1(l, [2]lua.LNumber{lua.LNumber(VAL[0]), lua.LNumber(VAL[1])})",
40         box2 = "Box2(l, [2][2]lua.LNumber{{lua.LNumber(VAL[0][0]), lua.LNumber(VAL[0][1])}, {lua.LNumber(VAL[1][0]), lua.LNumber(VAL[1][1])}})",
41         box3 = "Box3(l, [2][3]lua.LNumber{{lua.LNumber(VAL[0][0]), lua.LNumber(VAL[0][1]), lua.LNumber(VAL[0][2])}, {lua.LNumber(VAL[1][0]), lua.LNumber(VAL[1][1]), lua.LNumber(VAL[1][2])}})",
42 }
43
44 local function fields_to_lua(fields, indent)
45         local impl = ""
46         
47         for name, type in spairs(fields) do
48                 if name:sub(1, 1) ~= "[" then
49                         local camel = "val." .. camel_case(name)
50
51                         local idt = indent
52                         local condition = fields["[" .. name .. "]"]
53
54                         if condition then
55                                 impl = impl .. indent .. "if " .. condition .. " {\n" 
56                                 idt = idt .. "\t"
57                         end
58
59                         impl = impl .. idt .. "l.SetField(tbl, \"" .. name .. "\", "
60                         if to_lua[type] then
61                                 impl = impl .. to_lua[type]:gsub("VAL", camel)
62                         else
63                                 impl = impl .. camel_case(type) .. "(l, " .. camel .. ")"
64                         end
65                         impl = impl .. ")\n"
66
67                         if condition then
68                                 impl = impl .. indent .. "}\n"
69                         end
70                 end
71         end
72
73         return impl
74 end
75
76 for name, fields in spairs(parse_spec("client/struct", true)) do
77         local camel = camel_case(name)
78         funcs = funcs
79                 .. "func " .. camel .. "(l *lua.LState, val mt." .. camel .. ") lua.LValue {\n\ttbl := l.NewTable()\n"
80                 .. fields_to_lua(fields, "\t")
81                 .. "\treturn tbl\n}\n\n"
82 end
83
84 local to_string_impl = ""
85 local to_lua_impl = ""
86
87 for name, fields in spairs(parse_spec("client/pkt", true)) do
88         local case = "\tcase *mt.ToClt" .. camel_case(name) .. ":\n"
89
90         to_string_impl = to_string_impl
91                 .. case .. "\t\treturn lua.LString(\"" .. name .. "\")\n"
92
93         if next(fields) then
94                 to_lua_impl = to_lua_impl .. case .. fields_to_lua(fields, "\t\t")
95         end
96 end
97
98 local f = io.open("generated.go", "w")
99 f:write([[
100 // generated by generate.lua, DO NOT EDIT
101 package tolua
102
103 import (
104         "github.com/anon55555/mt"
105         "github.com/yuin/gopher-lua"
106 )
107
108 ]] .. funcs .. [[
109 func PktType(pkt *mt.Pkt) lua.LString {
110         switch pkt.Cmd.(type) {
111 ]] .. to_string_impl .. [[
112         }
113         panic("impossible")
114         return ""
115 }
116
117 func Pkt(l *lua.LState, pkt *mt.Pkt) lua.LValue {
118         if pkt == nil {
119                 return lua.LNil
120         }
121         tbl := l.NewTable()
122         l.SetField(tbl, "_type", PktType(pkt))
123         switch val := pkt.Cmd.(type) {
124 ]] .. to_lua_impl .. [[
125         }
126         return tbl
127 }
128 ]])
129 f:close()