shader: Bug fix: true/false should be available as a local variable

This commit is contained in:
Hajime Hoshi 2020-09-13 20:38:51 +09:00
parent a5af597594
commit e543d4f107
2 changed files with 20 additions and 8 deletions

View File

@ -369,14 +369,6 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
return exprs, f.ir.OutParams, stmts, true return exprs, f.ir.OutParams, stmts, true
case *ast.Ident: case *ast.Ident:
if e.Name == "true" || e.Name == "false" {
return []shaderir.Expr{
{
Type: shaderir.NumberExpr,
Const: gconstant.MakeBool(e.Name == "true"),
},
}, []shaderir.Type{{Main: shaderir.Bool}}, nil, true
}
if e.Name == "_" { if e.Name == "_" {
return []shaderir.Expr{ return []shaderir.Expr{
{ {
@ -425,6 +417,14 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
}, },
}, nil, nil, true }, nil, nil, true
} }
if e.Name == "true" || e.Name == "false" {
return []shaderir.Expr{
{
Type: shaderir.NumberExpr,
Const: gconstant.MakeBool(e.Name == "true"),
},
}, []shaderir.Type{{Main: shaderir.Bool}}, nil, true
}
cs.addError(e.Pos(), fmt.Sprintf("unexpected identifier: %s", e.Name)) cs.addError(e.Pos(), fmt.Sprintf("unexpected identifier: %s", e.Name))
case *ast.ParenExpr: case *ast.ParenExpr:

View File

@ -506,3 +506,15 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
t.Errorf("error must be non-nil but was nil") t.Errorf("error must be non-nil but was nil")
} }
} }
func TestShaderBoolLiteral(t *testing.T) {
if _, err := NewShader([]byte(`package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
true := vec4(0)
return true
}
`)); err != nil {
t.Errorf("error must be nil but was non-nil")
}
}