shaderir: Refactoring: Add OpFromToken

This commit is contained in:
Hajime Hoshi 2020-06-20 23:26:29 +09:00
parent 28948a9976
commit 4b5de9f445
2 changed files with 50 additions and 41 deletions

View File

@ -786,47 +786,8 @@ func (cs *compileState) parseExpr(block *block, expr ast.Expr) ([]shaderir.Expr,
cs.addError(e.Pos(), fmt.Sprintf("literal not implemented: %#v", e))
}
case *ast.BinaryExpr:
var op shaderir.Op
switch e.Op {
case token.ADD:
op = shaderir.Add
case token.SUB:
op = shaderir.Sub
case token.NOT:
op = shaderir.NotOp
case token.MUL:
op = shaderir.Mul
case token.QUO:
op = shaderir.Div
case token.REM:
op = shaderir.ModOp
case token.SHL:
op = shaderir.LeftShift
case token.SHR:
op = shaderir.RightShift
case token.LSS:
op = shaderir.LessThanOp
case token.LEQ:
op = shaderir.LessThanEqualOp
case token.GTR:
op = shaderir.GreaterThanOp
case token.GEQ:
op = shaderir.GreaterThanEqualOp
case token.EQL:
op = shaderir.EqualOp
case token.NEQ:
op = shaderir.NotEqualOp
case token.AND:
op = shaderir.And
case token.XOR:
op = shaderir.Xor
case token.OR:
op = shaderir.Or
case token.LAND:
op = shaderir.AndAnd
case token.LOR:
op = shaderir.OrOr
default:
op, ok := shaderir.OpFromToken(e.Op)
if !ok {
cs.addError(e.Pos(), fmt.Sprintf("unexpected operator: %s", e.Op))
return nil, nil, nil
}

View File

@ -15,6 +15,10 @@
// Package shaderir offers intermediate representation for shader programs.
package shaderir
import (
"go/token"
)
type Program struct {
Uniforms []Type
Attributes []Type
@ -135,6 +139,50 @@ const (
OrOr Op = "||"
)
func OpFromToken(t token.Token) (Op, bool) {
switch t {
case token.ADD:
return Add, true
case token.SUB:
return Sub, true
case token.NOT:
return NotOp, true
case token.MUL:
return Mul, true
case token.QUO:
return Div, true
case token.REM:
return ModOp, true
case token.SHL:
return LeftShift, true
case token.SHR:
return RightShift, true
case token.LSS:
return LessThanOp, true
case token.LEQ:
return LessThanEqualOp, true
case token.GTR:
return GreaterThanOp, true
case token.GEQ:
return GreaterThanEqualOp, true
case token.EQL:
return EqualOp, true
case token.NEQ:
return NotEqualOp, true
case token.AND:
return And, true
case token.XOR:
return Xor, true
case token.OR:
return Or, true
case token.LAND:
return AndAnd, true
case token.LOR:
return OrOr, true
}
return "", false
}
type BuiltinFunc string
const (