From ebef5d6999b9efc932003c84407d1e95f1f136b8 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 13 May 2017 10:42:51 +1000 Subject: [PATCH] Refactor the ast Eq code. --- lang/ast/eq.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++ lang/ast/string.go | 81 ----------------------------------------- 2 files changed, 89 insertions(+), 81 deletions(-) create mode 100644 lang/ast/eq.go diff --git a/lang/ast/eq.go b/lang/ast/eq.go new file mode 100644 index 00000000..4344f0f9 --- /dev/null +++ b/lang/ast/eq.go @@ -0,0 +1,89 @@ +// Use of this source code is governed by a BSD-style license that can be found +// in the LICENSE file. + +package ast + +import ( + t "github.com/google/puffs/lang/token" +) + +// Eq returns whether n and o are equal. +// +// It may return false negatives. In general, it will not report that "x + y" +// equals "y + x". However, if both are constant expressions (i.e. each Expr +// node, including the sum nodes, has a ConstValue), both sums will have the +// same value and will compare equal. +func (n *Expr) Eq(o *Expr) bool { + if n == o { + return true + } + if n == nil || o == nil { + return false + } + if n.constValue != nil && o.constValue != nil { + return n.constValue.Cmp(o.constValue) == 0 + } + + if n.flags != o.flags || n.id0 != o.id0 || n.id1 != o.id1 { + return false + } + if !n.lhs.Expr().Eq(o.lhs.Expr()) { + return false + } + if !n.mhs.Expr().Eq(o.mhs.Expr()) { + return false + } + + if n.id0 == t.IDXBinaryAs { + if !n.rhs.TypeExpr().Eq(o.rhs.TypeExpr()) { + return false + } + } else if !n.rhs.Expr().Eq(o.rhs.Expr()) { + return false + } + + if len(n.list0) != len(o.list0) { + return false + } + for i, x := range n.list0 { + if !x.Expr().Eq(o.list0[i].Expr()) { + return false + } + } + return true +} + +// Eq returns whether n and o are equal. +func (n *TypeExpr) Eq(o *TypeExpr) bool { + return n.eq(o, false) +} + +// EqIgnoringRefinements returns whether n and o are equal, ignoring the +// "[i:j]" in "u32[i:j]". +func (n *TypeExpr) EqIgnoringRefinements(o *TypeExpr) bool { + return n.eq(o, true) +} + +func (n *TypeExpr) eq(o *TypeExpr, ignoreRefinements bool) bool { + for { + if n == o { + return true + } + if n == nil || o == nil { + return false + } + if n.id0 != o.id0 || n.id1 != o.id1 { + return false + } + if n.id0 == t.IDOpenBracket || !ignoreRefinements { + if !n.lhs.Expr().Eq(o.lhs.Expr()) || !n.mhs.Expr().Eq(o.mhs.Expr()) { + return false + } + } + if n.rhs == nil && o.rhs == nil { + return true + } + n = n.rhs.TypeExpr() + o = o.rhs.TypeExpr() + } +} diff --git a/lang/ast/string.go b/lang/ast/string.go index 0330a77b..dbe57bc3 100644 --- a/lang/ast/string.go +++ b/lang/ast/string.go @@ -133,52 +133,6 @@ var opStrings = [256]string{ t.IDXAssociativeOr >> t.KeyShift: " or ", } -// Eq returns whether n and o are equal. -// -// It may return false negatives. In general, it will not report that "x + y" -// equals "y + x". However, if both are constant expressions (i.e. each Expr -// node, including the sum nodes, has a ConstValue), both sums will have the -// same value and will compare equal. -func (n *Expr) Eq(o *Expr) bool { - if n == o { - return true - } - if n == nil || o == nil { - return false - } - if n.constValue != nil && o.constValue != nil { - return n.constValue.Cmp(o.constValue) == 0 - } - - if n.flags != o.flags || n.id0 != o.id0 || n.id1 != o.id1 { - return false - } - if !n.lhs.Expr().Eq(o.lhs.Expr()) { - return false - } - if !n.mhs.Expr().Eq(o.mhs.Expr()) { - return false - } - - if n.id0 == t.IDXBinaryAs { - if !n.rhs.TypeExpr().Eq(o.rhs.TypeExpr()) { - return false - } - } else if !n.rhs.Expr().Eq(o.rhs.Expr()) { - return false - } - - if len(n.list0) != len(o.list0) { - return false - } - for i, x := range n.list0 { - if !x.Expr().Eq(o.list0[i].Expr()) { - return false - } - } - return true -} - // String returns a string form of n. func (n *TypeExpr) String(m *t.IDMap) string { if n != nil { @@ -200,38 +154,3 @@ func (n *TypeExpr) String(m *t.IDMap) string { } return "!invalid_type!" } - -// Eq returns whether n and o are equal. -func (n *TypeExpr) Eq(o *TypeExpr) bool { - return n.eq(o, false) -} - -// EqIgnoringRefinements returns whether n and o are equal, ignoring the -// "[i:j]" in "u32[i:j]". -func (n *TypeExpr) EqIgnoringRefinements(o *TypeExpr) bool { - return n.eq(o, true) -} - -func (n *TypeExpr) eq(o *TypeExpr, ignoreRefinements bool) bool { - for { - if n == o { - return true - } - if n == nil || o == nil { - return false - } - if n.id0 != o.id0 || n.id1 != o.id1 { - return false - } - if n.id0 == t.IDOpenBracket || !ignoreRefinements { - if !n.lhs.Expr().Eq(o.lhs.Expr()) || !n.mhs.Expr().Eq(o.mhs.Expr()) { - return false - } - } - if n.rhs == nil && o.rhs == nil { - return true - } - n = n.rhs.TypeExpr() - o = o.rhs.TypeExpr() - } -}