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 var rets []variable
for _, f := range d.Type.Results.List { if d.Type.Results != nil {
t := parseType(f.Type) for _, f := range d.Type.Results.List {
if t == typNone { t := parseType(f.Type)
sh.addError(f.Type.Pos(), fmt.Sprintf("unexpected type: %s", f.Type)) if t == typNone {
continue sh.addError(f.Type.Pos(), fmt.Sprintf("unexpected type: %s", f.Type))
} continue
if len(f.Names) == 0 { }
rets = append(rets, variable{ if len(f.Names) == 0 {
name: "",
typ: t,
})
} else {
for _, n := range f.Names {
rets = append(rets, variable{ rets = append(rets, variable{
name: n.Name, name: "",
typ: t, 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 { if e.Kind == token.FLOAT {
return typFloat 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 return typNone
case *ast.CompositeLit: case *ast.CompositeLit:
return parseType(e.Type) return parseType(e.Type)
@ -425,7 +431,9 @@ func (s *Shader) detectType(b *block, expr ast.Expr) typ {
func (s *Shader) Dump() string { func (s *Shader) Dump() string {
var lines []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 { for _, v := range s.varyings {
lines = append(lines, fmt.Sprintf("var %s varying %s", v.name, v.typ)) 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) { func TestDump(t *testing.T) {
tests := []struct { tests := []struct {
Name string Name string
In string Src string
Dump string Dump string
}{ }{
{ {
Name: "general", Name: "general",
In: `package main Src: `package main
type VertexOut struct { type VertexOut struct {
Position vec4 ` + "`kage:\"position\"`" + ` Position vec4 ` + "`kage:\"position\"`" + `
@ -74,11 +74,27 @@ func F1(a vec2, b vec2) (_ vec4) {
c3 = vec4{c0, c1} c3 = vec4{c0, c1}
return c2 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 { for _, tc := range tests {
s, err := NewShader([]byte(tc.In)) s, err := NewShader([]byte(tc.Src))
if err != nil { if err != nil {
t.Error(err) t.Error(err)
continue continue