shader: Refactoring

This commit is contained in:
Hajime Hoshi 2020-09-12 18:54:36 +09:00
parent a0db26f234
commit 8833e46c7e
2 changed files with 83 additions and 9 deletions

View File

@ -393,24 +393,23 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
return nil, false
}
stmts = append(stmts, ss...)
if len(exprs) == 0 {
if len(exprs) != len(outParams) {
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(stmt.Results)))
}
continue
}
if len(exprs) > 1 {
if len(stmt.Results) > 1 || len(outParams) == 1 {
cs.addError(r.Pos(), "single-value context and multiple-value context cannot be mixed")
return nil, false
}
}
// TODO: Is this logic really true?
if len(exprs) > 1 || len(outParams) > 1 {
if len(exprs) != len(outParams) && len(stmt.Results) != len(outParams) {
if len(outParams) > 1 && len(stmt.Results) == 1 {
if len(exprs) == 1 {
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(stmt.Results)))
return nil, false
}
if len(exprs) > 1 && len(exprs) != len(outParams) {
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(exprs)))
return nil, false
}
}
for j, t := range ts {

View File

@ -283,3 +283,78 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
t.Errorf("error must be non-nil but was nil")
}
}
func TestShaderMultipleValueReturn(t *testing.T) {
if _, err := NewShader([]byte(`package main
func Foo() (float, float) {
return 0.0
}
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
return vec4(0)
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
if _, err := NewShader([]byte(`package main
func Foo() float {
return 0.0, 0.0
}
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
return vec4(0)
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
if _, err := NewShader([]byte(`package main
func Foo() (float, float, float) {
return 0.0, 0.0
}
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
return vec4(0)
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
if _, err := NewShader([]byte(`package main
func Foo() (float, float) {
return 0.0, 0.0
}
func Foo2() (float, float, float) {
return Foo()
}
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
return vec4(0)
}
`)); err == nil {
t.Errorf("error must be non-nil but was nil")
}
if _, err := NewShader([]byte(`package main
func Foo() (float, float, float) {
return 0.0, 0.0, 0.0
}
func Foo2() (float, float, float) {
return Foo()
}
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
return vec4(0.0)
}
`)); err != nil {
t.Error(err)
}
}