diff --git a/cmd/wuffs-c/internal/cgen/expr.go b/cmd/wuffs-c/internal/cgen/expr.go index f5e404d9..e083fe70 100644 --- a/cmd/wuffs-c/internal/cgen/expr.go +++ b/cmd/wuffs-c/internal/cgen/expr.go @@ -57,28 +57,15 @@ func (g *gen) writeExpr(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthes return nil } - switch n.Operator().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { - case 0: - if err := g.writeExprOther(b, n, rp, pp, depth); err != nil { - return err - } - case t.FlagsUnaryOp: - if err := g.writeExprUnaryOp(b, n, rp, pp, depth); err != nil { - return err - } - case t.FlagsBinaryOp: - if err := g.writeExprBinaryOp(b, n, rp, pp, depth); err != nil { - return err - } - case t.FlagsAssociativeOp: - if err := g.writeExprAssociativeOp(b, n, rp, pp, depth); err != nil { - return err - } - default: - return fmt.Errorf("unrecognized token.Key (0x%X) for writeExpr", n.Operator().Key()) + switch op := n.Operator(); { + case op.IsXUnaryOp(): + return g.writeExprUnaryOp(b, n, rp, pp, depth) + case op.IsXBinaryOp(): + return g.writeExprBinaryOp(b, n, rp, pp, depth) + case op.IsXAssociativeOp(): + return g.writeExprAssociativeOp(b, n, rp, pp, depth) } - - return nil + return g.writeExprOther(b, n, rp, pp, depth) } func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error { diff --git a/lang/ast/string.go b/lang/ast/string.go index e6514ef6..4437abe1 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -36,8 +36,8 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32) depth++ if n != nil { - switch n.id0.Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { - case 0: + switch { + default: switch n.id0.Key() { case t.KeyError, t.KeyStatus, t.KeySuspension: switch n.id0.Key() { @@ -109,11 +109,11 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32) buf = append(buf, ')') } - case t.FlagsUnaryOp: + case n.id0.IsXUnaryOp(): buf = append(buf, opStrings[0xFF&n.id0.Key()]...) buf = n.rhs.Expr().appendStr(buf, tm, true, depth) - case t.FlagsBinaryOp: + case n.id0.IsXBinaryOp(): if parenthesize { buf = append(buf, '(') } @@ -128,7 +128,7 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32) buf = append(buf, ')') } - case t.FlagsAssociativeOp: + case n.id0.IsXAssociativeOp(): if parenthesize { buf = append(buf, '(') } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index f3c4c78a..e99f0730 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -755,20 +755,19 @@ func (q *checker) bcheckExpr1(n *a.Expr, depth uint32) (*big.Int, *big.Int, erro if cv := n.ConstValue(); cv != nil { return cv, cv, nil } - switch n.Operator().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { - case 0: - return q.bcheckExprOther(n, depth) - case t.FlagsUnaryOp: + switch op := n.Operator(); { + case op.IsXUnaryOp(): return q.bcheckExprUnaryOp(n, depth) - case t.FlagsBinaryOp: - if n.Operator().Key() == t.KeyXBinaryAs { + case op.IsXBinaryOp(): + if op.Key() == t.KeyXBinaryAs { return q.bcheckExpr(n.LHS().Expr(), depth) } - return q.bcheckExprBinaryOp(n.Operator().Key(), n.LHS().Expr(), n.RHS().Expr(), depth) - case t.FlagsAssociativeOp: + return q.bcheckExprBinaryOp(op.Key(), n.LHS().Expr(), n.RHS().Expr(), depth) + case op.IsXAssociativeOp(): return q.bcheckExprAssociativeOp(n, depth) } - return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExpr", n.Operator().Key()) + + return q.bcheckExprOther(n, depth) } func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) { diff --git a/lang/check/type.go b/lang/check/type.go index 07a71289..34bfa71c 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -349,25 +349,23 @@ func (q *checker) tcheckExpr(n *a.Expr, depth uint32) error { } depth++ - switch n.Operator().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) { - case 0: - if err := q.tcheckExprOther(n, depth); err != nil { - return err - } - case t.FlagsUnaryOp: + switch op := n.Operator(); { + case op.IsXUnaryOp(): if err := q.tcheckExprUnaryOp(n, depth); err != nil { return err } - case t.FlagsBinaryOp: + case op.IsXBinaryOp(): if err := q.tcheckExprBinaryOp(n, depth); err != nil { return err } - case t.FlagsAssociativeOp: + case op.IsXAssociativeOp(): if err := q.tcheckExprAssociativeOp(n, depth); err != nil { return err } default: - return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExpr", n.Operator().Key()) + if err := q.tcheckExprOther(n, depth); err != nil { + return err + } } n.Node().SetTypeChecked() return nil diff --git a/lang/render/render.go b/lang/render/render.go index 0e81dcfc..52ac2edd 100644 --- a/lang/render/render.go +++ b/lang/render/render.go @@ -107,10 +107,6 @@ func Render(w io.Writer, tm *t.Map, src []t.Token, comments []string) (err error // Render the lineTokens. prevID, prevIsTightRight := t.ID(0), false for _, tok := range lineTokens { - const ( - flagsUB = t.FlagsUnaryOp | t.FlagsBinaryOp - ) - if prevID != 0 && !prevIsTightRight && !tok.ID.IsTightLeft() { // The "(" token's tight-left-ness is context dependent. For // "f(x)", the "(" is tight-left. For "a * (b + c)", it is not. @@ -136,7 +132,7 @@ func Render(w io.Writer, tm *t.Map, src []t.Token, comments []string) (err error prevIsTightRight = tok.ID.IsTightRight() // The "+" and "-" tokens' tight-right-ness is context dependent. // The unary flavor is tight-right, the binary flavor is not. - if prevID != 0 && tok.ID.Flags()&flagsUB == flagsUB { + if prevID != 0 && tok.ID.IsUnaryOp() && tok.ID.IsBinaryOp() { // Token-based (not ast.Node-based) heuristic for whether the // operator looks unary instead of binary. prevIsTightRight = !isCloseIdentLiteral(prevID) diff --git a/lang/token/list.go b/lang/token/list.go index ad35f82f..2b6d983c 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -30,7 +30,6 @@ type Flags uint32 const ( FlagsOther = Flags(0x0001) - FlagsUnaryOp = Flags(0x0002) FlagsBinaryOp = Flags(0x0004) FlagsAssociativeOp = Flags(0x0008) FlagsLiteral = Flags(0x0010) @@ -59,6 +58,7 @@ type ID uint32 // Str returns a string form of x. func (x ID) Str(m *Map) string { return m.ByID(x) } +// TODO: the XxxForm methods should return 0 if x > 0xFF. func (x ID) AmbiguousForm() ID { return ambiguousForms[0xFF&(x>>KeyShift)] } func (x ID) UnaryForm() ID { return unaryForms[0xFF&(x>>KeyShift)] } func (x ID) BinaryForm() ID { return binaryForms[0xFF&(x>>KeyShift)] } @@ -69,7 +69,7 @@ func (x ID) Flags() Flags { return Flags(x & FlagsMask) } func (x ID) IsBuiltIn() bool { return Key(x>>KeyShift) < nBuiltInKeys } -func (x ID) IsUnaryOp() bool { return Flags(x)&FlagsUnaryOp != 0 } +func (x ID) IsUnaryOp() bool { return x.Key() <= 0xFF && x.UnaryForm() != 0 } func (x ID) IsBinaryOp() bool { return Flags(x)&FlagsBinaryOp != 0 } func (x ID) IsAssociativeOp() bool { return Flags(x)&FlagsAssociativeOp != 0 } @@ -381,8 +381,8 @@ const ( IDPercentEq = ID(0x2B<