2 dofile("../parse_spec.lua")
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"
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"
16 funcs = funcs .. "\t}\n\tpanic(\"impossible\")\n\treturn lua.LNil\n}\n\n"
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"
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"
29 funcs = funcs .. "\treturn tbl\n}\n\n"
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])}})",
44 local function fields_to_lua(fields, indent)
47 for name, type in spairs(fields) do
48 if name:sub(1, 1) ~= "[" then
49 local camel = "val." .. camel_case(name)
52 local condition = fields["[" .. name .. "]"]
55 impl = impl .. indent .. "if " .. condition .. " {\n"
59 impl = impl .. idt .. "l.SetField(tbl, \"" .. name .. "\", "
61 impl = impl .. to_lua[type]:gsub("VAL", camel)
63 impl = impl .. camel_case(type) .. "(l, " .. camel .. ")"
68 impl = impl .. indent .. "}\n"
76 for name, fields in spairs(parse_spec("client/struct", true)) do
77 local camel = camel_case(name)
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"
84 local to_string_impl = ""
85 local to_lua_impl = ""
87 for name, fields in spairs(parse_spec("client/pkt", true)) do
88 local case = "\tcase *mt.ToClt" .. camel_case(name) .. ":\n"
90 to_string_impl = to_string_impl
91 .. case .. "\t\treturn lua.LString(\"" .. name .. "\")\n"
94 to_lua_impl = to_lua_impl .. case .. fields_to_lua(fields, "\t\t")
98 local f = io.open("generated.go", "w")
100 // generated by generate.lua, DO NOT EDIT
104 "github.com/anon55555/mt"
105 "github.com/yuin/gopher-lua"
109 func PktType(pkt *mt.Pkt) lua.LString {
110 switch pkt.Cmd.(type) {
111 ]] .. to_string_impl .. [[
117 func Pkt(l *lua.LState, pkt *mt.Pkt) lua.LValue {
122 l.SetField(tbl, "_type", PktType(pkt))
123 switch val := pkt.Cmd.(type) {
124 ]] .. to_lua_impl .. [[