diff --git a/internal/shader/shader.go b/internal/shader/shader.go index 0ad3c31f9..9178cc708 100644 --- a/internal/shader/shader.go +++ b/internal/shader/shader.go @@ -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 } diff --git a/internal/shaderir/program.go b/internal/shaderir/program.go index 8613ffe5e..1f580dc1d 100644 --- a/internal/shaderir/program.go +++ b/internal/shaderir/program.go @@ -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 (