libui/redo/tools/idl2typefuncs.go

55 lines
1.1 KiB
Go

// 17 may 2015
package main
import (
"fmt"
"os"
"flag"
"github.com/andlabs/pgidl"
)
func geniface(iface *pgidl.Interface, prefix string) {
v := fmt.Sprintf("type%s%s", prefix, iface.Name)
fmt.Printf("static uintmax_t %s = 0;\n", v)
fmt.Printf("uintmax_t %sType%s(void)\n", prefix, iface.Name)
fmt.Printf("{\n")
fmt.Printf("\tif (%s == 0)\n", v)
fmt.Printf("\t\t%s = %sRegisterType(%q, ",
v,
prefix,
prefix + iface.Name)
if iface.From != "" {
fmt.Printf("%sType%s()", prefix, iface.From)
} else {
fmt.Printf("0")
}
fmt.Printf(");\n")
fmt.Printf("\treturn %s;\n", v)
fmt.Printf("}\n")
}
func genpkg(p *pgidl.Package) {
for _, o := range p.Order {
switch o.Which {
case pgidl.Interfaces:
geniface(p.Interfaces[o.Index], p.Name)
}
}
}
func main() {
flag.Parse()
idl, errs := pgidl.Parse(os.Stdin, "<stdin>")
if len(errs) != 0 {
for _, e := range errs {
fmt.Fprintf(os.Stderr, "%s\n", e)
}
os.Exit(1)
}
fmt.Printf("// generated by idl2h; do not edit\n")
fmt.Printf("#include %q\n", os.Args[1])
for _, p := range idl {
genpkg(p)
}
}