shader: Parse return

This commit is contained in:
Hajime Hoshi 2020-05-09 23:58:22 +09:00
parent 5e6be91eac
commit 8b2a22fb47
3 changed files with 85 additions and 3 deletions

View File

@ -35,8 +35,68 @@ func (b *block) dump(indent int) []string {
lines = append(lines, fmt.Sprintf("%svar %s %s", idt, v.name, v.typ)) lines = append(lines, fmt.Sprintf("%svar %s %s", idt, v.name, v.typ))
} }
for _, s := range b.stmts {
lines = append(lines, s.dump(indent)...)
}
return lines return lines
} }
type stmtType int
const (
stmtNone stmtType = iota
stmtReturn
)
type stmt struct { type stmt struct {
stmtType stmtType
exprs []expr
}
func (s *stmt) dump(indent int) []string {
idt := strings.Repeat("\t", indent)
var lines []string
switch s.stmtType {
case stmtNone:
lines = append(lines, "%s(none)", idt)
case stmtReturn:
var expr string
if len(s.exprs) > 0 {
var strs []string
for _, e := range s.exprs {
strs = append(strs, e.dump())
}
expr = " " + strings.Join(strs, ", ")
}
lines = append(lines, fmt.Sprintf("%sreturn%s", idt, expr))
default:
lines = append(lines, fmt.Sprintf("%s(unknown stmt: %d)", idt, s.stmtType))
}
return lines
}
type exprType int
const (
exprNone exprType = iota
exprIdent
)
type expr struct {
exprType exprType
value string
}
func (e *expr) dump() string {
switch e.exprType {
case exprNone:
return "(none)"
case exprIdent:
return e.value
default:
return fmt.Sprintf("(unkown expr: %d)", e.exprType)
}
} }

View File

@ -248,7 +248,7 @@ func (s *Shader) parseTopLevelConstant(vs *ast.ValueSpec) {
s.addError(v.Pos(), fmt.Sprintf("literal must be int or float but %s", v.Kind)) s.addError(v.Pos(), fmt.Sprintf("literal must be int or float but %s", v.Kind))
return return
} }
init = v.Value // TODO: This should be math/big.Int or Float. init = v.Value // TODO: This should be go/constant.Value
default: default:
// TODO: Parse the expression. // TODO: Parse the expression.
} }
@ -348,6 +348,14 @@ func (sh *Shader) parseBlock(b *ast.BlockStmt) *block {
} }
} }
case *ast.ReturnStmt: case *ast.ReturnStmt:
var exprs []expr
for _, r := range l.Results {
exprs = append(exprs, sh.parseExpr(r))
}
block.stmts = append(block.stmts, stmt{
stmtType: stmtReturn,
exprs: exprs,
})
default: default:
} }
} }
@ -359,6 +367,17 @@ func (sh *Shader) parseBlock(b *ast.BlockStmt) *block {
return block return block
} }
func (sh *Shader) parseExpr(e ast.Expr) expr {
switch e := e.(type) {
case *ast.Ident:
return expr{
exprType: exprIdent,
value: e.Name,
}
}
return expr{}
}
// Dump dumps the shader state in an intermediate language. // Dump dumps the shader state in an intermediate language.
func (s *Shader) Dump() string { func (s *Shader) Dump() string {
var lines []string var lines []string

View File

@ -46,8 +46,9 @@ const C2, C3 float = 2, 3
func F1(a, b vec2) vec4 { func F1(a, b vec2) vec4 {
var c0 vec2 = a var c0 vec2 = a
var c1 = b var c1, c2 = b, 1.0
c2 := vec4{c0, c1} c1.x = c2.x
c3 := vec4{c0, c1}
return c2 return c2
} }
`, `,
@ -66,6 +67,8 @@ func F1(a vec2, b vec2) (_ vec4) {
var c0 vec2 var c0 vec2
var c1 (none) var c1 (none)
var c2 (none) var c2 (none)
var c3 (none)
return c2
} }
`, `,
}, },