mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-25 00:55:11 +01:00
Remove token ID/Key distinction, part 2 of N
This commit is contained in:
1 parent
afd7429e9a
commit
7007682182
6 files changed
+67
-61
No files matched your search
@@ -57,28 +57,15 @@ func (g *gen) writeExpr(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthes
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch n.Operator().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
|
switch op := n.Operator(); {
|
||||||
case 0:
|
case op.IsXUnaryOp():
|
||||||
if err := g.writeExprOther(b, n, rp, pp, depth); err != nil {
|
return g.writeExprUnaryOp(b, n, rp, pp, depth)
|
||||||
return err
|
case op.IsXBinaryOp():
|
||||||
}
|
return g.writeExprBinaryOp(b, n, rp, pp, depth)
|
||||||
case t.FlagsUnaryOp:
|
case op.IsXAssociativeOp():
|
||||||
if err := g.writeExprUnaryOp(b, n, rp, pp, depth); err != nil {
|
return g.writeExprAssociativeOp(b, n, rp, pp, depth)
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
|
return g.writeExprOther(b, n, rp, pp, depth)
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp parenthesesPolicy, depth uint32) error {
|
||||||
|
|||||||
+5
-5
@@ -36,8 +36,8 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32)
|
|||||||
depth++
|
depth++
|
||||||
|
|
||||||
if n != nil {
|
if n != nil {
|
||||||
switch n.id0.Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
|
switch {
|
||||||
case 0:
|
default:
|
||||||
switch n.id0.Key() {
|
switch n.id0.Key() {
|
||||||
case t.KeyError, t.KeyStatus, t.KeySuspension:
|
case t.KeyError, t.KeyStatus, t.KeySuspension:
|
||||||
switch n.id0.Key() {
|
switch n.id0.Key() {
|
||||||
@@ -109,11 +109,11 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32)
|
|||||||
buf = append(buf, ')')
|
buf = append(buf, ')')
|
||||||
}
|
}
|
||||||
|
|
||||||
case t.FlagsUnaryOp:
|
case n.id0.IsXUnaryOp():
|
||||||
buf = append(buf, opStrings[0xFF&n.id0.Key()]...)
|
buf = append(buf, opStrings[0xFF&n.id0.Key()]...)
|
||||||
buf = n.rhs.Expr().appendStr(buf, tm, true, depth)
|
buf = n.rhs.Expr().appendStr(buf, tm, true, depth)
|
||||||
|
|
||||||
case t.FlagsBinaryOp:
|
case n.id0.IsXBinaryOp():
|
||||||
if parenthesize {
|
if parenthesize {
|
||||||
buf = append(buf, '(')
|
buf = append(buf, '(')
|
||||||
}
|
}
|
||||||
@@ -128,7 +128,7 @@ func (n *Expr) appendStr(buf []byte, tm *t.Map, parenthesize bool, depth uint32)
|
|||||||
buf = append(buf, ')')
|
buf = append(buf, ')')
|
||||||
}
|
}
|
||||||
|
|
||||||
case t.FlagsAssociativeOp:
|
case n.id0.IsXAssociativeOp():
|
||||||
if parenthesize {
|
if parenthesize {
|
||||||
buf = append(buf, '(')
|
buf = append(buf, '(')
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -755,20 +755,19 @@ func (q *checker) bcheckExpr1(n *a.Expr, depth uint32) (*big.Int, *big.Int, erro
|
|||||||
if cv := n.ConstValue(); cv != nil {
|
if cv := n.ConstValue(); cv != nil {
|
||||||
return cv, cv, nil
|
return cv, cv, nil
|
||||||
}
|
}
|
||||||
switch n.Operator().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
|
switch op := n.Operator(); {
|
||||||
case 0:
|
case op.IsXUnaryOp():
|
||||||
return q.bcheckExprOther(n, depth)
|
|
||||||
case t.FlagsUnaryOp:
|
|
||||||
return q.bcheckExprUnaryOp(n, depth)
|
return q.bcheckExprUnaryOp(n, depth)
|
||||||
case t.FlagsBinaryOp:
|
case op.IsXBinaryOp():
|
||||||
if n.Operator().Key() == t.KeyXBinaryAs {
|
if op.Key() == t.KeyXBinaryAs {
|
||||||
return q.bcheckExpr(n.LHS().Expr(), depth)
|
return q.bcheckExpr(n.LHS().Expr(), depth)
|
||||||
}
|
}
|
||||||
return q.bcheckExprBinaryOp(n.Operator().Key(), n.LHS().Expr(), n.RHS().Expr(), depth)
|
return q.bcheckExprBinaryOp(op.Key(), n.LHS().Expr(), n.RHS().Expr(), depth)
|
||||||
case t.FlagsAssociativeOp:
|
case op.IsXAssociativeOp():
|
||||||
return q.bcheckExprAssociativeOp(n, depth)
|
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) {
|
func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int, error) {
|
||||||
|
|||||||
+7
-9
@@ -349,25 +349,23 @@ func (q *checker) tcheckExpr(n *a.Expr, depth uint32) error {
|
|||||||
}
|
}
|
||||||
depth++
|
depth++
|
||||||
|
|
||||||
switch n.Operator().Flags() & (t.FlagsUnaryOp | t.FlagsBinaryOp | t.FlagsAssociativeOp) {
|
switch op := n.Operator(); {
|
||||||
case 0:
|
case op.IsXUnaryOp():
|
||||||
if err := q.tcheckExprOther(n, depth); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case t.FlagsUnaryOp:
|
|
||||||
if err := q.tcheckExprUnaryOp(n, depth); err != nil {
|
if err := q.tcheckExprUnaryOp(n, depth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case t.FlagsBinaryOp:
|
case op.IsXBinaryOp():
|
||||||
if err := q.tcheckExprBinaryOp(n, depth); err != nil {
|
if err := q.tcheckExprBinaryOp(n, depth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case t.FlagsAssociativeOp:
|
case op.IsXAssociativeOp():
|
||||||
if err := q.tcheckExprAssociativeOp(n, depth); err != nil {
|
if err := q.tcheckExprAssociativeOp(n, depth); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
default:
|
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()
|
n.Node().SetTypeChecked()
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -107,10 +107,6 @@ func Render(w io.Writer, tm *t.Map, src []t.Token, comments []string) (err error
|
|||||||
// Render the lineTokens.
|
// Render the lineTokens.
|
||||||
prevID, prevIsTightRight := t.ID(0), false
|
prevID, prevIsTightRight := t.ID(0), false
|
||||||
for _, tok := range lineTokens {
|
for _, tok := range lineTokens {
|
||||||
const (
|
|
||||||
flagsUB = t.FlagsUnaryOp | t.FlagsBinaryOp
|
|
||||||
)
|
|
||||||
|
|
||||||
if prevID != 0 && !prevIsTightRight && !tok.ID.IsTightLeft() {
|
if prevID != 0 && !prevIsTightRight && !tok.ID.IsTightLeft() {
|
||||||
// The "(" token's tight-left-ness is context dependent. For
|
// The "(" token's tight-left-ness is context dependent. For
|
||||||
// "f(x)", the "(" is tight-left. For "a * (b + c)", it is not.
|
// "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()
|
prevIsTightRight = tok.ID.IsTightRight()
|
||||||
// The "+" and "-" tokens' tight-right-ness is context dependent.
|
// The "+" and "-" tokens' tight-right-ness is context dependent.
|
||||||
// The unary flavor is tight-right, the binary flavor is not.
|
// 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
|
// Token-based (not ast.Node-based) heuristic for whether the
|
||||||
// operator looks unary instead of binary.
|
// operator looks unary instead of binary.
|
||||||
prevIsTightRight = !isCloseIdentLiteral(prevID)
|
prevIsTightRight = !isCloseIdentLiteral(prevID)
|
||||||
|
|||||||
+38
-12
@@ -30,7 +30,6 @@ type Flags uint32
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
FlagsOther = Flags(0x0001)
|
FlagsOther = Flags(0x0001)
|
||||||
FlagsUnaryOp = Flags(0x0002)
|
|
||||||
FlagsBinaryOp = Flags(0x0004)
|
FlagsBinaryOp = Flags(0x0004)
|
||||||
FlagsAssociativeOp = Flags(0x0008)
|
FlagsAssociativeOp = Flags(0x0008)
|
||||||
FlagsLiteral = Flags(0x0010)
|
FlagsLiteral = Flags(0x0010)
|
||||||
@@ -59,6 +58,7 @@ type ID uint32
|
|||||||
// Str returns a string form of x.
|
// Str returns a string form of x.
|
||||||
func (x ID) Str(m *Map) string { return m.ByID(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) AmbiguousForm() ID { return ambiguousForms[0xFF&(x>>KeyShift)] }
|
||||||
func (x ID) UnaryForm() ID { return unaryForms[0xFF&(x>>KeyShift)] }
|
func (x ID) UnaryForm() ID { return unaryForms[0xFF&(x>>KeyShift)] }
|
||||||
func (x ID) BinaryForm() ID { return binaryForms[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) 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) IsBinaryOp() bool { return Flags(x)&FlagsBinaryOp != 0 }
|
||||||
func (x ID) IsAssociativeOp() bool { return Flags(x)&FlagsAssociativeOp != 0 }
|
func (x ID) IsAssociativeOp() bool { return Flags(x)&FlagsAssociativeOp != 0 }
|
||||||
|
|
||||||
@@ -381,8 +381,8 @@ const (
|
|||||||
IDPercentEq = ID(0x2B<<KeyShift | FlagsOther)
|
IDPercentEq = ID(0x2B<<KeyShift | FlagsOther)
|
||||||
IDTildePlusEq = ID(0x2C<<KeyShift | FlagsOther)
|
IDTildePlusEq = ID(0x2C<<KeyShift | FlagsOther)
|
||||||
|
|
||||||
IDPlus = ID(0x31<<KeyShift | FlagsBinaryOp | FlagsUnaryOp | FlagsAssociativeOp)
|
IDPlus = ID(0x31<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
|
||||||
IDMinus = ID(0x32<<KeyShift | FlagsBinaryOp | FlagsUnaryOp)
|
IDMinus = ID(0x32<<KeyShift | FlagsBinaryOp)
|
||||||
IDStar = ID(0x33<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
|
IDStar = ID(0x33<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
|
||||||
IDSlash = ID(0x34<<KeyShift | FlagsBinaryOp)
|
IDSlash = ID(0x34<<KeyShift | FlagsBinaryOp)
|
||||||
IDShiftL = ID(0x35<<KeyShift | FlagsBinaryOp)
|
IDShiftL = ID(0x35<<KeyShift | FlagsBinaryOp)
|
||||||
@@ -404,10 +404,10 @@ const (
|
|||||||
// TODO: sort these by name, when the list has stabilized.
|
// TODO: sort these by name, when the list has stabilized.
|
||||||
IDAnd = ID(0x48<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
|
IDAnd = ID(0x48<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
|
||||||
IDOr = ID(0x49<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
|
IDOr = ID(0x49<<KeyShift | FlagsBinaryOp | FlagsAssociativeOp)
|
||||||
IDNot = ID(0x4A<<KeyShift | FlagsUnaryOp)
|
IDNot = ID(0x4A<<KeyShift | FlagsOther)
|
||||||
IDAs = ID(0x4B<<KeyShift | FlagsBinaryOp)
|
IDAs = ID(0x4B<<KeyShift | FlagsBinaryOp)
|
||||||
IDRef = ID(0x4C<<KeyShift | FlagsUnaryOp)
|
IDRef = ID(0x4C<<KeyShift | FlagsOther)
|
||||||
IDDeref = ID(0x4D<<KeyShift | FlagsUnaryOp)
|
IDDeref = ID(0x4D<<KeyShift | FlagsOther)
|
||||||
|
|
||||||
// TODO: sort these by name, when the list has stabilized.
|
// TODO: sort these by name, when the list has stabilized.
|
||||||
IDFunc = ID(0x50<<KeyShift | FlagsOther)
|
IDFunc = ID(0x50<<KeyShift | FlagsOther)
|
||||||
@@ -512,11 +512,11 @@ const (
|
|||||||
minXKey = 0xD0
|
minXKey = 0xD0
|
||||||
maxXKey = 0xFF
|
maxXKey = 0xFF
|
||||||
|
|
||||||
IDXUnaryPlus = ID(0xD0<<KeyShift | FlagsUnaryOp)
|
IDXUnaryPlus = ID(0xD0<<KeyShift | FlagsOther)
|
||||||
IDXUnaryMinus = ID(0xD1<<KeyShift | FlagsUnaryOp)
|
IDXUnaryMinus = ID(0xD1<<KeyShift | FlagsOther)
|
||||||
IDXUnaryNot = ID(0xD2<<KeyShift | FlagsUnaryOp)
|
IDXUnaryNot = ID(0xD2<<KeyShift | FlagsOther)
|
||||||
IDXUnaryRef = ID(0xD3<<KeyShift | FlagsUnaryOp)
|
IDXUnaryRef = ID(0xD3<<KeyShift | FlagsOther)
|
||||||
IDXUnaryDeref = ID(0xD4<<KeyShift | FlagsUnaryOp)
|
IDXUnaryDeref = ID(0xD4<<KeyShift | FlagsOther)
|
||||||
|
|
||||||
IDXBinaryPlus = ID(0xD8<<KeyShift | FlagsBinaryOp)
|
IDXBinaryPlus = ID(0xD8<<KeyShift | FlagsBinaryOp)
|
||||||
IDXBinaryMinus = ID(0xD9<<KeyShift | FlagsBinaryOp)
|
IDXBinaryMinus = ID(0xD9<<KeyShift | FlagsBinaryOp)
|
||||||
@@ -859,6 +859,32 @@ var ambiguousForms = [256]ID{
|
|||||||
KeyXAssociativeOr: IDOr,
|
KeyXAssociativeOr: IDOr,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
addXForms(&unaryForms)
|
||||||
|
addXForms(&binaryForms)
|
||||||
|
addXForms(&associativeForms)
|
||||||
|
}
|
||||||
|
|
||||||
|
// addXForms modifies table so that, if table[x] == y, then table[y] = y.
|
||||||
|
//
|
||||||
|
// For example, for the unaryForms table, the explicit entries are like:
|
||||||
|
// KeyPlus: IDXUnaryPlus,
|
||||||
|
// and this function implicitly addes entries like:
|
||||||
|
// KeyXUnaryPlus: IDXUnaryPlus,
|
||||||
|
func addXForms(table *[256]ID) {
|
||||||
|
implicitEntries := [256]ID{}
|
||||||
|
for _, y := range table {
|
||||||
|
if y != 0 {
|
||||||
|
implicitEntries[y.Key()] = y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, y := range implicitEntries {
|
||||||
|
if y != 0 {
|
||||||
|
table[y.Key()] = y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var unaryForms = [256]ID{
|
var unaryForms = [256]ID{
|
||||||
KeyPlus: IDXUnaryPlus,
|
KeyPlus: IDXUnaryPlus,
|
||||||
KeyMinus: IDXUnaryMinus,
|
KeyMinus: IDXUnaryMinus,
|
||||||
|
|||||||
Reference in new issue
Block a user