mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-26 10:42:42 +01:00
internal/shader: refactoring
This commit is contained in:
parent
bf0f3d304b
commit
8c879c7bcf
@ -442,62 +442,66 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, r := range stmt.Results {
|
var exprs []shaderir.Expr
|
||||||
exprs, ts, ss, ok := cs.parseExpr(block, r, true)
|
var types []shaderir.Type
|
||||||
|
for _, r := range stmt.Results {
|
||||||
|
es, ts, ss, ok := cs.parseExpr(block, r, true)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
stmts = append(stmts, ss...)
|
stmts = append(stmts, ss...)
|
||||||
|
|
||||||
if len(exprs) > 1 {
|
if len(es) > 1 && (len(stmt.Results) > 1 || len(outParams) == 1) {
|
||||||
if len(stmt.Results) > 1 || len(outParams) == 1 {
|
cs.addError(r.Pos(), "single-value context and multiple-value context cannot be mixed")
|
||||||
cs.addError(r.Pos(), "single-value context and multiple-value context cannot be mixed")
|
return nil, false
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(outParams) > 1 && len(stmt.Results) == 1 {
|
if len(outParams) > 1 && len(stmt.Results) == 1 {
|
||||||
if len(exprs) == 1 {
|
if len(es) == 1 {
|
||||||
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(stmt.Results)))
|
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(stmt.Results)))
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
if len(exprs) > 1 && len(exprs) != len(outParams) {
|
if len(es) > 1 && len(es) != len(outParams) {
|
||||||
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(exprs)))
|
cs.addError(stmt.Pos(), fmt.Sprintf("the number of returning variables must be %d but %d", len(outParams), len(es)))
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for j, t := range ts {
|
exprs = append(exprs, es...)
|
||||||
expr := exprs[j]
|
types = append(types, ts...)
|
||||||
if expr.Type == shaderir.NumberExpr {
|
|
||||||
switch outParams[i+j].typ.Main {
|
|
||||||
case shaderir.Int:
|
|
||||||
if !cs.forceToInt(stmt, &expr) {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
t = shaderir.Type{Main: shaderir.Int}
|
|
||||||
case shaderir.Float:
|
|
||||||
t = shaderir.Type{Main: shaderir.Float}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !t.Equal(&outParams[i+j].typ) {
|
|
||||||
cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outParams[i].typ))
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
stmts = append(stmts, shaderir.Stmt{
|
|
||||||
Type: shaderir.Assign,
|
|
||||||
Exprs: []shaderir.Expr{
|
|
||||||
{
|
|
||||||
Type: shaderir.LocalVariable,
|
|
||||||
Index: len(inParams) + i + j,
|
|
||||||
},
|
|
||||||
expr,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for i, t := range types {
|
||||||
|
expr := exprs[i]
|
||||||
|
if expr.Type == shaderir.NumberExpr {
|
||||||
|
switch outParams[i].typ.Main {
|
||||||
|
case shaderir.Int:
|
||||||
|
if !cs.forceToInt(stmt, &expr) {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
t = shaderir.Type{Main: shaderir.Int}
|
||||||
|
case shaderir.Float:
|
||||||
|
t = shaderir.Type{Main: shaderir.Float}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !t.Equal(&outParams[i].typ) {
|
||||||
|
cs.addError(stmt.Pos(), fmt.Sprintf("cannot use type %s as type %s in return argument", t.String(), &outParams[i].typ))
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
stmts = append(stmts, shaderir.Stmt{
|
||||||
|
Type: shaderir.Assign,
|
||||||
|
Exprs: []shaderir.Expr{
|
||||||
|
{
|
||||||
|
Type: shaderir.LocalVariable,
|
||||||
|
Index: len(inParams) + i,
|
||||||
|
},
|
||||||
|
expr,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
stmts = append(stmts, shaderir.Stmt{
|
stmts = append(stmts, shaderir.Stmt{
|
||||||
Type: shaderir.Return,
|
Type: shaderir.Return,
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user