2020-07-04 16:47:23 +02:00
|
|
|
// Copyright 2020 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package shader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/ast"
|
2020-07-12 16:07:30 +02:00
|
|
|
gconstant "go/constant"
|
2020-07-04 16:47:23 +02:00
|
|
|
"go/token"
|
2020-07-04 20:23:56 +02:00
|
|
|
"strings"
|
2020-07-04 16:47:23 +02:00
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/shaderir"
|
|
|
|
)
|
|
|
|
|
2020-08-09 16:40:41 +02:00
|
|
|
func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inParams []variable) ([]shaderir.Stmt, bool) {
|
2020-07-04 17:03:21 +02:00
|
|
|
var stmts []shaderir.Stmt
|
|
|
|
|
2020-07-04 16:47:23 +02:00
|
|
|
switch stmt := stmt.(type) {
|
|
|
|
case *ast.AssignStmt:
|
|
|
|
switch stmt.Tok {
|
|
|
|
case token.DEFINE:
|
|
|
|
if len(stmt.Lhs) != len(stmt.Rhs) && len(stmt.Rhs) != 1 {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("single-value context and multiple-value context cannot be mixed"))
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
|
2020-07-04 17:03:21 +02:00
|
|
|
ss, ok := cs.assign(block, stmt.Pos(), stmt.Lhs, stmt.Rhs, true)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
case token.ASSIGN:
|
|
|
|
// TODO: What about the statement `a,b = b,a?`
|
|
|
|
if len(stmt.Lhs) != len(stmt.Rhs) && len(stmt.Rhs) != 1 {
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("single-value context and multiple-value context cannot be mixed"))
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
ss, ok := cs.assign(block, stmt.Pos(), stmt.Lhs, stmt.Rhs, false)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
case token.ADD_ASSIGN, token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN, token.REM_ASSIGN:
|
|
|
|
var op shaderir.Op
|
|
|
|
switch stmt.Tok {
|
|
|
|
case token.ADD_ASSIGN:
|
|
|
|
op = shaderir.Add
|
|
|
|
case token.SUB_ASSIGN:
|
|
|
|
op = shaderir.Sub
|
|
|
|
case token.MUL_ASSIGN:
|
|
|
|
op = shaderir.Mul
|
|
|
|
case token.QUO_ASSIGN:
|
|
|
|
op = shaderir.Div
|
|
|
|
case token.REM_ASSIGN:
|
|
|
|
op = shaderir.ModOp
|
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
|
|
|
|
rhs, _, ss, ok := cs.parseExpr(block, stmt.Rhs[0])
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
|
|
|
|
|
|
|
lhs, _, ss, ok := cs.parseExpr(block, stmt.Lhs[0])
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
|
|
|
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-04 16:47:23 +02:00
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
lhs[0],
|
|
|
|
{
|
|
|
|
Type: shaderir.Binary,
|
|
|
|
Op: op,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
lhs[0],
|
|
|
|
rhs[0],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("unexpected token: %s", stmt.Tok))
|
|
|
|
}
|
|
|
|
case *ast.BlockStmt:
|
2020-08-09 16:40:41 +02:00
|
|
|
b, ok := cs.parseBlock(block, fname, stmt.List, inParams, nil)
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-04 16:47:23 +02:00
|
|
|
Type: shaderir.BlockStmt,
|
2020-08-09 08:55:59 +02:00
|
|
|
Blocks: []*shaderir.Block{
|
2020-07-04 16:47:23 +02:00
|
|
|
b.ir,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
case *ast.DeclStmt:
|
2020-07-12 08:51:06 +02:00
|
|
|
ss, ok := cs.parseDecl(block, stmt.Decl)
|
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-12 08:51:06 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-11 14:08:19 +02:00
|
|
|
|
|
|
|
case *ast.ForStmt:
|
|
|
|
msg := "for-statement must follow this format: for (varname) := (constant); (varname) (op) (constant); (varname) (op) (constant) { ..."
|
|
|
|
if stmt.Init == nil {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if stmt.Cond == nil {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if stmt.Post == nil {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new pseudo block for the initial statement, so that the counter variable belongs to the
|
|
|
|
// new pseudo block for each for-loop. Without this, the samely named counter variables in different
|
|
|
|
// for-loops confuses the parser.
|
2020-08-09 16:40:41 +02:00
|
|
|
pseudoBlock, ok := cs.parseBlock(block, fname, []ast.Stmt{stmt.Init}, inParams, nil)
|
2020-07-11 14:08:19 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
ss := pseudoBlock.ir.Stmts
|
|
|
|
|
|
|
|
if len(ss) != 1 {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if ss[0].Type != shaderir.Assign {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if ss[0].Exprs[0].Type != shaderir.LocalVariable {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
varidx := ss[0].Exprs[0].Index
|
|
|
|
if ss[0].Exprs[1].Type != shaderir.NumberExpr {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
vartype := pseudoBlock.vars[0].typ
|
|
|
|
init := ss[0].Exprs[1].Const
|
|
|
|
|
|
|
|
exprs, ts, ss, ok := cs.parseExpr(pseudoBlock, stmt.Cond)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if len(exprs) != 1 {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if len(ts) != 1 || ts[0].Main != shaderir.Bool {
|
|
|
|
cs.addError(stmt.Pos(), "for-statement's condition must be bool")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if len(ss) != 0 {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if exprs[0].Type != shaderir.Binary {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
op := exprs[0].Op
|
|
|
|
if op != shaderir.LessThanOp && op != shaderir.LessThanEqualOp && op != shaderir.GreaterThanOp && op != shaderir.GreaterThanEqualOp && op != shaderir.EqualOp && op != shaderir.NotEqualOp {
|
|
|
|
cs.addError(stmt.Pos(), "for-statement's condition must have one of these operators: <, <=, >, >=, ==, !=")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if exprs[0].Exprs[0].Type != shaderir.LocalVariable {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if exprs[0].Exprs[0].Index != varidx {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if exprs[0].Exprs[1].Type != shaderir.NumberExpr {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
end := exprs[0].Exprs[1].Const
|
|
|
|
|
2020-08-09 16:40:41 +02:00
|
|
|
postSs, ok := cs.parseStmt(pseudoBlock, fname, stmt.Post, inParams)
|
2020-07-11 14:08:19 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if len(postSs) != 1 {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Type != shaderir.Assign {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[0].Type != shaderir.LocalVariable {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[0].Index != varidx {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[1].Type != shaderir.Binary {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[1].Exprs[0].Type != shaderir.LocalVariable {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[1].Exprs[0].Index != varidx {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if postSs[0].Exprs[1].Exprs[1].Type != shaderir.NumberExpr {
|
|
|
|
cs.addError(stmt.Pos(), msg)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
delta := postSs[0].Exprs[1].Exprs[1].Const
|
|
|
|
switch postSs[0].Exprs[1].Op {
|
|
|
|
case shaderir.Add:
|
|
|
|
case shaderir.Sub:
|
|
|
|
delta = gconstant.UnaryOp(token.SUB, delta, 0)
|
|
|
|
default:
|
|
|
|
cs.addError(stmt.Pos(), "for-statement's post statement must have one of these operators: +=, -=, ++, --")
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2020-08-09 16:40:41 +02:00
|
|
|
b, ok := cs.parseBlock(pseudoBlock, fname, []ast.Stmt{stmt.Body}, inParams, nil)
|
2020-07-11 14:08:19 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
bodyir := b.ir
|
|
|
|
for len(bodyir.Stmts) == 1 && bodyir.Stmts[0].Type == shaderir.BlockStmt {
|
|
|
|
bodyir = bodyir.Stmts[0].Blocks[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
// As the pseudo block is not actually used, copy the variable part to the actual block.
|
|
|
|
// This must be done after parsing the for-loop is done, or the duplicated variables confuses the
|
|
|
|
// parsing.
|
2020-08-09 16:40:41 +02:00
|
|
|
v := pseudoBlock.vars[0]
|
|
|
|
v.forLoopCounter = true
|
|
|
|
block.vars = append(block.vars, v)
|
2020-07-11 14:08:19 +02:00
|
|
|
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.For,
|
2020-08-09 08:55:59 +02:00
|
|
|
Blocks: []*shaderir.Block{bodyir},
|
2020-07-11 14:08:19 +02:00
|
|
|
ForVarType: vartype,
|
|
|
|
ForVarIndex: varidx,
|
|
|
|
ForInit: init,
|
|
|
|
ForEnd: end,
|
|
|
|
ForOp: op,
|
|
|
|
ForDelta: delta,
|
|
|
|
})
|
|
|
|
|
2020-07-04 15:49:29 +02:00
|
|
|
case *ast.IfStmt:
|
2020-07-04 20:29:34 +02:00
|
|
|
if stmt.Init != nil {
|
|
|
|
init := stmt.Init
|
|
|
|
stmt.Init = nil
|
2020-08-09 16:40:41 +02:00
|
|
|
b, ok := cs.parseBlock(block, fname, []ast.Stmt{init, stmt}, inParams, nil)
|
2020-07-04 20:29:34 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-11 14:08:19 +02:00
|
|
|
Type: shaderir.BlockStmt,
|
2020-08-09 08:55:59 +02:00
|
|
|
Blocks: []*shaderir.Block{b.ir},
|
2020-07-04 20:29:34 +02:00
|
|
|
})
|
|
|
|
return stmts, true
|
|
|
|
}
|
2020-07-04 15:49:29 +02:00
|
|
|
|
|
|
|
exprs, ts, ss, ok := cs.parseExpr(block, stmt.Cond)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
if len(ts) != 1 || ts[0].Main != shaderir.Bool {
|
2020-07-04 20:23:56 +02:00
|
|
|
var tss []string
|
|
|
|
for _, t := range ts {
|
|
|
|
tss = append(tss, t.String())
|
|
|
|
}
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("if-condition must be bool but: %s", strings.Join(tss, ", ")))
|
2020-07-04 15:49:29 +02:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
stmts = append(stmts, ss...)
|
|
|
|
|
2020-08-09 08:55:59 +02:00
|
|
|
var bs []*shaderir.Block
|
2020-08-09 16:40:41 +02:00
|
|
|
b, ok := cs.parseBlock(block, fname, stmt.Body.List, inParams, nil)
|
2020-07-04 15:49:29 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
bs = append(bs, b.ir)
|
|
|
|
|
|
|
|
if stmt.Else != nil {
|
|
|
|
switch s := stmt.Else.(type) {
|
|
|
|
case *ast.BlockStmt:
|
2020-08-09 16:40:41 +02:00
|
|
|
b, ok := cs.parseBlock(block, fname, s.List, inParams, nil)
|
2020-07-04 15:49:29 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
bs = append(bs, b.ir)
|
|
|
|
default:
|
2020-08-09 16:40:41 +02:00
|
|
|
b, ok := cs.parseBlock(block, fname, []ast.Stmt{s}, inParams, nil)
|
2020-07-04 15:49:29 +02:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
bs = append(bs, b.ir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.If,
|
|
|
|
Exprs: exprs,
|
|
|
|
Blocks: bs,
|
|
|
|
})
|
2020-07-11 14:08:19 +02:00
|
|
|
|
2020-07-12 16:07:30 +02:00
|
|
|
case *ast.IncDecStmt:
|
|
|
|
exprs, _, ss, ok := cs.parseExpr(block, stmt.X)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
stmts = append(stmts, ss...)
|
|
|
|
var op shaderir.Op
|
|
|
|
switch stmt.Tok {
|
|
|
|
case token.INC:
|
|
|
|
op = shaderir.Add
|
|
|
|
case token.DEC:
|
|
|
|
op = shaderir.Sub
|
|
|
|
}
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
exprs[0],
|
|
|
|
{
|
|
|
|
Type: shaderir.Binary,
|
|
|
|
Op: op,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
exprs[0],
|
|
|
|
{
|
|
|
|
Type: shaderir.NumberExpr,
|
|
|
|
Const: gconstant.MakeInt64(1),
|
|
|
|
ConstType: shaderir.ConstTypeInt,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2020-07-11 14:08:19 +02:00
|
|
|
|
2020-07-04 16:47:23 +02:00
|
|
|
case *ast.ReturnStmt:
|
|
|
|
for i, r := range stmt.Results {
|
2020-07-04 17:03:21 +02:00
|
|
|
exprs, _, ss, ok := cs.parseExpr(block, r)
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
if len(exprs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if len(exprs) > 1 {
|
|
|
|
cs.addError(r.Pos(), "multiple-context with return is not implemented yet")
|
|
|
|
continue
|
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-04 16:47:23 +02:00
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{
|
|
|
|
{
|
|
|
|
Type: shaderir.LocalVariable,
|
|
|
|
Index: len(inParams) + i,
|
|
|
|
},
|
|
|
|
exprs[0],
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-04 16:47:23 +02:00
|
|
|
Type: shaderir.Return,
|
|
|
|
})
|
2020-07-11 14:08:19 +02:00
|
|
|
|
2020-07-12 19:59:11 +02:00
|
|
|
case *ast.BranchStmt:
|
|
|
|
switch stmt.Tok {
|
|
|
|
case token.BREAK:
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Break,
|
|
|
|
})
|
|
|
|
case token.CONTINUE:
|
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
|
|
|
Type: shaderir.Continue,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("invalid token: %s", stmt.Tok))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2020-07-04 16:47:23 +02:00
|
|
|
case *ast.ExprStmt:
|
2020-07-04 17:03:21 +02:00
|
|
|
exprs, _, ss, ok := cs.parseExpr(block, stmt.X)
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
|
|
|
|
2020-07-04 16:47:23 +02:00
|
|
|
for _, expr := range exprs {
|
|
|
|
if expr.Type != shaderir.Call {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-04 16:47:23 +02:00
|
|
|
Type: shaderir.ExprStmt,
|
|
|
|
Exprs: []shaderir.Expr{expr},
|
|
|
|
})
|
|
|
|
}
|
2020-07-11 14:08:19 +02:00
|
|
|
|
2020-07-04 16:47:23 +02:00
|
|
|
default:
|
|
|
|
cs.addError(stmt.Pos(), fmt.Sprintf("unexpected statement: %#v", stmt))
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
return stmts, true
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
|
2020-07-04 17:03:21 +02:00
|
|
|
func (cs *compileState) assign(block *block, pos token.Pos, lhs, rhs []ast.Expr, define bool) ([]shaderir.Stmt, bool) {
|
|
|
|
var stmts []shaderir.Stmt
|
2020-07-04 16:47:23 +02:00
|
|
|
var rhsExprs []shaderir.Expr
|
|
|
|
var rhsTypes []shaderir.Type
|
|
|
|
|
|
|
|
for i, e := range lhs {
|
|
|
|
if len(lhs) == len(rhs) {
|
|
|
|
// Prase RHS first for the order of the statements.
|
2020-07-04 17:03:21 +02:00
|
|
|
r, origts, ss, ok := cs.parseExpr(block, rhs[i])
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
|
|
|
|
if define {
|
|
|
|
v := variable{
|
|
|
|
name: e.(*ast.Ident).Name,
|
|
|
|
}
|
|
|
|
ts, ok := cs.functionReturnTypes(block, rhs[i])
|
|
|
|
if !ok {
|
|
|
|
ts = origts
|
|
|
|
}
|
|
|
|
if len(ts) > 1 {
|
|
|
|
cs.addError(pos, fmt.Sprintf("single-value context and multiple-value context cannot be mixed"))
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
if len(ts) == 1 {
|
|
|
|
v.typ = ts[0]
|
|
|
|
}
|
|
|
|
block.vars = append(block.vars, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r) > 1 {
|
|
|
|
cs.addError(pos, fmt.Sprintf("single-value context and multiple-value context cannot be mixed"))
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
|
2020-07-04 17:03:21 +02:00
|
|
|
l, _, ss, ok := cs.parseExpr(block, lhs[i])
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
|
|
|
|
if r[0].Type == shaderir.NumberExpr {
|
2020-07-12 11:40:01 +02:00
|
|
|
t, ok := block.findLocalVariableByIndex(l[0].Index)
|
|
|
|
if !ok {
|
|
|
|
cs.addError(pos, fmt.Sprintf("unexpected local variable index: %d", l[0].Index))
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
switch t.Main {
|
2020-07-04 16:47:23 +02:00
|
|
|
case shaderir.Int:
|
|
|
|
r[0].ConstType = shaderir.ConstTypeInt
|
|
|
|
case shaderir.Float:
|
|
|
|
r[0].ConstType = shaderir.ConstTypeFloat
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-04 16:47:23 +02:00
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{l[0], r[0]},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if i == 0 {
|
2020-07-04 17:03:21 +02:00
|
|
|
var ss []shaderir.Stmt
|
2020-07-04 16:47:23 +02:00
|
|
|
var ok bool
|
2020-07-04 17:03:21 +02:00
|
|
|
rhsExprs, rhsTypes, ss, ok = cs.parseExpr(block, rhs[0])
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
if len(rhsExprs) != len(lhs) {
|
|
|
|
cs.addError(pos, fmt.Sprintf("single-value context and multiple-value context cannot be mixed"))
|
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if define {
|
|
|
|
v := variable{
|
|
|
|
name: e.(*ast.Ident).Name,
|
|
|
|
}
|
|
|
|
v.typ = rhsTypes[i]
|
|
|
|
block.vars = append(block.vars, v)
|
|
|
|
}
|
|
|
|
|
2020-07-04 17:03:21 +02:00
|
|
|
l, _, ss, ok := cs.parseExpr(block, lhs[i])
|
2020-07-04 16:47:23 +02:00
|
|
|
if !ok {
|
2020-07-04 17:03:21 +02:00
|
|
|
return nil, false
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, ss...)
|
2020-07-04 16:47:23 +02:00
|
|
|
|
2020-07-04 17:03:21 +02:00
|
|
|
stmts = append(stmts, shaderir.Stmt{
|
2020-07-04 16:47:23 +02:00
|
|
|
Type: shaderir.Assign,
|
|
|
|
Exprs: []shaderir.Expr{l[0], rhsExprs[i]},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-04 17:03:21 +02:00
|
|
|
return stmts, true
|
2020-07-04 16:47:23 +02:00
|
|
|
}
|