diff --git a/cmd/puffs-gen-c/main.go b/cmd/puffs-gen-c/main.go index 07cceab1..7da987f5 100644 --- a/cmd/puffs-gen-c/main.go +++ b/cmd/puffs-gen-c/main.go @@ -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 } diff --git a/lang/check/bounds.go b/lang/check/bounds.go index 39c15726..1c2d6036 100644 --- a/lang/check/bounds.go +++ b/lang/check/bounds.go @@ -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 { diff --git a/lang/check/type.go b/lang/check/type.go index 8a56bcde..8c814177 100644 --- a/lang/check/type.go +++ b/lang/check/type.go @@ -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 }