shader: Parse return

This commit is contained in:
Hajime Hoshi 2020-05-31 18:01:12 +09:00
parent ddba8a911f
commit 91e5dde320
2 changed files with 73 additions and 15 deletions

View File

@ -41,6 +41,8 @@ type function struct {
in []string in []string
out []string out []string
block *block block *block
ir shaderir.Func
} }
type compileState struct { type compileState struct {
@ -63,6 +65,8 @@ type block struct {
funcs []function funcs []function
pos token.Pos pos token.Pos
outer *block outer *block
ir shaderir.Block
} }
type ParseError struct { type ParseError struct {
@ -105,6 +109,14 @@ func (cs *compileState) parse(f *ast.File) {
for _, d := range f.Decls { for _, d := range f.Decls {
cs.parseDecl(&cs.global, d, true) cs.parseDecl(&cs.global, d, true)
} }
if len(cs.errs) > 0 {
return
}
for _, f := range cs.global.funcs {
cs.ir.Funcs = append(cs.ir.Funcs, f.ir)
}
} }
func (cs *compileState) parseDecl(b *block, d ast.Decl, global bool) { func (cs *compileState) parseDecl(b *block, d ast.Decl, global bool) {
@ -233,17 +245,19 @@ func (cs *compileState) parseFunc(d *ast.FuncDecl, block *block) function {
} }
} }
cs.ir.Funcs = append(cs.ir.Funcs, shaderir.Func{ b := cs.parseBlock(block, d.Body)
Index: len(cs.ir.Funcs),
InParams: inT,
OutParams: outT,
})
return function{ return function{
name: d.Name.Name, name: d.Name.Name,
in: in, in: in,
out: out, out: out,
//block: cs.parseBlock(block, d.Body), block: b,
ir: shaderir.Func{
Index: len(cs.ir.Funcs),
InParams: inT,
OutParams: outT,
Block: b.ir,
},
} }
} }
@ -289,14 +303,22 @@ func (cs *compileState) parseBlock(outer *block, b *ast.BlockStmt) *block {
case *ast.DeclStmt: case *ast.DeclStmt:
cs.parseDecl(block, l.Decl, false) cs.parseDecl(block, l.Decl, false)
case *ast.ReturnStmt: case *ast.ReturnStmt:
var exprs []ast.Expr
for _, r := range l.Results { for _, r := range l.Results {
exprs = append(exprs, r) e := cs.parseExpr(r)
block.ir.Stmts = append(block.ir.Stmts, shaderir.Stmt{
Type: shaderir.Assign,
Exprs: []shaderir.Expr{
{
Type: shaderir.LocalVariable,
Index: 1, // TODO: Fix this
},
e,
},
})
} }
/*block.stmts = append(block.stmts, stmt{ block.ir.Stmts = append(block.ir.Stmts, shaderir.Stmt{
stmtType: stmtReturn, Type: shaderir.Return,
exprs: exprs, })
})*/
} }
} }
@ -346,3 +368,23 @@ func (s *compileState) detectType(b *block, expr ast.Expr) typ {
return typ{} return typ{}
} }
} }
func (cs *compileState) parseExpr(expr ast.Expr) shaderir.Expr {
switch e := expr.(type) {
case *ast.CallExpr:
return shaderir.Expr{
Type: shaderir.Call,
Exprs: []shaderir.Expr{
cs.parseExpr(e.Fun),
},
}
case *ast.Ident:
return shaderir.Expr{
Type: shaderir.BuiltinFuncExpr,
BuiltinFunc: shaderir.BuiltinFunc(e.Name),
}
default:
cs.addError(expr.Pos(), fmt.Sprintf("detecting expression not implemented: %#v", e))
}
return shaderir.Expr{}
}

View File

@ -49,6 +49,22 @@ func Foo(foo vec2) vec4 {
VS: `void F0(in vec2 l0, out vec4 l1) { VS: `void F0(in vec2 l0, out vec4 l1) {
}`, }`,
FS: `void F0(in vec2 l0, out vec4 l1) { FS: `void F0(in vec2 l0, out vec4 l1) {
}`,
},
{
Name: "func body",
Src: `package main
func Foo(foo vec2) vec4 {
return vec4(foo, 0, 1);
}`,
VS: `void F0(in vec2 l0, out vec4 l1) {
l1 = vec4(l0, 0, 1);
return;
}`,
FS: `void F0(in vec2 l0, out vec4 l1) {
l1 = vec4(l0, 0, 1);
return;
}`, }`,
}, },
} }