mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Refactor internal funcs like isDecode, isLength.
This commit is contained in:
1 parent
b14bafa717
commit
569e800bac
4 files changed
+35
-111
No files matched your search
@@ -85,7 +85,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
case t.KeyOpenParen:
|
||||
// n is a function call.
|
||||
// TODO: delete this hack that only matches "foo.bar_bits(etc)".
|
||||
if isLowHighBits(g.tm, n, t.KeyLowBits) {
|
||||
if isThatMethod(g.tm, n, t.KeyLowBits, 1) {
|
||||
// "x.low_bits(n:etc)" in C is "((x) & ((1 << (n)) - 1))".
|
||||
x := n.LHS().Expr().LHS().Expr()
|
||||
b.writes("((")
|
||||
@@ -99,7 +99,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
b.writes(")) - 1))")
|
||||
return nil
|
||||
}
|
||||
if isLowHighBits(g.tm, n, t.KeyHighBits) {
|
||||
if isThatMethod(g.tm, n, t.KeyHighBits, 1) {
|
||||
// "x.high_bits(n:etc)" in C is "((x) >> (8*sizeof(x) - (n)))".
|
||||
x := n.LHS().Expr().LHS().Expr()
|
||||
b.writes("((")
|
||||
@@ -119,7 +119,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
b.writes(")))")
|
||||
return nil
|
||||
}
|
||||
if isIsErrorOKSuspension(g.tm, n, t.KeyIsSuspension) {
|
||||
if isThatMethod(g.tm, n, t.KeyIsSuspension, 0) {
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb('(')
|
||||
}
|
||||
@@ -134,7 +134,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if isIsPrefixSuffix(g.tm, n, t.KeySuffix) {
|
||||
if isThatMethod(g.tm, n, t.KeySuffix, 1) {
|
||||
// TODO: don't assume that the slice is a slice of u8.
|
||||
b.writes("puffs_base_slice_u8_suffix(")
|
||||
x := n.LHS().Expr().LHS().Expr()
|
||||
@@ -158,7 +158,7 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if isLength(g.tm, n) {
|
||||
if isThatMethod(g.tm, n, t.KeyLength, 0) {
|
||||
if pp == parenthesesMandatory {
|
||||
b.writeb('(')
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.set_literal_width(etc)".
|
||||
if isSetLiteralWidth(g.tm, n) {
|
||||
if isThatMethod(g.tm, n, g.tm.ByName("set_literal_width").Key(), 1) {
|
||||
b.printf("%slzw_decoder_set_literal_width(&self->private_impl.f_lzw, ", g.pkgPrefix)
|
||||
a := n.Args()[0].Arg().Value()
|
||||
if err := g.writeExpr(b, a, replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||
@@ -584,7 +584,7 @@ func (g *gen) writeCallSuspendibles(b *buffer, n *a.Expr, depth uint32) error {
|
||||
}
|
||||
b.writes("if (status) { goto suspend; }\n")
|
||||
|
||||
} else if isDecode(g.tm, n) {
|
||||
} else if isThatMethod(g.tm, n, g.tm.ByName("decode").Key(), 2) {
|
||||
switch g.pkgName {
|
||||
case "flate":
|
||||
b.printf("status = %sdecoder_decode(&self->private_impl.f_dec, %sdst, %ssrc);\n",
|
||||
@@ -732,7 +732,7 @@ func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool {
|
||||
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
|
||||
return false
|
||||
}
|
||||
if !n.CallSuspendible() || len(n.Args()) != nArgs {
|
||||
if len(n.Args()) != nArgs {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
@@ -743,53 +743,14 @@ func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool {
|
||||
return n.ID0() == 0 && n.ID1().Key() == t.KeyThis
|
||||
}
|
||||
|
||||
func isLowHighBits(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
// TODO: check that n.Args() is "(n:bar)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
// isThatMethod is like isThisMethod but for foo.bar(etc), not this.bar(etc).
|
||||
func isThatMethod(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
|
||||
return false
|
||||
}
|
||||
if len(n.Args()) != nArgs {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isIsErrorOKSuspension(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 0 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isIsPrefixSuffix(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool {
|
||||
// TODO: check that n.Args() is "(lw:bar)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("set_literal_width")
|
||||
}
|
||||
|
||||
func isDecode(tm *t.Map, n *a.Expr) bool {
|
||||
// TODO: check that n.Args() is "(dst:bar, src:baz)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 2 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("decode")
|
||||
}
|
||||
|
||||
func isLength(tm *t.Map, n *a.Expr) bool {
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallSuspendible() || len(n.Args()) != 0 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLength
|
||||
}
|
||||
@@ -671,7 +671,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
break
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.bar_bits(etc)".
|
||||
if isLowHighBits(q.tm, n, t.KeyLowBits) || isLowHighBits(q.tm, n, t.KeyHighBits) {
|
||||
if isThatMethod(q.tm, n, t.KeyLowBits, 1) || isThatMethod(q.tm, n, t.KeyHighBits, 1) {
|
||||
a := n.Args()[0].Arg().Value()
|
||||
aMin, aMax, err := q.bcheckExpr(a, depth)
|
||||
if err != nil {
|
||||
@@ -689,11 +689,11 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
return zero, bitMask(int(aMax.Int64())), nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.is_suspension(etc)".
|
||||
if isIsErrorOKSuspension(q.tm, n, t.KeyIsSuspension) || isIsPrefixSuffix(q.tm, n, t.KeySuffix) {
|
||||
if isThatMethod(q.tm, n, t.KeyIsSuspension, 0) || isThatMethod(q.tm, n, t.KeySuffix, 1) {
|
||||
return nil, nil, nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.set_literal_width(etc)".
|
||||
if isSetLiteralWidth(q.tm, n) {
|
||||
if isThatMethod(q.tm, n, q.tm.ByName("set_literal_width").Key(), 1) {
|
||||
a := n.Args()[0].Arg().Value()
|
||||
aMin, aMax, err := q.bcheckExpr(a, depth)
|
||||
if err != nil {
|
||||
@@ -705,7 +705,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
return nil, nil, nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.decode(etc)".
|
||||
if isDecode(q.tm, n) {
|
||||
if isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) {
|
||||
for _, aNode := range n.Args() {
|
||||
a := aNode.Arg().Value()
|
||||
_, _, err := q.bcheckExpr(a, depth)
|
||||
@@ -715,7 +715,8 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
|
||||
}
|
||||
return nil, nil, nil
|
||||
}
|
||||
if isLength(q.tm, n) {
|
||||
// TODO: delete this hack that only matches "foo.length(etc)".
|
||||
if isThatMethod(q.tm, n, t.KeyLength, 0) {
|
||||
break
|
||||
}
|
||||
return nil, nil, fmt.Errorf("check: unrecognized token.Key (0x%X) for bcheckExprOther", n.ID0().Key())
|
||||
|
||||
+15
-53
@@ -379,7 +379,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.bar_bits(etc)".
|
||||
if isLowHighBits(q.tm, n, t.KeyLowBits) || isLowHighBits(q.tm, n, t.KeyHighBits) {
|
||||
if isThatMethod(q.tm, n, t.KeyLowBits, 1) || isThatMethod(q.tm, n, t.KeyHighBits, 1) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
@@ -395,7 +395,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.is_suspension(etc)".
|
||||
if isIsErrorOKSuspension(q.tm, n, t.KeyIsSuspension) {
|
||||
if isThatMethod(q.tm, n, t.KeyIsSuspension, 0) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
@@ -411,7 +411,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.suffix(etc)".
|
||||
if isIsPrefixSuffix(q.tm, n, t.KeySuffix) {
|
||||
if isThatMethod(q.tm, n, t.KeySuffix, 1) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
@@ -428,7 +428,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.set_literal_width(etc)".
|
||||
if isSetLiteralWidth(q.tm, n) {
|
||||
if isThatMethod(q.tm, n, q.tm.ByName("set_literal_width").Key(), 1) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
@@ -444,7 +444,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.decode(etc)".
|
||||
if isDecode(q.tm, n) {
|
||||
if isThatMethod(q.tm, n, q.tm.ByName("decode").Key(), 2) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
@@ -460,7 +460,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
return nil
|
||||
}
|
||||
// TODO: delete this hack that only matches "foo.length(etc)".
|
||||
if isLength(q.tm, n) {
|
||||
if isThatMethod(q.tm, n, t.KeyLength, 0) {
|
||||
foo := n.LHS().Expr().LHS().Expr()
|
||||
if err := q.tcheckExpr(foo, depth); err != nil {
|
||||
return err
|
||||
@@ -544,7 +544,8 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
|
||||
n.SetMType(rTyp)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("check: unrecognized token.Key (0x%X) for tcheckExprOther", n.ID0().Key())
|
||||
return fmt.Errorf("check: unrecognized token.Key (0x%X) in expression %q for tcheckExprOther",
|
||||
n.ID0().Key(), n.String(q.tm))
|
||||
}
|
||||
|
||||
func isInSrc(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
@@ -586,7 +587,7 @@ func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool {
|
||||
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
|
||||
return false
|
||||
}
|
||||
if !n.CallSuspendible() || len(n.Args()) != nArgs {
|
||||
if len(n.Args()) != nArgs {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
@@ -597,57 +598,18 @@ func isThisMethod(tm *t.Map, n *a.Expr, methodName string, nArgs int) bool {
|
||||
return n.ID0() == 0 && n.ID1().Key() == t.KeyThis
|
||||
}
|
||||
|
||||
func isLowHighBits(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
// TODO: check that n.Args() is "(n:bar)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
// isThatMethod is like isThisMethod but for foo.bar(etc), not this.bar(etc).
|
||||
func isThatMethod(tm *t.Map, n *a.Expr, methodName t.Key, nArgs int) bool {
|
||||
if k := n.ID0().Key(); k != t.KeyOpenParen && k != t.KeyTry {
|
||||
return false
|
||||
}
|
||||
if len(n.Args()) != nArgs {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isIsErrorOKSuspension(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 0 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isIsPrefixSuffix(tm *t.Map, n *a.Expr, methodName t.Key) bool {
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == methodName
|
||||
}
|
||||
|
||||
func isSetLiteralWidth(tm *t.Map, n *a.Expr) bool {
|
||||
// TODO: check that n.Args() is "(lw:bar)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallImpure() || len(n.Args()) != 1 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("set_literal_width")
|
||||
}
|
||||
|
||||
func isDecode(tm *t.Map, n *a.Expr) bool {
|
||||
// TODO: check that n.Args() is "(dst:bar, src:baz)".
|
||||
if n.ID0().Key() != t.KeyOpenParen || !n.CallSuspendible() || len(n.Args()) != 2 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1() == tm.ByName("decode")
|
||||
}
|
||||
|
||||
func isLength(tm *t.Map, n *a.Expr) bool {
|
||||
if n.ID0().Key() != t.KeyOpenParen || n.CallSuspendible() || len(n.Args()) != 0 {
|
||||
return false
|
||||
}
|
||||
n = n.LHS().Expr()
|
||||
return n.ID0().Key() == t.KeyDot && n.ID1().Key() == t.KeyLength
|
||||
}
|
||||
|
||||
func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
|
||||
lhs := n.LHS().Expr()
|
||||
if err := q.tcheckExpr(lhs, depth); err != nil {
|
||||
|
||||
Reference in new issue
Block a user