Skip to content

Commit

Permalink
Refactor checkpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Oct 19, 2018
1 parent 1c4555f commit 10486e4
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 59 deletions.
4 changes: 2 additions & 2 deletions expreduce/builtin_flowcontrol.go
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion expreduce/builtin_functional.go
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion expreduce/builtin_replacement.go
Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions expreduce/builtin_system.go
Expand Up @@ -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,
Expand Down Expand Up @@ -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")})
},
Expand Down Expand Up @@ -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")})
},
})
Expand Down
14 changes: 7 additions & 7 deletions expreduce/cas_test.go
Expand Up @@ -114,21 +114,21 @@ 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"),
NewSymbol("Global`c"),
}),
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),
Expand All @@ -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)),
Expand Down
8 changes: 4 additions & 4 deletions expreduce/definition.go
Expand Up @@ -9,15 +9,15 @@ 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"),
E(
S("Attributes"),
defSym,
),
def.attributes.toSymList(),
def.Attributes.toSymList(),
)
buffer = append(buffer, e.StringForm(params))
}
Expand All @@ -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))
}
Expand Down
4 changes: 2 additions & 2 deletions expreduce/definition_map.go
Expand Up @@ -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)
}
Expand Down
30 changes: 19 additions & 11 deletions expreduce/evalstate.go
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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,
}),
},
Expand Down Expand Up @@ -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:]...,
)...,
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion expreduce/ex_complex.go
Expand Up @@ -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"
Expand Down
26 changes: 12 additions & 14 deletions expreduce/ex_expression.go
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}

Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions expreduce/ex_symbol.go
Expand Up @@ -108,15 +108,15 @@ 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 {
def, isDef := dm.Get(this.Name)
if !isDef {
return nil
}
return def.defaultExpr
return def.DefaultExpr
}

func stringsToAttributes(strings []string) expreduceapi.Attributes {
Expand Down
4 changes: 2 additions & 2 deletions expreduce/matchq.go
Expand Up @@ -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
Expand Down Expand Up @@ -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++
}
Expand Down
4 changes: 2 additions & 2 deletions expreduce/parse_form.go
Expand Up @@ -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
Expand Down

0 comments on commit 10486e4

Please sign in to comment.