Refactor isInSrcReadU8 and isLowBits.

This commit is contained in:
Nigel Tao committed 2017-06-09 15:44:43 +10:00
1 parent 711adf5273
commit c1dff0ca41
3 files changed
+22 -6

No files matched your search

+10 -2
View File
@@ -849,7 +849,7 @@ func (g *gen) writeCallSuspendibles(n *a.Expr) error {
g.perFunc.tempW++
// TODO: delete this hack that only matches "in.src.read_u8?()".
if isInSrcReadU8(g.tm, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
if isInSrcReadU8(g.tm, n) {
// TODO: suspend coroutine state.
//
// TODO: choose error_unexpected_eof versus status_short_read based on
@@ -947,7 +947,7 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
case t.KeyOpenParen:
// n is a function call.
// TODO: delete this hack that only matches "foo.low_bits(etc)".
if isLowBits(g.tm, n.LHS().Expr()) && !n.CallImpure() && len(n.Args()) == 1 {
if isLowBits(g.tm, n) {
g.printf("PUFFS_LOW_BITS(")
if err := g.writeExpr(n.LHS().Expr().LHS().Expr(), rp, parenthesesMandatory, depth); err != nil {
return err
@@ -985,6 +985,10 @@ func (g *gen) writeExprOther(n *a.Expr, rp replacementPolicy, depth uint32) erro
}
func isInSrcReadU8(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()
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyReadU8 {
return false
}
@@ -998,6 +1002,10 @@ func isInSrcReadU8(tm *t.Map, n *a.Expr) bool {
func isLowBits(tm *t.Map, n *a.Expr) bool {
// TODO: check that n.Args() is "(n: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().Key() == t.KeyLowBits
}
+2 -2
View File
@@ -590,11 +590,11 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
case t.KeyOpenParen:
// TODO: delete this hack that only matches "in.src.read_u8?()".
if isInSrcReadU8(q.tm, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
if isInSrcReadU8(q.tm, n) {
break
}
// TODO: delete this hack that only matches "foo.low_bits(etc)".
if isLowBits(q.tm, n.LHS().Expr()) && !n.CallImpure() && len(n.Args()) == 1 {
if isLowBits(q.tm, n) {
a := n.Args()[0].Arg().Value()
aMin, aMax, err := q.bcheckExpr(a, depth)
if err != nil {
+10 -2
View File
@@ -314,7 +314,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
case t.KeyOpenParen:
// n is a function call.
// TODO: delete this hack that only matches "in.src.read_u8?()".
if isInSrcReadU8(q.tm, n.LHS().Expr()) && n.CallSuspendible() && len(n.Args()) == 0 {
if isInSrcReadU8(q.tm, n) {
if err := q.tcheckExpr(n.LHS().Expr(), depth); err != nil {
return err
}
@@ -322,7 +322,7 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
return nil
}
// TODO: delete this hack that only matches "foo.low_bits(etc)".
if isLowBits(q.tm, n.LHS().Expr()) && !n.CallImpure() && len(n.Args()) == 1 {
if isLowBits(q.tm, n) {
foo := n.LHS().Expr().LHS().Expr()
if err := q.tcheckExpr(foo, depth); err != nil {
return err
@@ -353,6 +353,10 @@ func (q *checker) tcheckExprOther(n *a.Expr, depth uint32) error {
}
func isInSrcReadU8(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()
if n.ID0().Key() != t.KeyDot || n.ID1().Key() != t.KeyReadU8 {
return false
}
@@ -366,6 +370,10 @@ func isInSrcReadU8(tm *t.Map, n *a.Expr) bool {
func isLowBits(tm *t.Map, n *a.Expr) bool {
// TODO: check that n.Args() is "(n: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().Key() == t.KeyLowBits
}