mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 00:47:28 +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
|
||||
}
|
||||
Reference in new issue
Block a user