From 8b2a22fb47ad5f97ece29fc3ce81e689ed096387 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 9 May 2020 23:58:22 +0900 Subject: [PATCH] shader: Parse return --- internal/shader/block.go | 60 ++++++++++++++++++++++++++++++++++ internal/shader/shader.go | 21 +++++++++++- internal/shader/shader_test.go | 7 ++-- 3 files changed, 85 insertions(+), 3 deletions(-) diff --git a/internal/shader/block.go b/internal/shader/block.go index 207eaa1d4..7060809d8 100644 --- a/internal/shader/block.go +++ b/internal/shader/block.go @@ -35,8 +35,68 @@ func (b *block) dump(indent int) []string { 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 } +type stmtType int + +const ( + stmtNone stmtType = iota + stmtReturn +) + 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) + } } diff --git a/internal/shader/shader.go b/internal/shader/shader.go index 5c71ccd94..4c5e3e7b7 100644 --- a/internal/shader/shader.go +++ b/internal/shader/shader.go @@ -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)) return } - init = v.Value // TODO: This should be math/big.Int or Float. + init = v.Value // TODO: This should be go/constant.Value default: // TODO: Parse the expression. } @@ -348,6 +348,14 @@ func (sh *Shader) parseBlock(b *ast.BlockStmt) *block { } } 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: } } @@ -359,6 +367,17 @@ func (sh *Shader) parseBlock(b *ast.BlockStmt) *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. func (s *Shader) Dump() string { var lines []string diff --git a/internal/shader/shader_test.go b/internal/shader/shader_test.go index ed5ad2669..fc7c66be2 100644 --- a/internal/shader/shader_test.go +++ b/internal/shader/shader_test.go @@ -46,8 +46,9 @@ const C2, C3 float = 2, 3 func F1(a, b vec2) vec4 { var c0 vec2 = a - var c1 = b - c2 := vec4{c0, c1} + var c1, c2 = b, 1.0 + c1.x = c2.x + c3 := vec4{c0, c1} return c2 } `, @@ -66,6 +67,8 @@ func F1(a vec2, b vec2) (_ vec4) { var c0 vec2 var c1 (none) var c2 (none) + var c3 (none) + return c2 } `, },