Skip to content

Commit

Permalink
Fix some Int rule cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Sep 27, 2017
1 parent 309ecc2 commit 984eec1
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
46 changes: 41 additions & 5 deletions expreduce/builtin_system.go
Expand Up @@ -111,7 +111,7 @@ func TryReadFile(fn Ex, es *EvalState) (string, string, bool) {
return "", "", false
}

func snagUnique(prefix string, es *EvalState) (string, bool) {
func snagUnique(context string, prefix string, es *EvalState) (string, bool) {
mnExpr, mnIsDef := es.GetSymDef("System`$ModuleNumber")
if !mnIsDef {
return "", false
Expand All @@ -124,7 +124,7 @@ func snagUnique(prefix string, es *EvalState) (string, bool) {

// Find the next ModuleNumber to use.
for {
toTry := fmt.Sprintf("%v$%v", prefix, mn)
toTry := fmt.Sprintf("%v%v%v", context, prefix, mn)
if !es.IsDef(toTry) {
es.Define(NewSymbol("System`$ModuleNumber"), NewInteger(big.NewInt(mn+1)))
return toTry, true
Expand Down Expand Up @@ -177,7 +177,7 @@ func applyModuleFn(this *Expression, es *EvalState) (Ex, bool) {
}

for i := range parsedLocals {
unique, ok := snagUnique(parsedLocals[i].sym.Name, es)
unique, ok := snagUnique(parsedLocals[i].sym.Name, "$", es)
if !ok {
log.Fatal("Error snagging unique.")
}
Expand Down Expand Up @@ -726,16 +726,52 @@ func GetSystemDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "Unique",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 1 {
if len(this.Parts) > 3 {
return this
}
unique, ok := snagUnique("Global`", es)
prefix := "$"
if len(this.Parts) == 2 {
asStr, isStr := this.Parts[1].(*String)
if !isStr {
return this
}
prefix = asStr.Val
}
unique, ok := snagUnique("Global`", prefix, es)
if !ok {
log.Fatal("Error snagging unique.")
}
return NewSymbol(unique)
},
})
defs = append(defs, Definition{
Name: "Sow",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
fmt.Println("Unsupported call to Sow.")
return this
}
res := this.Parts[1].Eval(es)
if es.reapSown != nil {
es.reapSown.appendEx(res)
}
return res
},
})
defs = append(defs, Definition{
Name: "Reap",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
fmt.Println("Unsupported call to Reap.")
return this
}
es.reapSown = E(S("List"))
res := this.Parts[1].Eval(es)
res = E(S("List"), res, E(S("List"), es.reapSown))
es.reapSown = nil
return res
},
})
defs = append(defs, Definition{
Name: "Defer",
OmitDocumentation: true,
Expand Down
2 changes: 2 additions & 0 deletions expreduce/evalstate.go
Expand Up @@ -23,6 +23,7 @@ type EvalState struct {
timeCounter TimeCounterGroup
freeze bool
thrown *Expression
reapSown *Expression
interrupted bool
}

Expand Down Expand Up @@ -140,6 +141,7 @@ func (es *EvalState) Init(loadAllDefs bool) {
es.MarkSeen("System`Temporary")
es.MarkSeen("System`Stub")
es.MarkSeen("System`$Failed")
es.MarkSeen("System`Null")

es.MarkSeen("System`Exp")
es.MarkSeen("System`AppellF1")
Expand Down
16 changes: 8 additions & 8 deletions expreduce/resources.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions expreduce/resources/power.m
Expand Up @@ -241,6 +241,7 @@
genExpand[List @@ s, possibleExponents[n, Length[s]]],
c_*s_Plus :> ((c*#) &) /@ s
};
Expand[a_, x_] := (Print["Expand does not support second argument."];Expand[a]);
Attributes[Expand] = {Protected};
Tests`Expand = {
ESimpleExamples[
Expand Down
6 changes: 6 additions & 0 deletions expreduce/resources/system.m
Expand Up @@ -315,3 +315,9 @@
Attributes[Unique] = {Protected};

Attributes[Defer] = {HoldAll, Protected, ReadProtected};

Sow::usage = "`Sow[e]` sows a value `e` for `Reap[]`.";
Attributes[Sow] = {Protected};

Reap::usage = "`Reap[expr]` returns the result of `expr` and a list of all the sown values during evaluation.";
Attributes[Reap] = {HoldFirst, Protected};

0 comments on commit 984eec1

Please sign in to comment.