From e15ee77e8e3b05f654a294bbecdc05476ae0ae5f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 12 Jul 2020 23:07:30 +0900 Subject: [PATCH] shader: Implement operators ++ and -- --- internal/shader/stmt.go | 32 ++++++++++++++++++++++++ internal/shader/testdata/inc.expected.vs | 10 ++++++++ internal/shader/testdata/inc.go | 9 +++++++ 3 files changed, 51 insertions(+) create mode 100644 internal/shader/testdata/inc.expected.vs create mode 100644 internal/shader/testdata/inc.go diff --git a/internal/shader/stmt.go b/internal/shader/stmt.go index 808ba658e..cb09e1c51 100644 --- a/internal/shader/stmt.go +++ b/internal/shader/stmt.go @@ -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) diff --git a/internal/shader/testdata/inc.expected.vs b/internal/shader/testdata/inc.expected.vs new file mode 100644 index 000000000..2532b1288 --- /dev/null +++ b/internal/shader/testdata/inc.expected.vs @@ -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; +} diff --git a/internal/shader/testdata/inc.go b/internal/shader/testdata/inc.go new file mode 100644 index 000000000..2f4384bb6 --- /dev/null +++ b/internal/shader/testdata/inc.go @@ -0,0 +1,9 @@ +package main + +func Foo() vec2 { + l0 := 0 + l0++ + l1 := 1 + l1-- + return vec2(l0, l1) +}