Skip to content

Commit

Permalink
Unexport from matcher.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Oct 22, 2018
1 parent c8107f1 commit 79e23ca
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 57 deletions.
6 changes: 3 additions & 3 deletions expreduce/matcher/allocations.go
Expand Up @@ -62,7 +62,7 @@ func (ai *allocIter) next() bool {
return false
}

func NewAllocIter(l int, forms []parsedForm) allocIter {
func newAllocIter(l int, forms []parsedForm) allocIter {
ai := allocIter{}
ai.forms = forms
ai.alloc = make([]int, len(forms))
Expand Down Expand Up @@ -179,7 +179,7 @@ func (asi *assnIter) next() bool {
return false
}

func NewAssnIter(l int, forms []parsedForm, formMatches [][]bool, orderless bool) assnIter {
func newAssnIter(l int, forms []parsedForm, formMatches [][]bool, orderless bool) assnIter {
asi := assnIter{}
asi.forms = forms
asi.assnData = make([]int, l)
Expand All @@ -189,7 +189,7 @@ func NewAssnIter(l int, forms []parsedForm, formMatches [][]bool, orderless bool
asi.taken = make([]bool, l)
asi.formMatches = formMatches

asi.ai = NewAllocIter(len(asi.assnData), asi.forms)
asi.ai = newAllocIter(len(asi.assnData), asi.forms)
for i := range asi.assnData {
asi.assnData[i] = i
}
Expand Down
10 changes: 5 additions & 5 deletions expreduce/matcher/blank.go
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/corywalker/expreduce/pkg/expreduceapi"
)

func IsBlankTypeOnly(e expreduceapi.Ex) bool {
func isBlankTypeOnly(e expreduceapi.Ex) bool {
asPattern, patternOk := atoms.HeadAssertion(e, "System`Pattern")
if patternOk {
_, blankOk := atoms.HeadAssertion(asPattern.GetParts()[2], "System`Blank")
Expand All @@ -24,7 +24,7 @@ func IsBlankTypeOnly(e expreduceapi.Ex) bool {
return false
}

func IsBlankTypeCapturing(e expreduceapi.Ex, target expreduceapi.Ex, head expreduceapi.Ex, pm *PDManager, cl expreduceapi.LoggingInterface) (bool, *PDManager) {
func isBlankTypeCapturing(e expreduceapi.Ex, target expreduceapi.Ex, head expreduceapi.Ex, pm *PDManager, cl expreduceapi.LoggingInterface) (bool, *PDManager) {
// Similar to IsBlankType, but will capture target into es.patternDefined
// if there is a valid match.
asPattern, patternOk := atoms.HeadAssertion(e, "System`Pattern")
Expand Down Expand Up @@ -60,7 +60,7 @@ func IsBlankTypeCapturing(e expreduceapi.Ex, target expreduceapi.Ex, head expred
toMatch, ispd := pm.patternDefined[sAsSymbol.Name]
if !ispd {
toMatch = target
pm.LazyMakeMap()
pm.lazyMakeMap()
pm.patternDefined[sAsSymbol.Name] = target
}
if !atoms.IsSameQ(toMatch, target, cl) {
Expand Down Expand Up @@ -92,14 +92,14 @@ func IsBlankTypeCapturing(e expreduceapi.Ex, target expreduceapi.Ex, head expred
return false, pm
}

func BlankNullSequenceToBlank(bns expreduceapi.ExpressionInterface) expreduceapi.ExpressionInterface {
func blankNullSequenceToBlank(bns expreduceapi.ExpressionInterface) expreduceapi.ExpressionInterface {
if len(bns.GetParts()) < 2 {
return atoms.NewExpression([]expreduceapi.Ex{atoms.NewSymbol("System`Blank")})
}
return atoms.NewExpression([]expreduceapi.Ex{atoms.NewSymbol("System`Blank"), bns.GetParts()[1]})
}

func BlankSequenceToBlank(bs expreduceapi.ExpressionInterface) expreduceapi.ExpressionInterface {
func blankSequenceToBlank(bs expreduceapi.ExpressionInterface) expreduceapi.ExpressionInterface {
if len(bs.GetParts()) < 2 {
return atoms.NewExpression([]expreduceapi.Ex{atoms.NewSymbol("System`Blank")})
}
Expand Down
46 changes: 23 additions & 23 deletions expreduce/matcher/matchq.go
Expand Up @@ -15,10 +15,10 @@ var freezeStateDuringPreMatch = flag.Bool(
"thread safety.",
)

const MaxUint = ^uint(0)
const MaxInt = int(MaxUint >> 1)
const MaxUint64 = ^uint64(0)
const MaxInt64 = int64(MaxUint64 >> 1)
const maxUint = ^uint(0)
const maxInt = int(maxUint >> 1)
const maxUint64 = ^uint64(0)
const maxInt64 = int64(maxUint64 >> 1)

type matchIter interface {
// returns ismatch, pd, isdone
Expand Down Expand Up @@ -198,8 +198,8 @@ func NewMatchIter(a expreduceapi.Ex, b expreduceapi.Ex, pm *PDManager, es expred
headEx = complexSym
}

if IsBlankTypeOnly(b) {
ibtc, ibtcNewPDs := IsBlankTypeCapturing(b, a, headEx, pm, es.GetLogger())
if isBlankTypeOnly(b) {
ibtc, ibtcNewPDs := isBlankTypeCapturing(b, a, headEx, pm, es.GetLogger())
if ibtc {
return &dummyMatchIter{ibtcNewPDs}, true
}
Expand Down Expand Up @@ -290,7 +290,7 @@ func NewMatchIter(a expreduceapi.Ex, b expreduceapi.Ex, pm *PDManager, es expred

isOrderless := attrs.Orderless && !forceOrdered
isFlat := attrs.Flat && !forceOrdered
nomi, ok := NewSequenceMatchIter(aExpression.GetParts()[startI:], bExpression.GetParts()[startI:], isOrderless, isFlat, sequenceHead, pm, es)
nomi, ok := newSequenceMatchIter(aExpression.GetParts()[startI:], bExpression.GetParts()[startI:], isOrderless, isFlat, sequenceHead, pm, es)
if !ok {
return nil, false
}
Expand Down Expand Up @@ -337,7 +337,7 @@ type assignedMatchIter struct {
stack []assignedIterState
}

func NewAssignedMatchIter(assn [][]int, smi *sequenceMatchIter) assignedMatchIter {
func newAssignedMatchIter(assn [][]int, smi *sequenceMatchIter) assignedMatchIter {
ami := assignedMatchIter{}
ami.assn = assn
ami.components = smi.components
Expand All @@ -346,12 +346,12 @@ func NewAssignedMatchIter(assn [][]int, smi *sequenceMatchIter) assignedMatchIte
ami.sequenceHead = smi.sequenceHead
ami.es = smi.es
ami.stack = []assignedIterState{
{0, 0, CopyPD(ami.pm)},
{0, 0, copyPD(ami.pm)},
}
return ami
}

func (ami *assignedMatchIter) Next() bool {
func (ami *assignedMatchIter) next() bool {
for len(ami.stack) > 0 {
var p assignedIterState
l := len(ami.stack)
Expand All @@ -370,7 +370,7 @@ func (ami *assignedMatchIter) Next() bool {
for i, assignedComp := range ami.assn[p.formI] {
seq[i] = ami.components[assignedComp]
}
patOk := DefineSequence(lhs, seq, p.pm, ami.sequenceHead, ami.es)
patOk := defineSequence(lhs, seq, p.pm, ami.sequenceHead, ami.es)
if patOk {
ami.stack = append(ami.stack, assignedIterState{
p.formI + 1, 0, p.pm,
Expand All @@ -395,11 +395,11 @@ func (ami *assignedMatchIter) Next() bool {
}
for i := len(toAddReversed) - 1; i >= 0; i-- {
updatedPm := p.pm
if toAddReversed[i] != nil && toAddReversed[i].Len() > 0 {
if toAddReversed[i] != nil && toAddReversed[i].len() > 0 {
if len(toAddReversed) > 1 {
updatedPm = CopyPD(p.pm)
updatedPm = copyPD(p.pm)
}
updatedPm.Update(toAddReversed[i])
updatedPm.update(toAddReversed[i])
}
ami.stack = append(ami.stack, assignedIterState{
p.formI, p.assnI + 1, updatedPm,
Expand All @@ -420,16 +420,16 @@ type sequenceMatchIter struct {
ami assignedMatchIter
}

func NewSequenceMatchIter(components []expreduceapi.Ex, lhs_components []expreduceapi.Ex, isOrderless bool, isFlat bool, sequenceHead string, pm *PDManager, es expreduceapi.EvalStateInterface) (matchIter, bool) {
func newSequenceMatchIter(components []expreduceapi.Ex, lhs_components []expreduceapi.Ex, isOrderless bool, isFlat bool, sequenceHead string, pm *PDManager, es expreduceapi.EvalStateInterface) (matchIter, bool) {
headDefault := (atoms.NewSymbol(sequenceHead)).Default(es.GetDefinedMap())
fp_components := make([]parsedForm, len(lhs_components))
for i, comp := range lhs_components {
fp_components[i] = ParseForm(comp, isFlat, sequenceHead, headDefault, es.GetLogger())
fp_components[i] = parseForm(comp, isFlat, sequenceHead, headDefault, es.GetLogger())
}
return NewSequenceMatchIterPreparsed(components, fp_components, isOrderless, sequenceHead, pm, es)
return newSequenceMatchIterPreparsed(components, fp_components, isOrderless, sequenceHead, pm, es)
}

func NewSequenceMatchIterPreparsed(components []expreduceapi.Ex, lhs_components []parsedForm, isOrderless bool, sequenceHead string, pm *PDManager, es expreduceapi.EvalStateInterface) (matchIter, bool) {
func newSequenceMatchIterPreparsed(components []expreduceapi.Ex, lhs_components []parsedForm, isOrderless bool, sequenceHead string, pm *PDManager, es expreduceapi.EvalStateInterface) (matchIter, bool) {
nomi := &sequenceMatchIter{}
nomi.components = components
nomi.lhs_components = lhs_components
Expand Down Expand Up @@ -465,29 +465,29 @@ func NewSequenceMatchIterPreparsed(components []expreduceapi.Ex, lhs_components
es.SetFrozen(origFrozen)
}

nomi.ai = NewAssnIter(len(components), lhs_components, formMatches, isOrderless)
nomi.ai = newAssnIter(len(components), lhs_components, formMatches, isOrderless)

return nomi, true
}

func (this *sequenceMatchIter) Next() (bool, *PDManager, bool) {
for {
if this.iteratingAmi && this.ami.Next() {
if this.iteratingAmi && this.ami.next() {
return true, this.ami.pm, false
}
this.iteratingAmi = false
if !this.ai.next() {
break
}
this.ami = NewAssignedMatchIter(this.ai.assns, this)
this.ami = newAssignedMatchIter(this.ai.assns, this)
this.iteratingAmi = true
}
return false, this.pm, true
}

// HELPER FUNCTIONS

func GetMatchQ(mi matchIter, cont bool, pm *PDManager) (bool, *PDManager) {
func getMatchQ(mi matchIter, cont bool, pm *PDManager) (bool, *PDManager) {
for cont {
matchq, newPd, done := mi.Next()
cont = !done
Expand All @@ -503,5 +503,5 @@ func GetMatchQ(mi matchIter, cont bool, pm *PDManager) (bool, *PDManager) {
// TODO: do not export this
func IsMatchQ(a expreduceapi.Ex, b expreduceapi.Ex, pm *PDManager, es expreduceapi.EvalStateInterface) (bool, *PDManager) {
mi, cont := NewMatchIter(a, b, pm, es)
return GetMatchQ(mi, cont, pm)
return getMatchQ(mi, cont, pm)
}
22 changes: 11 additions & 11 deletions expreduce/matcher/parse_form.go
Expand Up @@ -18,7 +18,7 @@ type parsedForm struct {
patSym *atoms.Symbol
}

func ParseRepeated(e expreduceapi.ExpressionInterface) (expreduceapi.Ex, int, int, bool) {
func parseRepeated(e expreduceapi.ExpressionInterface) (expreduceapi.Ex, int, int, bool) {
min, max := -1, -1
if len(e.GetParts()) < 2 {
return nil, min, max, false
Expand All @@ -42,7 +42,7 @@ func ParseRepeated(e expreduceapi.ExpressionInterface) (expreduceapi.Ex, int, in
return e.GetParts()[1], min, max, true
}

func ParseForm(lhs_component expreduceapi.Ex, isFlat bool, sequenceHead string, headDefault expreduceapi.Ex, cl expreduceapi.LoggingInterface) (res parsedForm) {
func parseForm(lhs_component expreduceapi.Ex, isFlat bool, sequenceHead string, headDefault expreduceapi.Ex, cl expreduceapi.LoggingInterface) (res parsedForm) {
// Calculate the min and max elements this component can match.
toParse := lhs_component
optional, isOptional := atoms.HeadAssertion(toParse, "System`Optional")
Expand Down Expand Up @@ -92,18 +92,18 @@ func ParseForm(lhs_component expreduceapi.Ex, isFlat bool, sequenceHead string,
endI = 1
// I think the !isPatTest part might be a hack.
if isImpliedBs && !isPatTest {
endI = MaxInt
endI = maxInt
}
//form = optional.Parts[1]
defaultExpr = defaultToUse
}
} else if isBns {
form = BlankNullSequenceToBlank(bns)
form = blankNullSequenceToBlank(bns)
startI = 0
endI = MaxInt
endI = maxInt
} else if isImpliedBs {
form = blank
endI = MaxInt
endI = maxInt
if len(blank.GetParts()) >= 2 {
sym, isSym := blank.GetParts()[1].(*atoms.Symbol)
if isSym {
Expand All @@ -119,7 +119,7 @@ func ParseForm(lhs_component expreduceapi.Ex, isFlat bool, sequenceHead string,
} else if isBlank {
form = blank
} else if isRepeated {
repPat, repMin, repMax, repOk := ParseRepeated(repeated)
repPat, repMin, repMax, repOk := parseRepeated(repeated)
if repOk {
if repMin != -1 {
startI = repMin
Expand All @@ -128,19 +128,19 @@ func ParseForm(lhs_component expreduceapi.Ex, isFlat bool, sequenceHead string,
endI = repMax
} else {
// an undefined end can match to the end of the sequence.
endI = MaxInt
endI = maxInt
}
form = repPat
}
} else if isRepeatedNull {
if len(repeatedNull.GetParts()) == 2 {
startI = 0
endI = MaxInt
endI = maxInt
form = repeatedNull.GetParts()[1]
}
} else if isBs {
form = BlankSequenceToBlank(bs)
endI = MaxInt
form = blankSequenceToBlank(bs)
endI = maxInt
}

if isPatTest {
Expand Down
24 changes: 12 additions & 12 deletions expreduce/matcher/pdmanager.go
Expand Up @@ -17,47 +17,47 @@ func EmptyPD() *PDManager {
return &PDManager{nil}
}

func CopyPD(orig *PDManager) (dest *PDManager) {
func copyPD(orig *PDManager) (dest *PDManager) {
dest = EmptyPD()
// We do not care that this iterates in a random order.
if (*orig).Len() > 0 {
dest.LazyMakeMap()
if (*orig).len() > 0 {
dest.lazyMakeMap()
for k, v := range (*orig).patternDefined {
(*dest).patternDefined[k] = v
}
}
return
}

func (this *PDManager) LazyMakeMap() {
func (this *PDManager) lazyMakeMap() {
if this.patternDefined == nil {
this.patternDefined = make(map[string]expreduceapi.Ex)
}
}

func (this *PDManager) Define(name string, val expreduceapi.Ex) {
this.LazyMakeMap()
this.lazyMakeMap()
this.patternDefined[name] = val
}

func (this *PDManager) Update(toAdd *PDManager) {
if (*toAdd).Len() > 0 {
this.LazyMakeMap()
func (this *PDManager) update(toAdd *PDManager) {
if (*toAdd).len() > 0 {
this.lazyMakeMap()
}
// We do not care that this iterates in a random order.
for k, v := range (*toAdd).patternDefined {
(*this).patternDefined[k] = v
}
}

func (this *PDManager) Len() int {
func (this *PDManager) len() int {
if this.patternDefined == nil {
return 0
}
return len(this.patternDefined)
}

func (this *PDManager) String(es expreduceapi.EvalStateInterface) string {
func (this *PDManager) string(es expreduceapi.EvalStateInterface) string {
var buffer bytes.Buffer
buffer.WriteString("{")
// We sort the keys here such that converting identical PDManagers always
Expand Down Expand Up @@ -101,7 +101,7 @@ func (this *PDManager) Expression() expreduceapi.Ex {
return res
}

func DefineSequence(lhs parsedForm, sequence []expreduceapi.Ex, pm *PDManager, sequenceHead string, es expreduceapi.EvalStateInterface) bool {
func defineSequence(lhs parsedForm, sequence []expreduceapi.Ex, pm *PDManager, sequenceHead string, es expreduceapi.EvalStateInterface) bool {
var attemptDefine expreduceapi.Ex = nil
if lhs.hasPat {
sequenceHeadSym := atoms.NewSymbol(sequenceHead)
Expand All @@ -124,7 +124,7 @@ func DefineSequence(lhs parsedForm, sequence []expreduceapi.Ex, pm *PDManager, s
return false
}
}
pm.LazyMakeMap()
pm.lazyMakeMap()
pm.patternDefined[lhs.patSym.Name] = attemptDefine
}
return true
Expand Down
6 changes: 3 additions & 3 deletions expreduce/matcher/pdreplace.go
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/corywalker/expreduce/pkg/expreduceapi"
)

func ReplacePDInternal(e expreduceapi.Ex, pm *PDManager) (expreduceapi.Ex, bool) {
func replacePDInternal(e expreduceapi.Ex, pm *PDManager) (expreduceapi.Ex, bool) {
asSym, isSym := e.(*atoms.Symbol)
if isSym {
for k, def := range pm.patternDefined {
Expand All @@ -19,7 +19,7 @@ func ReplacePDInternal(e expreduceapi.Ex, pm *PDManager) (expreduceapi.Ex, bool)
asExpr, isExpr := e.(expreduceapi.ExpressionInterface)
if isExpr {
for i := range asExpr.GetParts() {
possiblyNewExpr, dirty := ReplacePDInternal(asExpr.GetParts()[i], pm)
possiblyNewExpr, dirty := replacePDInternal(asExpr.GetParts()[i], pm)
if dirty {
thisDirty = true
// Mark the expression as dirty and needing eval.
Expand Down Expand Up @@ -48,6 +48,6 @@ func ReplacePD(this expreduceapi.Ex, es expreduceapi.EvalStateInterface, pm *PDM

// Expressions are immutable. Any time we change an expression, we must
// first copy it.
res, _ := ReplacePDInternal(this.Copy(), pm)
res, _ := replacePDInternal(this.Copy(), pm)
return res
}

0 comments on commit 79e23ca

Please sign in to comment.