shader: Add stmtBlock

This commit is contained in:
Hajime Hoshi 2020-05-10 21:37:59 +09:00
parent 73255f4663
commit a39376ad47
3 changed files with 59 additions and 10 deletions

View File

@ -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 {

View File

@ -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:
}
}

View File

@ -77,18 +77,57 @@ func F1(a vec2, b vec2) (_ vec4) {
`,
},
{
Name: "Type",
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
}
}
`,
},