shader: Add break/continue

This commit is contained in:
Hajime Hoshi 2020-07-13 02:59:11 +09:00
parent c02213e900
commit 0e6f4fccc0
3 changed files with 54 additions and 0 deletions

View File

@ -391,6 +391,21 @@ func (cs *compileState) parseStmt(block *block, stmt ast.Stmt, inParams []variab
Type: shaderir.Return,
})
case *ast.BranchStmt:
switch stmt.Tok {
case token.BREAK:
stmts = append(stmts, shaderir.Stmt{
Type: shaderir.Break,
})
case token.CONTINUE:
stmts = append(stmts, shaderir.Stmt{
Type: shaderir.Continue,
})
default:
cs.addError(stmt.Pos(), fmt.Sprintf("invalid token: %s", stmt.Tok))
return nil, false
}
case *ast.ExprStmt:
exprs, _, ss, ok := cs.parseExpr(block, stmt.X)
if !ok {

View File

@ -0,0 +1,20 @@
void F0(out vec2 l0) {
vec2 l1 = vec2(0);
vec2 l3 = vec2(0);
l1 = vec2(0.0);
for (int l2 = 0; l2 < 100; l2++) {
(l1).x = ((l1).x) + (l2);
if (((l1).x) >= (100.0)) {
break;
}
}
l3 = vec2(0.0);
for (float l4 = 10.0; l4 >= 0.0; l4 -= 2.0) {
if (((l3).x) < (100.0)) {
continue;
}
(l3).x = ((l3).x) + (l4);
}
l0 = l1;
return;
}

19
internal/shader/testdata/for2.go vendored Normal file
View File

@ -0,0 +1,19 @@
package main
func Foo() vec2 {
v := vec2(0)
for i := 0; i < 100; i++ {
v.x += i
if v.x >= 100 {
break
}
}
v2 := vec2(0)
for i := 10.0; i >= 0; i -= 2 {
if v2.x < 100 {
continue
}
v2.x += i
}
return v
}