shadeir: Add array composite literals

Updates #1235
This commit is contained in:
Hajime Hoshi 2020-07-29 23:25:16 +09:00
parent 004b279e4f
commit a0549820fd
3 changed files with 68 additions and 0 deletions

View File

@ -446,6 +446,59 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
Exprs: exprs, Exprs: exprs,
}, },
}, t, stmts, true }, t, stmts, true
case *ast.CompositeLit:
t, ok := cs.parseType(block, e.Type)
if !ok {
return nil, nil, nil, false
}
fmt.Println(t)
idx := len(block.vars)
block.vars = append(block.vars, variable{
typ: t,
})
var stmts []shaderir.Stmt
for i, e := range e.Elts {
exprs, _, ss, ok := cs.parseExpr(block, e)
if !ok {
return nil, nil, nil, false
}
if len(exprs) != 1 {
cs.addError(e.Pos(), fmt.Sprintf("multiple-value context is not available at a composite literal"))
return nil, nil, nil, false
}
stmts = append(stmts, ss...)
stmts = append(stmts, shaderir.Stmt{
Type: shaderir.Assign,
Exprs: []shaderir.Expr{
{
Type: shaderir.Index,
Exprs: []shaderir.Expr{
{
Type: shaderir.LocalVariable,
Index: idx,
},
{
Type: shaderir.NumberExpr,
Const: gconstant.MakeInt64(int64(i)),
ConstType: shaderir.ConstTypeInt,
},
},
},
exprs[0],
},
})
}
return []shaderir.Expr{
{
Type: shaderir.LocalVariable,
Index: idx,
},
}, []shaderir.Type{t}, stmts, true
default: default:
cs.addError(e.Pos(), fmt.Sprintf("expression not implemented: %#v", e)) cs.addError(e.Pos(), fmt.Sprintf("expression not implemented: %#v", e))
} }

View File

@ -1,9 +1,19 @@
uniform vec2[4] U0; uniform vec2[4] U0;
void F0(out vec2[2] l0); void F0(out vec2[2] l0);
void F1(out vec2[2] l0);
void F0(out vec2[2] l0) { void F0(out vec2[2] l0) {
vec2[2] l1 = vec2[2](vec2(0), vec2(0)); vec2[2] l1 = vec2[2](vec2(0), vec2(0));
l0 = l1; l0 = l1;
return; return;
} }
void F1(out vec2[2] l0) {
vec2[2] l1 = vec2[2](vec2(0), vec2(0));
vec2[2] l2 = vec2[2](vec2(0), vec2(0));
(l1)[0] = vec2(1.0);
l2 = l1;
l0 = l2;
return;
}

View File

@ -6,3 +6,8 @@ func Foo() [2]vec2 {
var x [2]vec2 var x [2]vec2
return x return x
} }
func Bar() [2]vec2 {
x := [2]vec2{vec2(1)}
return x
}