diff --git a/internal/shader/block.go b/internal/shader/block.go index 49d58cdef..e63df174e 100644 --- a/internal/shader/block.go +++ b/internal/shader/block.go @@ -80,12 +80,14 @@ type stmtType int const ( stmtNone stmtType = iota stmtAssign + stmtBlock stmtReturn ) type stmt struct { stmtType stmtType exprs []ast.Expr + block *block } func (s *stmt) dump(indent int) []string { @@ -97,6 +99,10 @@ func (s *stmt) dump(indent int) []string { lines = append(lines, "%s(none)", idt) case stmtAssign: lines = append(lines, fmt.Sprintf("%s%s = %s", idt, dumpExpr(s.exprs[0]), dumpExpr(s.exprs[1]))) + case stmtBlock: + lines = append(lines, fmt.Sprintf("%s{", idt)) + lines = append(lines, s.block.dump(indent+1)...) + lines = append(lines, fmt.Sprintf("%s}", idt)) case stmtReturn: var expr string if len(s.exprs) > 0 { diff --git a/internal/shader/shader.go b/internal/shader/shader.go index 1e33a6b93..f22a15f7b 100644 --- a/internal/shader/shader.go +++ b/internal/shader/shader.go @@ -330,11 +330,11 @@ func (sh *Shader) parseFunc(d *ast.FuncDecl, block *block) function { name: d.Name.Name, args: args, rets: rets, - body: sh.parseBlock(d.Body, block), + body: sh.parseBlock(block, d.Body), } } -func (sh *Shader) parseBlock(b *ast.BlockStmt, outer *block) *block { +func (sh *Shader) parseBlock(outer *block, b *ast.BlockStmt) *block { block := &block{ outer: outer, } @@ -368,6 +368,11 @@ func (sh *Shader) parseBlock(b *ast.BlockStmt, outer *block) *block { }) } } + case *ast.BlockStmt: + block.stmts = append(block.stmts, stmt{ + stmtType: stmtBlock, + block: sh.parseBlock(block, l), + }) case *ast.DeclStmt: sh.parseDecl(block, l.Decl) case *ast.ReturnStmt: @@ -379,7 +384,6 @@ func (sh *Shader) parseBlock(b *ast.BlockStmt, outer *block) *block { stmtType: stmtReturn, exprs: exprs, }) - default: } } diff --git a/internal/shader/shader_test.go b/internal/shader/shader_test.go index eddc3521b..34621645a 100644 --- a/internal/shader/shader_test.go +++ b/internal/shader/shader_test.go @@ -77,18 +77,57 @@ func F1(a vec2, b vec2) (_ vec4) { `, }, { - Name: "Type", - Src: `package main + Name: "AutoType", + Src: `package main -var c0 = 0.0 +var v0 = 0.0 func F() { - c1 := c0 + v1 := v0 } `, - Dump: `var c0 float = 0.0 + Dump: `var v0 float = 0.0 func F() { - var c1 float - c1 = c0 + var v1 float + v1 = v0 +} +`, + }, + { + Name: "AutoType", + Src: `package main + +var v0 = 0.0 +func F() { + v1 := v0 +} +`, + Dump: `var v0 float = 0.0 +func F() { + var v1 float + v1 = v0 +} +`, + }, + { + Name: "AutoType2", + Src: `package main + +var v0 = 0.0 +func F() { + v1 := v0 + { + v2 := v1 + } +} +`, + Dump: `var v0 float = 0.0 +func F() { + var v1 float + v1 = v0 + { + var v2 float + v2 = v1 + } } `, },