From 10486e4b6cdc1140a159c606a1db90daac7225d1 Mon Sep 17 00:00:00 2001 From: cmwslw Date: Fri, 19 Oct 2018 00:55:54 -0700 Subject: [PATCH] Refactor checkpoint. --- expreduce/builtin_flowcontrol.go | 4 ++-- expreduce/builtin_functional.go | 2 +- expreduce/builtin_replacement.go | 2 +- expreduce/builtin_system.go | 10 +++++----- expreduce/cas_test.go | 14 +++++++------- expreduce/definition.go | 8 ++++---- expreduce/definition_map.go | 4 ++-- expreduce/evalstate.go | 30 +++++++++++++++++++----------- expreduce/ex_complex.go | 2 +- expreduce/ex_expression.go | 26 ++++++++++++-------------- expreduce/ex_symbol.go | 4 ++-- expreduce/matchq.go | 4 ++-- expreduce/parse_form.go | 4 ++-- expreduce/string.go | 10 +++++----- 14 files changed, 65 insertions(+), 59 deletions(-) diff --git a/expreduce/builtin_flowcontrol.go b/expreduce/builtin_flowcontrol.go index e4e395a..e967f79 100644 --- a/expreduce/builtin_flowcontrol.go +++ b/expreduce/builtin_flowcontrol.go @@ -98,7 +98,7 @@ func GetFlowControlDefinitions() (defs []Definition) { for i := 1; i < len(this.GetParts()); i++ { toReturn = this.GetParts()[i].Eval(es) if es.HasThrown() { - return es.thrown + return es.Thrown() } if _, isReturn := HeadAssertion(toReturn, "System`Return"); isReturn { return toReturn @@ -164,7 +164,7 @@ func GetFlowControlDefinitions() (defs []Definition) { mis.defineCurrent(es) res := this.GetParts()[1].DeepCopy().Eval(es) if es.HasThrown() { - return es.thrown + return es.Thrown() } if asReturn, isReturn := HeadAssertion(res, "System`Return"); isReturn { if len(asReturn.GetParts()) < 2 { diff --git a/expreduce/builtin_functional.go b/expreduce/builtin_functional.go index 84aeb87..4127ce0 100644 --- a/expreduce/builtin_functional.go +++ b/expreduce/builtin_functional.go @@ -613,7 +613,7 @@ func getFunctionalDefinitions() (defs []Definition) { toReturn.AppendEx(expr) } } else { - toReturn.GetParts() = toReturn.GetParts()[:int64(len(toReturn.GetParts()))+n] + toReturn.SetParts(toReturn.GetParts()[:int64(len(toReturn.GetParts()))+n]) } return toReturn diff --git a/expreduce/builtin_replacement.go b/expreduce/builtin_replacement.go index dc6d9db..0d61b3e 100644 --- a/expreduce/builtin_replacement.go +++ b/expreduce/builtin_replacement.go @@ -71,7 +71,7 @@ func replaceParts(e expreduceapi.Ex, rules []expreduceapi.ExpressionInterface, p } } } - part.GetParts() = part.GetParts()[:len(part.GetParts())-1] + part.SetParts(part.GetParts()[:len(part.GetParts())-1]) if !dirty { return e } diff --git a/expreduce/builtin_system.go b/expreduce/builtin_system.go index 53d3e01..1ab3123 100644 --- a/expreduce/builtin_system.go +++ b/expreduce/builtin_system.go @@ -198,7 +198,7 @@ func applyModuleFn(this expreduceapi.ExpressionInterface, es expreduceapi.EvalSt es.GetDefinedMap().Set(pl.uniqueName, expreduceapi.Def{ Downvalues: []expreduceapi.DownValue{ expreduceapi.DownValue{ - rule: NewExpression([]expreduceapi.Ex{ + Rule: NewExpression([]expreduceapi.Ex{ NewSymbol("System`Rule"), E(S("HoldPattern"), NewSymbol(pl.uniqueName)), rhs, @@ -283,7 +283,7 @@ func GetSystemDefinitions() (defs []Definition) { def, isDef := es.GetDefinedMap().Get(sym.Name) if isDef { - return def.attributes.toSymList() + return def.Attributes.toSymList() } return NewExpression([]expreduceapi.Ex{NewSymbol("System`List")}) }, @@ -557,16 +557,16 @@ func GetSystemDefinitions() (defs []Definition) { // way. // Put system in trace mode: - es.GetTrace() = NewExpression([]expreduceapi.Ex{NewSymbol("System`List")}) + es.SetTrace(NewExpression([]expreduceapi.Ex{NewSymbol("System`List")})) // Evaluate first argument in trace mode: this.GetParts()[1].Eval(es) if es.GetTrace() != nil && len(es.GetTrace().GetParts()) > 2 { // Take system out of trace mode: toReturn := es.GetTrace().DeepCopy() - es.GetTrace() = nil + es.SetTrace(nil) return toReturn } - es.GetTrace() = nil + es.SetTrace(nil) return NewExpression([]expreduceapi.Ex{NewSymbol("System`List")}) }, }) diff --git a/expreduce/cas_test.go b/expreduce/cas_test.go index 499cd5b..763c0f2 100644 --- a/expreduce/cas_test.go +++ b/expreduce/cas_test.go @@ -114,9 +114,9 @@ func TestLowLevel(t *testing.T) { es := NewEvalState() - lhs := NewExpression([]Ex{ + lhs := NewExpression([]expreduceapi.Ex{ NewSymbol("System`Power"), - NewExpression([]Ex{ + NewExpression([]expreduceapi.Ex{ NewSymbol("System`Plus"), NewSymbol("Global`a"), NewSymbol("Global`b"), @@ -124,11 +124,11 @@ func TestLowLevel(t *testing.T) { }), NewInt(0), }) - rule := NewExpression([]Ex{ + rule := NewExpression([]expreduceapi.Ex{ NewSymbol("System`Rule"), - NewExpression([]Ex{ + NewExpression([]expreduceapi.Ex{ NewSymbol("System`Power"), - NewExpression([]Ex{ + NewExpression([]expreduceapi.Ex{ NewSymbol("System`Blank"), }), NewInt(0), @@ -146,9 +146,9 @@ func TestLowLevel(t *testing.T) { assert.Equal(t, "5.5", f.String(es)) // Test nested addition functionality - var a = NewExpression([]Ex{ + var a = NewExpression([]expreduceapi.Ex{ NewSymbol("System`Plus"), - NewExpression([]Ex{ + NewExpression([]expreduceapi.Ex{ NewSymbol("System`Plus"), NewReal(big.NewFloat(80)), NewReal(big.NewFloat(3)), diff --git a/expreduce/definition.go b/expreduce/definition.go index 09a3bff..2010041 100644 --- a/expreduce/definition.go +++ b/expreduce/definition.go @@ -9,7 +9,7 @@ import ( func StringForm(def *expreduceapi.Def, defSym *Symbol, params expreduceapi.ToStringParams) string { var buffer []string - attrs := def.attributes.toStrings() + attrs := def.Attributes.toStrings() if len(attrs) > 0 { e := E( S("Set"), @@ -17,7 +17,7 @@ func StringForm(def *expreduceapi.Def, defSym *Symbol, params expreduceapi.ToStr S("Attributes"), defSym, ), - def.attributes.toSymList(), + def.Attributes.toSymList(), ) buffer = append(buffer, e.StringForm(params)) } @@ -31,14 +31,14 @@ func StringForm(def *expreduceapi.Def, defSym *Symbol, params expreduceapi.ToStr buffer = append(buffer, e.StringForm(params)) } - if def.defaultExpr != nil { + if def.DefaultExpr != nil { e := E( S("Set"), E( S("Default"), defSym, ), - def.defaultExpr, + def.DefaultExpr, ) buffer = append(buffer, e.StringForm(params)) } diff --git a/expreduce/definition_map.go b/expreduce/definition_map.go index 479ac32..7e30e62 100644 --- a/expreduce/definition_map.go +++ b/expreduce/definition_map.go @@ -52,8 +52,8 @@ func (dm ThreadSafeDefinitionMap) CopyDefs() expreduceapi.DefinitionMap { newDef := expreduceapi.Def{} for _, dv := range v.Downvalues { newDv := expreduceapi.DownValue{ - rule: dv.Rule.DeepCopy().(expreduceapi.ExpressionInterface), - specificity: dv.Specificity, + Rule: dv.Rule.DeepCopy().(expreduceapi.ExpressionInterface), + Specificity: dv.Specificity, } newDef.Downvalues = append(newDef.Downvalues, newDv) } diff --git a/expreduce/evalstate.go b/expreduce/evalstate.go index 2959ea2..0606a1a 100644 --- a/expreduce/evalstate.go +++ b/expreduce/evalstate.go @@ -61,13 +61,13 @@ func (this *EvalState) Load(def Definition) { newDef = expreduceapi.Def{} } - if def.legacyEvalFn != nil { - newDef.legacyEvalFn = def.legacyEvalFn + if def.LegacyEvalFn != nil { + newDef.LegacyEvalFn = def.LegacyEvalFn } protectedAttrs := append(def.Attributes, "Protected") - newDef.attributes = stringsToAttributes(protectedAttrs) + newDef.Attributes = stringsToAttributes(protectedAttrs) if def.Default != "" { - newDef.defaultExpr = Interp(def.Default, this) + newDef.DefaultExpr = Interp(def.Default, this) } if def.toString != nil { this.toStringFns[def.Name] = def.toString @@ -77,7 +77,7 @@ func (this *EvalState) Load(def Definition) { } func (es *EvalState) Init(loadAllDefs bool) { - es.GetDefinedMap() = newDefinitionMap() + es.defined = newDefinitionMap() es.toStringFns = make(map[string]expreduceapi.ToStringFnType) // These are fundamental symbols that affect even the parsing of // expressions. We must define them before even the bootstrap definitions. @@ -353,7 +353,7 @@ func (this *EvalState) DefineAttrs(sym *Symbol, rhs expreduceapi.Ex) { this.defined.Set(sym.Name, expreduceapi.Def{}) } tmp := this.defined.GetDef(sym.Name) - tmp.attributes = attrs + tmp.Attributes = attrs this.defined.Set(sym.Name, tmp) } @@ -373,7 +373,7 @@ func (this *EvalState) DefineDownValues(sym *Symbol, rhs expreduceapi.Ex) { if !isRule || len(rule.GetParts()) != 3 { fmt.Println("Assignment to DownValues must be List of Rules.") } - dvs = append(dvs, expreduceapi.DownValue{rule: rule}) + dvs = append(dvs, expreduceapi.DownValue{Rule: rule}) } if !this.IsDef(sym.Name) { @@ -486,7 +486,7 @@ func (this *EvalState) Define(lhs expreduceapi.Ex, rhs expreduceapi.Ex) { newDef := expreduceapi.Def{ Downvalues: []expreduceapi.DownValue{ expreduceapi.DownValue{ - rule: NewExpression([]expreduceapi.Ex{ + Rule: NewExpression([]expreduceapi.Ex{ NewSymbol("System`Rule"), heldLhs, rhs, }), }, @@ -532,8 +532,8 @@ func (this *EvalState) Define(lhs expreduceapi.Ex, rhs expreduceapi.Ex) { tmp.Downvalues[:i], append( []expreduceapi.DownValue{expreduceapi.DownValue{ - rule: newRule, - specificity: newSpecificity, + Rule: newRule, + Specificity: newSpecificity, }}, this.defined.GetDef(name).Downvalues[i:]..., )..., @@ -542,7 +542,7 @@ func (this *EvalState) Define(lhs expreduceapi.Ex, rhs expreduceapi.Ex) { return } } - tmp.Downvalues = append(tmp.Downvalues, expreduceapi.DownValue{rule: NewExpression([]expreduceapi.Ex{NewSymbol("System`Rule"), heldLhs, rhs})}) + tmp.Downvalues = append(tmp.Downvalues, expreduceapi.DownValue{Rule: NewExpression([]expreduceapi.Ex{NewSymbol("System`Rule"), heldLhs, rhs})}) this.defined.Set(name, tmp) } @@ -612,10 +612,18 @@ func (es *EvalState) HasThrown() bool { return es.thrown != nil } +func (es *EvalState) Thrown() expreduceapi.ExpressionInterface { + return es.thrown +} + func (es *EvalState) GetTrace() expreduceapi.ExpressionInterface { return es.trace } +func (es *EvalState) SetTrace(newTrace expreduceapi.ExpressionInterface) { + es.trace = newTrace +} + func (es *EvalState) GetReapSown() expreduceapi.ExpressionInterface { return es.reapSown } diff --git a/expreduce/ex_complex.go b/expreduce/ex_complex.go index ce10f15..34ab46c 100644 --- a/expreduce/ex_complex.go +++ b/expreduce/ex_complex.go @@ -25,7 +25,7 @@ func (this *Complex) Eval(es expreduceapi.EvalStateInterface) expreduceapi.Ex { } func (this *Complex) StringForm(p expreduceapi.ToStringParams) string { - if p.form == "FullForm" { + if p.Form == "FullForm" { return fmt.Sprintf("Complex[%v, %v]", this.Re, this.Im) } p.PreviousHead = "System`Plus" diff --git a/expreduce/ex_expression.go b/expreduce/ex_expression.go index 87fc5e0..e5b37e0 100644 --- a/expreduce/ex_expression.go +++ b/expreduce/ex_expression.go @@ -194,10 +194,7 @@ func (this *Expression) Eval(es expreduceapi.EvalStateInterface) expreduceapi.Ex }) //fmt.Printf("Beginning: appending %v\n", toAppend.StringForm("FullForm")) - es.GetTrace().GetParts() = append( - es.GetTrace().GetParts(), - toAppend, - ) + es.GetTrace().AppendEx(toAppend) } return toReturn } @@ -240,7 +237,7 @@ func (this *Expression) Eval(es expreduceapi.EvalStateInterface) expreduceapi.Ex // Handle tracing traceBak := es.GetTrace() if es.GetTrace() != nil && !es.IsFrozen() { - es.GetTrace() = NewExpression([]expreduceapi.Ex{NewSymbol("System`List")}) + es.SetTrace(NewExpression([]expreduceapi.Ex{NewSymbol("System`List")})) } oldHash := curr.GetParts()[i].Hash() //fmt.Println(curr, i) @@ -258,7 +255,7 @@ func (this *Expression) Eval(es expreduceapi.EvalStateInterface) expreduceapi.Ex //fmt.Printf("Argument eval: appending %v\n", es.trace.DeepCopy().StringForm("FullForm")) traceBak.AppendEx(es.GetTrace().DeepCopy()) } - es.GetTrace() = traceBak + es.SetTrace(traceBak) } } @@ -271,10 +268,7 @@ func (this *Expression) Eval(es expreduceapi.EvalStateInterface) expreduceapi.Ex if !IsSameQ(es.GetTrace().GetParts()[len(es.GetTrace().GetParts())-1], toAppend, es.GetLogger()) { //fmt.Printf("Beginning: appending %v\n", toAppend.StringForm("FullForm")) - es.GetTrace().GetParts() = append( - es.GetTrace().GetParts(), - toAppend, - ) + es.GetTrace().AppendEx(toAppend) } } @@ -314,9 +308,9 @@ func (this *Expression) Eval(es expreduceapi.EvalStateInterface) expreduceapi.Ex legacyEvalFn, hasLegacyEvalFn := (func(*Expression, expreduceapi.EvalStateInterface) expreduceapi.Ex)(nil), false if _, inDefined := es.GetDefinedMap().Get(headStr); inDefined { - if es.GetDefinedMap().GetDef(headStr).legacyEvalFn != nil { + if es.GetDefinedMap().GetDef(headStr).LegacyEvalFn != nil { hasLegacyEvalFn = true - legacyEvalFn = es.GetDefinedMap().GetDef(headStr).legacyEvalFn + legacyEvalFn = es.GetDefinedMap().GetDef(headStr).LegacyEvalFn } } unchanged := true @@ -448,7 +442,7 @@ func (this *Expression) StringForm(params expreduceapi.ToStringParams) string { headAsSym.Name == "System`StandardForm" || headAsSym.Name == "System`OutputForm") { mutatedParams := params - mutatedParams.form = headAsSym.Name[7:] + mutatedParams.Form = headAsSym.Name[7:] return this.GetParts()[1].StringForm(mutatedParams) } @@ -512,7 +506,7 @@ func (this *Expression) DeepCopy() expreduceapi.Ex { func (this *Expression) ShallowCopy() *Expression { var thiscopy = NewEmptyExpression() - thiscopy.GetParts() = append([]expreduceapi.Ex{}, this.GetParts()...) + thiscopy.Parts = append([]expreduceapi.Ex{}, this.GetParts()...) thiscopy.needsEval = this.needsEval thiscopy.correctlyInstantiated = this.correctlyInstantiated thiscopy.evaledHash = this.evaledHash @@ -614,3 +608,7 @@ func NewEmptyExpressionOfLength(n int) *Expression { func (this *Expression) GetParts() []expreduceapi.Ex { return this.GetParts() } + +func (this *Expression) SetParts(newParts []expreduceapi.Ex) { + this.Parts = newParts +} diff --git a/expreduce/ex_symbol.go b/expreduce/ex_symbol.go index cfe511a..559ec16 100644 --- a/expreduce/ex_symbol.go +++ b/expreduce/ex_symbol.go @@ -108,7 +108,7 @@ func (this *Symbol) Attrs(dm expreduceapi.DefinitionMap) expreduceapi.Attributes if !isDef { return expreduceapi.Attributes{} } - return def.attributes + return def.Attributes } func (this *Symbol) Default(dm expreduceapi.DefinitionMap) expreduceapi.Ex { @@ -116,7 +116,7 @@ func (this *Symbol) Default(dm expreduceapi.DefinitionMap) expreduceapi.Ex { if !isDef { return nil } - return def.defaultExpr + return def.DefaultExpr } func stringsToAttributes(strings []string) expreduceapi.Attributes { diff --git a/expreduce/matchq.go b/expreduce/matchq.go index b551d14..e850ace 100644 --- a/expreduce/matchq.go +++ b/expreduce/matchq.go @@ -379,7 +379,7 @@ func (ami *assignedMatchIter) next() bool { //if matches { comp := ami.components[ami.assn[p.formI][p.assnI]] toAddReversed := []*PDManager{} - mi, cont := NewMatchIter(comp, lhs.form, p.pm, ami.es) + mi, cont := NewMatchIter(comp, lhs.Form, p.pm, ami.es) for cont { matchq, submatches, done := mi.next() cont = !done @@ -444,7 +444,7 @@ func NewSequenceMatchIterPreparsed(components []expreduceapi.Ex, lhs_components formMatches[i] = make([]bool, len(components)) num_matches := 0 for j, part := range components { - matchq, _ := IsMatchQ(part, mustContain.form, EmptyPD(), es) + matchq, _ := IsMatchQ(part, mustContain.Form, EmptyPD(), es) if matchq { num_matches++ } diff --git a/expreduce/parse_form.go b/expreduce/parse_form.go index 928df5f..be48085 100644 --- a/expreduce/parse_form.go +++ b/expreduce/parse_form.go @@ -148,8 +148,8 @@ func ParseForm(lhs_component expreduceapi.Ex, isFlat bool, sequenceHead string, res.startI = startI res.endI = endI - res.form = form - res.defaultExpr = defaultExpr + res.Form = form + res.DefaultExpr = defaultExpr res.origForm = lhs_component res.isImpliedBs = isImpliedBs res.isOptional = isOptional diff --git a/expreduce/string.go b/expreduce/string.go index 7e31996..f1dd77c 100644 --- a/expreduce/string.go +++ b/expreduce/string.go @@ -26,7 +26,7 @@ func needsParens(thisHead string, PreviousHead string) bool { } func ToStringInfix(parts []expreduceapi.Ex, delim string, thisHead string, p expreduceapi.ToStringParams) (bool, string) { - if p.form != "InputForm" && p.form != "OutputForm" && p.form != "TeXForm" { + if p.Form != "InputForm" && p.Form != "OutputForm" && p.Form != "TeXForm" { return false, "" } if len(parts) < 2 { @@ -35,7 +35,7 @@ func ToStringInfix(parts []expreduceapi.Ex, delim string, thisHead string, p exp addParens := needsParens(thisHead, p.PreviousHead) var buffer bytes.Buffer if addParens { - if p.form == "TeXForm" { + if p.Form == "TeXForm" { buffer.WriteString("{\\left(") } else { buffer.WriteString("(") @@ -50,7 +50,7 @@ func ToStringInfix(parts []expreduceapi.Ex, delim string, thisHead string, p exp } } if addParens { - if p.form == "TeXForm" { + if p.Form == "TeXForm" { buffer.WriteString("\\right)}") } else { buffer.WriteString(")") @@ -59,7 +59,7 @@ func ToStringInfix(parts []expreduceapi.Ex, delim string, thisHead string, p exp return true, buffer.String() } -func (this expreduceapi.ExpressionInterface) ToStringInfix(p expreduceapi.ToStringParams) (bool, string) { +/*func ToStringInfix(this expreduceapi.ExpressionInterface, p expreduceapi.ToStringParams) (bool, string) { if len(this.GetParts()) != 3 { return false, "" } @@ -69,7 +69,7 @@ func (this expreduceapi.ExpressionInterface) ToStringInfix(p expreduceapi.ToStri return false, "" } return ToStringInfix(expr.GetParts()[1:], delim.Val, "", p) -} +}*/ // TODO(corywalker): Remove start, end. No users of these values. func ToStringInfixAdvanced(parts []expreduceapi.Ex, delim string, thisHead string, surroundEachArg bool, start string, end string, params expreduceapi.ToStringParams) (bool, string) {