shader: Add more tests

This commit is contained in:
Hajime Hoshi 2020-05-10 20:41:43 +09:00
parent 8ede0b3891
commit 73255f4663
2 changed files with 43 additions and 19 deletions

View File

@ -303,23 +303,25 @@ func (sh *Shader) parseFunc(d *ast.FuncDecl, block *block) function {
}
var rets []variable
for _, f := range d.Type.Results.List {
t := parseType(f.Type)
if t == typNone {
sh.addError(f.Type.Pos(), fmt.Sprintf("unexpected type: %s", f.Type))
continue
}
if len(f.Names) == 0 {
rets = append(rets, variable{
name: "",
typ: t,
})
} else {
for _, n := range f.Names {
if d.Type.Results != nil {
for _, f := range d.Type.Results.List {
t := parseType(f.Type)
if t == typNone {
sh.addError(f.Type.Pos(), fmt.Sprintf("unexpected type: %s", f.Type))
continue
}
if len(f.Names) == 0 {
rets = append(rets, variable{
name: n.Name,
name: "",
typ: t,
})
} else {
for _, n := range f.Names {
rets = append(rets, variable{
name: n.Name,
typ: t,
})
}
}
}
}
@ -397,7 +399,11 @@ func (s *Shader) detectType(b *block, expr ast.Expr) typ {
if e.Kind == token.FLOAT {
return typFloat
}
s.addError(expr.Pos(), fmt.Sprintf("unexpected literal: %s", e.Value))
if e.Kind == token.INT {
s.addError(expr.Pos(), fmt.Sprintf("integer literal is not implemented yet: %s", e.Value))
} else {
s.addError(expr.Pos(), fmt.Sprintf("unexpected literal: %s", e.Value))
}
return typNone
case *ast.CompositeLit:
return parseType(e.Type)
@ -425,7 +431,9 @@ func (s *Shader) detectType(b *block, expr ast.Expr) typ {
func (s *Shader) Dump() string {
var lines []string
lines = append(lines, fmt.Sprintf("var %s varying %s // position", s.position.name, s.position.typ))
if s.position.name != "" {
lines = append(lines, fmt.Sprintf("var %s varying %s // position", s.position.name, s.position.typ))
}
for _, v := range s.varyings {
lines = append(lines, fmt.Sprintf("var %s varying %s", v.name, v.typ))
}

View File

@ -23,12 +23,12 @@ import (
func TestDump(t *testing.T) {
tests := []struct {
Name string
In string
Src string
Dump string
}{
{
Name: "general",
In: `package main
Src: `package main
type VertexOut struct {
Position vec4 ` + "`kage:\"position\"`" + `
@ -74,11 +74,27 @@ func F1(a vec2, b vec2) (_ vec4) {
c3 = vec4{c0, c1}
return c2
}
`,
},
{
Name: "Type",
Src: `package main
var c0 = 0.0
func F() {
c1 := c0
}
`,
Dump: `var c0 float = 0.0
func F() {
var c1 float
c1 = c0
}
`,
},
}
for _, tc := range tests {
s, err := NewShader([]byte(tc.In))
s, err := NewShader([]byte(tc.Src))
if err != nil {
t.Error(err)
continue