shader: Parse function signatures

This commit is contained in:
Hajime Hoshi 2020-05-31 16:20:36 +09:00
parent 5ea5dfccfc
commit ddba8a911f
2 changed files with 32 additions and 21 deletions

View File

@ -38,11 +38,9 @@ type constant struct {
type function struct {
name string
args []variable
rets []variable
in []string
out []string
block *block
ir shaderir.Func
}
type compileState struct {
@ -208,42 +206,44 @@ func (cs *compileState) parseFunc(d *ast.FuncDecl, block *block) function {
return function{}
}
var args []variable
var in []string
var inT []shaderir.Type
for _, f := range d.Type.Params.List {
t := cs.parseType(f.Type)
for _, n := range f.Names {
args = append(args, variable{
name: n.Name,
typ: t,
})
in = append(in, n.Name)
inT = append(inT, t.ir)
}
}
var rets []variable
var out []string
var outT []shaderir.Type
if d.Type.Results != nil {
for _, f := range d.Type.Results.List {
t := cs.parseType(f.Type)
if len(f.Names) == 0 {
rets = append(rets, variable{
name: "",
typ: t,
})
out = append(out, "")
outT = append(outT, t.ir)
} else {
for _, n := range f.Names {
rets = append(rets, variable{
name: n.Name,
typ: t,
})
out = append(out, n.Name)
outT = append(outT, t.ir)
}
}
}
}
cs.ir.Funcs = append(cs.ir.Funcs, shaderir.Func{
Index: len(cs.ir.Funcs),
InParams: inT,
OutParams: outT,
})
return function{
name: d.Name.Name,
args: args,
rets: rets,
//body: cs.parseBlock(block, d.Body),
in: in,
out: out,
//block: cs.parseBlock(block, d.Body),
}
}

View File

@ -40,6 +40,17 @@ uniform vec4 U1;`,
FS: `uniform vec2 U0;
uniform vec4 U1;`,
},
{
Name: "func",
Src: `package main
func Foo(foo vec2) vec4 {
}`,
VS: `void F0(in vec2 l0, out vec4 l1) {
}`,
FS: `void F0(in vec2 l0, out vec4 l1) {
}`,
},
}
for _, tc := range tests {
s, err := Compile([]byte(tc.Src))