shader: Implement operators ++ and --

This commit is contained in:
Hajime Hoshi 2020-07-12 23:07:30 +09:00
parent f95ca46c99
commit e15ee77e8e
3 changed files with 51 additions and 0 deletions

View File

@ -17,6 +17,7 @@ package shader
import (
"fmt"
"go/ast"
gconstant "go/constant"
"go/token"
"strings"
@ -173,6 +174,37 @@ func (cs *compileState) parseStmt(block *block, stmt ast.Stmt, inParams []variab
Exprs: exprs,
Blocks: bs,
})
case *ast.IncDecStmt:
exprs, _, ss, ok := cs.parseExpr(block, stmt.X)
if !ok {
return nil, false
}
stmts = append(stmts, ss...)
var op shaderir.Op
switch stmt.Tok {
case token.INC:
op = shaderir.Add
case token.DEC:
op = shaderir.Sub
}
stmts = append(stmts, shaderir.Stmt{
Type: shaderir.Assign,
Exprs: []shaderir.Expr{
exprs[0],
{
Type: shaderir.Binary,
Op: op,
Exprs: []shaderir.Expr{
exprs[0],
{
Type: shaderir.NumberExpr,
Const: gconstant.MakeInt64(1),
ConstType: shaderir.ConstTypeInt,
},
},
},
},
})
case *ast.ReturnStmt:
for i, r := range stmt.Results {
exprs, _, ss, ok := cs.parseExpr(block, r)

View File

@ -0,0 +1,10 @@
void F0(out vec2 l0) {
int l1 = 0;
int l2 = 0;
l1 = 0;
l1 = (l1) + (1);
l2 = 1;
l2 = (l2) - (1);
l0 = vec2(l1, l2);
return;
}

9
internal/shader/testdata/inc.go vendored Normal file
View File

@ -0,0 +1,9 @@
package main
func Foo() vec2 {
l0 := 0
l0++
l1 := 1
l1--
return vec2(l0, l1)
}