Skip to content

Commit

Permalink
Checkpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
corywalker committed Oct 19, 2018
1 parent 6889997 commit 3a5832f
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 22 deletions.
6 changes: 3 additions & 3 deletions expreduce/builtin_list.go
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/corywalker/expreduce/pkg/expreduceapi"
)

func (this *Expression) ToStringList(params expreduceapi.ToStringParams) (bool, string) {
func ToStringList(this expreduceapi.ExpressionInterface, params expreduceapi.ToStringParams) (bool, string) {
if params.Form == "FullForm" {
return false, ""
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func countFunctionLevelSpec(pattern expreduceapi.Ex, e expreduceapi.Ex, partList
func GetListDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "List",
toString: (expreduceapi.ExpressionInterface).ToStringList,
toString: ToStringList,
})
defs = append(defs, Definition{
Name: "Total",
Expand Down Expand Up @@ -596,7 +596,7 @@ func GetListDefinitions() (defs []Definition) {
part,
})).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_manip.go
Expand Up @@ -17,7 +17,7 @@ func distribute(e expreduceapi.ExpressionInterface, built expreduceapi.Expressio
}
if shouldDistribute {
for _, dPart := range partAsExpr.GetParts()[1:] {
builtCopy := built.ShallowCopy()
builtCopy := ShallowCopy(built)
builtCopy.AppendEx(dPart)
distribute(e, builtCopy, res)
}
Expand Down
8 changes: 4 additions & 4 deletions expreduce/builtin_matrix.go
Expand Up @@ -45,16 +45,16 @@ func intSliceToList(ints []int64) expreduceapi.Ex {

// This function assumes that mat is a matrix and that i and j are not out of
// bounds. i and j are 1-indexed.
func (mat *Expression) matrix2dGetElem(i, j int64) expreduceapi.Ex {
func matrix2dGetElem(mat expreduceapi.ExpressionInterface, i, j int64) expreduceapi.Ex {
return (mat.GetParts()[i].(expreduceapi.ExpressionInterface)).GetParts()[j]
}

func calcIJ(i, j, innerDim int64, a, b expreduceapi.ExpressionInterface) expreduceapi.Ex {
toReturn := NewExpression([]expreduceapi.Ex{NewSymbol("System`Plus")})
for pairI := int64(1); pairI <= innerDim; pairI++ {
toAdd := NewExpression([]expreduceapi.Ex{NewSymbol("System`Times")})
toAdd.AppendEx(a.matrix2dGetElem(i, pairI))
toAdd.AppendEx(b.matrix2dGetElem(pairI, j))
toAdd.AppendEx(matrix2dGetElem(a, i, pairI))
toAdd.AppendEx(matrix2dGetElem(b, pairI, j))
toReturn.AppendEx(toAdd)
}
return toReturn
Expand Down Expand Up @@ -162,7 +162,7 @@ func GetMatrixDefinitions() (defs []Definition) {
for tI := int64(1); tI <= w; tI++ {
tRow := NewExpression([]expreduceapi.Ex{NewSymbol("System`List")})
for tJ := int64(1); tJ <= h; tJ++ {
tRow.AppendEx(l.matrix2dGetElem(tJ, tI))
tRow.AppendEx(matrix2dGetElem(l, tJ, tI))
}
toReturn.AppendEx(tRow)
}
Expand Down
2 changes: 1 addition & 1 deletion expreduce/builtin_replacement.go
Expand Up @@ -52,7 +52,7 @@ func replaceParts(e expreduceapi.Ex, rules []expreduceapi.ExpressionInterface, p
return e
}
res := E(expr.GetParts()[0])
part.AppendExArray(NewInt)
part.AppendEx(NewInt(0))
dirty := false
for i, p := range expr.GetParts()[1:] {
part.GetParts()[len(part.GetParts())-1] = NewInt(int64(i + 1))
Expand Down
2 changes: 1 addition & 1 deletion expreduce/builtin_string.go
Expand Up @@ -56,7 +56,7 @@ func GetStringDefinitions() (defs []Definition) {
})
defs = append(defs, Definition{
Name: "Infix",
toString: (expreduceapi.ExpressionInterface).ToStringInfix,
toString: ToStringInfixFn,
})
defs = append(defs, Definition{
Name: "StringLength",
Expand Down
5 changes: 3 additions & 2 deletions expreduce/ex_expression.go
Expand Up @@ -385,7 +385,7 @@ func (this *Expression) EvalFunction(es expreduceapi.EvalStateInterface, args []
return this
}

func (this *Expression) ReplaceAll(r *Expression, stopAtHead string, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
func ExprReplaceAll(this expreduceapi.ExpressionInterface, r expreduceapi.ExpressionInterface, stopAtHead string, es expreduceapi.EvalStateInterface) expreduceapi.Ex {
es.Debugf("In Expression.ReplaceAll. First trying IsMatchQ(this, r.Parts[1], es).")
es.Debugf("Rule r is: %s", r)

Expand Down Expand Up @@ -504,7 +504,8 @@ func (this *Expression) DeepCopy() expreduceapi.Ex {
return thiscopy
}

func (this *Expression) ShallowCopy() *Expression {
func ShallowCopy(thisExprInt expreduceapi.ExpressionInterface) *Expression {
this := thisExprInt.(*Expression)
var thiscopy = NewEmptyExpression()
thiscopy.Parts = append([]expreduceapi.Ex{}, this.GetParts()...)
thiscopy.needsEval = this.needsEval
Expand Down
2 changes: 1 addition & 1 deletion expreduce/ex_symbol.go
Expand Up @@ -249,7 +249,7 @@ func attrsToStrings(this *expreduceapi.Attributes) []string {

func attrsToSymList(this *expreduceapi.Attributes) expreduceapi.ExpressionInterface {
toReturn := E(S("List"))
for _, s := range this.toStrings() {
for _, s := range attrsToStrings(this) {
toReturn.AppendEx(S(s))
}
return toReturn
Expand Down
2 changes: 1 addition & 1 deletion expreduce/parse_form.go
Expand Up @@ -149,7 +149,7 @@ func ParseForm(lhs_component expreduceapi.Ex, isFlat bool, sequenceHead string,
res.startI = startI
res.endI = endI
res.form = form
res.DefaultExpr = defaultExpr
res.defaultExpr = defaultExpr
res.origForm = lhs_component
res.isImpliedBs = isImpliedBs
res.isOptional = isOptional
Expand Down
2 changes: 1 addition & 1 deletion expreduce/replace.go
Expand Up @@ -104,7 +104,7 @@ func ReplaceAll(this expreduceapi.Ex, r expreduceapi.ExpressionInterface, es exp
} else {
// Continue recursion
es.Debugf("ReplaceAll(%v, %v, es, %v)", this, r, pm)
return asExpression.ReplaceAll(r, stopAtHead, es)
return ExprReplaceAll(asExpression, r, stopAtHead, es)
}
}
if res, matches := IsMatchQ(this, r.GetParts()[1], pm, es); res {
Expand Down
4 changes: 2 additions & 2 deletions expreduce/string.go
Expand Up @@ -59,7 +59,7 @@ func ToStringInfix(parts []expreduceapi.Ex, delim string, thisHead string, p exp
return true, buffer.String()
}

/*func ToStringInfix(this expreduceapi.ExpressionInterface, p expreduceapi.ToStringParams) (bool, string) {
func ToStringInfixFn(this expreduceapi.ExpressionInterface, p expreduceapi.ToStringParams) (bool, string) {
if len(this.GetParts()) != 3 {
return false, ""
}
Expand All @@ -69,7 +69,7 @@ func ToStringInfix(parts []expreduceapi.Ex, delim string, thisHead string, p exp
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) {
Expand Down
25 changes: 20 additions & 5 deletions pkg/expreduceapi/cas.go
Expand Up @@ -4,16 +4,17 @@ package expreduceapi

import (
gologging "github.com/op/go-logging"
"github.com/corywalker/expreduce/expreduce/timecounter"
)

type ToStringFnType (func(ExpressionInterface, ToStringParams) (bool, string))
type ToStringParams struct {
form string
context StringInterface
Form string
Context StringInterface
ContextPath ExpressionInterface
PreviousHead string
// Used by Definition[]
esi EvalStateInterface
Esi EvalStateInterface
}

// The interface that fundamental types must implement.
Expand Down Expand Up @@ -58,19 +59,30 @@ type EvalStateInterface interface {
GetDefinedSnapshot() DefinitionMap
IsFrozen() bool
SetFrozen(frozen bool)
IsInterrupted() bool
GetStringDef(name string, defaultVal string) string
GetListDef(name string) ExpressionInterface
Throw(e ExpressionInterface)
HasThrown() bool
Thrown() ExpressionInterface
ProcessTopLevelResult(in Ex, out Ex) Ex

GetLogger() LoggingInterface
GetTrace() ExpressionInterface
SetTrace(newTrace ExpressionInterface)
GetDefinedMap() DefinitionMap
GetReapSown() ExpressionInterface
SetReapSown(ex ExpressionInterface)

GetTimeCounter() *timecounter.Group
}

type ExpressionInterface interface {
Ex

GetParts() []Ex
SetParts(newParts []Ex)
ClearHashes()

EvalFunction(es EvalStateInterface, args []Ex) Ex
Len() int
Expand All @@ -83,6 +95,8 @@ type ExpressionInterface interface {

type StringInterface interface {
Ex

GetValue() string
}

type DefinitionMap interface {
Expand All @@ -99,13 +113,14 @@ type DownValue struct {
Specificity int
}

type EvalFnType (func(ExpressionInterface, EvalStateInterface) Ex)
type Def struct {
Downvalues []DownValue
attributes Attributes
Attributes Attributes
DefaultExpr Ex

// A function defined here will override downvalues.
legacyEvalFn (func(ExpressionInterface, EvalStateInterface) Ex)
LegacyEvalFn EvalFnType
}

// Functions for working with the attributes of symbols:
Expand Down

0 comments on commit 3a5832f

Please sign in to comment.