mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Fold Expr statements into Assign statements
This commit is contained in:
1 parent
796c8ccc30
commit
c9e6ee6fb3
6 files changed
+34
-38
No files matched your search
@@ -240,6 +240,10 @@ func (h *resumabilityHelper) doAssign(r resumabilities, n *a.Assign, depth uint3
|
||||
return err
|
||||
}
|
||||
|
||||
if n.LHS() == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// If the LHS is not a local variable (e.g. "this.foo[bar] = etc", or if
|
||||
// the LHS is implicitly also on the RHS (e.g. for a += or *= operator),
|
||||
// walk the LHS Expr.
|
||||
|
||||
@@ -39,15 +39,7 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
mightIntroduceTemporaries := false
|
||||
switch n.Kind() {
|
||||
case a.KAssign:
|
||||
n := n.AsAssign()
|
||||
mightIntroduceTemporaries = n.RHS().Effect().Coroutine()
|
||||
case a.KExpr:
|
||||
// TODO.
|
||||
}
|
||||
if mightIntroduceTemporaries {
|
||||
if (n.Kind() == a.KAssign) && (n.AsAssign().LHS() != nil) && n.AsAssign().RHS().Effect().Coroutine() {
|
||||
// Put n's code into its own block, to restrict the scope of the
|
||||
// tPrefix temporary variables. This helps avoid "jump bypasses
|
||||
// variable initialization" compiler warnings with the coroutine
|
||||
@@ -71,8 +63,6 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
case a.KAssign:
|
||||
n := n.AsAssign()
|
||||
return g.writeStatementAssign(b, n.Operator(), n.LHS(), n.RHS(), depth)
|
||||
case a.KExpr:
|
||||
return g.writeStatementAssign(b, 0, nil, n.AsExpr(), depth)
|
||||
case a.KIOBind:
|
||||
return g.writeStatementIOBind(b, n.AsIOBind(), depth)
|
||||
case a.KIf:
|
||||
@@ -92,8 +82,6 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
|
||||
}
|
||||
|
||||
func (g *gen) writeStatementAssign(b *buffer, op t.ID, lhs *a.Expr, rhs *a.Expr, depth uint32) error {
|
||||
// TODO: clean this method body up.
|
||||
|
||||
if depth > a.MaxExprDepth {
|
||||
return fmt.Errorf("expression recursion depth too large")
|
||||
}
|
||||
|
||||
+2
-3
@@ -414,9 +414,9 @@ func NewArg(name t.ID, value *Expr) *Arg {
|
||||
}
|
||||
}
|
||||
|
||||
// Assign is "LHS = RHS" or "LHS op= RHS":
|
||||
// Assign is "LHS = RHS" or "LHS op= RHS" or "RHS":
|
||||
// - ID0: operator
|
||||
// - LHS: <Expr>
|
||||
// - LHS: <nil|Expr>
|
||||
// - RHS: <Expr>
|
||||
type Assign Node
|
||||
|
||||
@@ -791,7 +791,6 @@ const MaxBodyDepth = 255
|
||||
// Statement means one of:
|
||||
// - Assert
|
||||
// - Assign
|
||||
// - Expr
|
||||
// - IOBind
|
||||
// - If
|
||||
// - Iterate
|
||||
|
||||
+19
-13
@@ -230,12 +230,6 @@ func (q *checker) bcheckStatement(n *a.Node) error {
|
||||
return err
|
||||
}
|
||||
|
||||
case a.KExpr:
|
||||
n := n.AsExpr()
|
||||
if _, err := q.bcheckExpr(n, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
case a.KIOBind:
|
||||
n := n.AsIOBind()
|
||||
if _, err := q.bcheckExpr(n.IO(), 0); err != nil {
|
||||
@@ -372,14 +366,23 @@ func (q *checker) bcheckAssert(n *a.Assert) error {
|
||||
}
|
||||
|
||||
func (q *checker) bcheckAssignment(lhs *a.Expr, op t.ID, rhs *a.Expr) error {
|
||||
if _, err := q.bcheckExpr(lhs, 0); err != nil {
|
||||
return err
|
||||
lTyp := (*a.TypeExpr)(nil)
|
||||
if lhs != nil {
|
||||
if _, err := q.bcheckExpr(lhs, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
lTyp = lhs.MType()
|
||||
}
|
||||
nb, err := q.bcheckAssignment1(lhs, lhs.MType(), op, rhs)
|
||||
|
||||
nb, err := q.bcheckAssignment1(lhs, lTyp, op, rhs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if lhs == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if op == t.IDEq {
|
||||
// Drop any facts involving lhs.
|
||||
if err := q.facts.update(func(x *a.Expr) (*a.Expr, error) {
|
||||
@@ -457,9 +460,12 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs
|
||||
return bounds{}, fmt.Errorf("check: internal error: missing LHS for op key 0x%02X", op)
|
||||
}
|
||||
|
||||
lb, err := q.bcheckTypeExpr(lTyp)
|
||||
if err != nil {
|
||||
return bounds{}, err
|
||||
lb, err := bounds{}, (error)(nil)
|
||||
if lTyp != nil {
|
||||
lb, err = q.bcheckTypeExpr(lTyp)
|
||||
if err != nil {
|
||||
return bounds{}, err
|
||||
}
|
||||
}
|
||||
|
||||
rb := bounds{}
|
||||
@@ -472,7 +478,7 @@ func (q *checker) bcheckAssignment1(lhs *a.Expr, lTyp *a.TypeExpr, op t.ID, rhs
|
||||
return bounds{}, err
|
||||
}
|
||||
|
||||
if (rb[0].Cmp(lb[0]) < 0) || (rb[1].Cmp(lb[1]) > 0) {
|
||||
if (lTyp != nil) && ((rb[0].Cmp(lb[0]) < 0) || (rb[1].Cmp(lb[1]) > 0)) {
|
||||
if op == t.IDEq {
|
||||
return bounds{}, fmt.Errorf("check: expression %q bounds %v is not within bounds %v",
|
||||
rhs.Str(q.tm), rb, lb)
|
||||
|
||||
+6
-6
@@ -57,9 +57,6 @@ func (q *checker) tcheckStatement(n *a.Node) error {
|
||||
return err
|
||||
}
|
||||
|
||||
case a.KExpr:
|
||||
return q.tcheckExpr(n.AsExpr(), 0)
|
||||
|
||||
case a.KIf:
|
||||
for n := n.AsIf(); n != nil; n = n.ElseIf() {
|
||||
cond := n.Condition()
|
||||
@@ -256,12 +253,15 @@ func (q *checker) tcheckEq(lID t.ID, lhs *a.Expr, lTyp *a.TypeExpr, rhs *a.Expr,
|
||||
}
|
||||
|
||||
func (q *checker) tcheckAssign(n *a.Assign) error {
|
||||
lhs := n.LHS()
|
||||
rhs := n.RHS()
|
||||
if err := q.tcheckExpr(lhs, 0); err != nil {
|
||||
if err := q.tcheckExpr(rhs, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := q.tcheckExpr(rhs, 0); err != nil {
|
||||
lhs := n.LHS()
|
||||
if lhs == nil {
|
||||
return nil
|
||||
}
|
||||
if err := q.tcheckExpr(lhs, 0); err != nil {
|
||||
return err
|
||||
}
|
||||
lTyp := lhs.MType()
|
||||
|
||||
+2
-3
@@ -701,6 +701,8 @@ func (p *parser) parseAssignNode() (*a.Node, error) {
|
||||
rhs.Str(p.tm), p.filename, p.line())
|
||||
}
|
||||
}
|
||||
} else {
|
||||
op = t.IDEq
|
||||
}
|
||||
|
||||
if p.funcEffect.WeakerThan(rhs.Effect()) {
|
||||
@@ -708,9 +710,6 @@ func (p *parser) parseAssignNode() (*a.Node, error) {
|
||||
rhs.Str(p.tm), rhs.Effect(), p.funcEffect, p.filename, p.line())
|
||||
}
|
||||
|
||||
if lhs == nil {
|
||||
return rhs.AsNode(), nil
|
||||
}
|
||||
return a.NewAssign(op, lhs, rhs).AsNode(), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user