-
Notifications
You must be signed in to change notification settings - Fork 25
/
builtin_flowcontrol.go
185 lines (179 loc) · 4.88 KB
/
builtin_flowcontrol.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package expreduce
func applyWithFn(e *Expression, es *EvalState) (Ex, bool) {
if len(e.Parts) != 3 {
return nil, false
}
vars, isList := HeadAssertion(e.Parts[1], "System`List")
if !isList {
return nil, false
}
rules := []*Expression{}
for _, vDef := range vars.Parts[1:] {
set, isSet := HeadAssertion(vDef, "System`Set")
setDelayed, isSetDelayed := HeadAssertion(vDef, "System`SetDelayed")
if !(isSet || isSetDelayed) {
return nil, false
}
var setEx *Expression = nil
ruleHead := ""
if isSet {
setEx = set
ruleHead = "System`Rule"
} else {
setEx = setDelayed
ruleHead = "System`RuleDelayed"
}
if len(setEx.Parts) != 3 {
return nil, false
}
rules = append(rules, NewExpression([]Ex{
&Symbol{ruleHead},
setEx.Parts[1],
setEx.Parts[2],
}))
}
return rulesReplaceAll(e.Parts[2], rules, es), true
}
func GetFlowControlDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "If",
// WARNING: Watch out for putting rules here. It can interfere with how
// Return works.
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) > 4 || len(this.Parts) < 3 {
return this
}
var falseVal Ex = &Symbol{"System`Null"}
if len(this.Parts) == 4 {
falseVal = this.Parts[3]
}
var isequal string = this.Parts[1].IsEqual(&Symbol{"System`True"}, &es.CASLogger)
if isequal == "EQUAL_UNK" {
return this
} else if isequal == "EQUAL_TRUE" {
return this.Parts[2]
} else if isequal == "EQUAL_FALSE" {
return falseVal
}
return NewExpression([]Ex{&Symbol{"System`Error"}, &String{"Unexpected equality return value."}})
},
})
defs = append(defs, Definition{
Name: "While",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 3 {
return this
}
isequal := this.Parts[1].DeepCopy().Eval(es).IsEqual(&Symbol{"System`True"}, &es.CASLogger)
cont := isequal == "EQUAL_TRUE"
for cont {
tmpRes := this.Parts[2].DeepCopy().Eval(es)
retVal, isReturn := tryReturnValue(tmpRes)
if isReturn {
return retVal
}
isequal = this.Parts[1].DeepCopy().Eval(es).IsEqual(&Symbol{"System`True"}, &es.CASLogger)
cont = isequal == "EQUAL_TRUE"
}
if isequal == "EQUAL_UNK" {
return NewExpression([]Ex{&Symbol{"System`Error"}, &String{"Encountered EQUAL_UNK when evaluating test for the While."}})
} else if isequal == "EQUAL_TRUE" {
return &Symbol{"System`Null"}
} else if isequal == "EQUAL_FALSE" {
return &Symbol{"System`Null"}
}
return NewExpression([]Ex{&Symbol{"System`Error"}, &String{"Unexpected equality return value."}})
},
})
defs = append(defs, Definition{
Name: "CompoundExpression",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
var toReturn Ex
for i := 1; i < len(this.Parts); i++ {
toReturn = this.Parts[i].Eval(es)
if es.HasThrown() {
return es.thrown
}
if _, isReturn := HeadAssertion(toReturn, "System`Return"); isReturn {
return toReturn
}
}
return toReturn
},
})
// https://mathematica.stackexchange.com/questions/29353/how-does-return-work
defs = append(defs, Definition{Name: "Return"})
defs = append(defs, Definition{
Name: "Which",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) % 2 != 1 {
return this
}
for i := 1; i < len(this.Parts); i += 2 {
condRes := this.Parts[i].Eval(es)
resSym, resIsSym := condRes.(*Symbol)
if !resIsSym {
continue
}
if resSym.Name == "System`True" {
return this.Parts[i+1]
}
}
return &Symbol{"System`Null"}
},
})
defs = append(defs, Definition{
Name: "Switch",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) < 4 || len(this.Parts) % 2 != 0 {
return this
}
for i := 2; i < len(this.Parts); i += 2 {
if match, _ := IsMatchQ(this.Parts[1], this.Parts[i], EmptyPD(), es); match {
return this.Parts[i+1]
}
}
return this
},
})
defs = append(defs, Definition{
Name: "With",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
res, ok := applyWithFn(this, es)
if !ok {
return this
}
return res
},
})
defs = append(defs, Definition{
Name: "Do",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) >= 3 {
mis, isOk := multiIterSpecFromLists(es, this.Parts[2:])
if isOk {
// Simulate evaluation within Block[]
mis.takeVarSnapshot(es)
for mis.cont() {
mis.defineCurrent(es)
res := this.Parts[1].DeepCopy().Eval(es)
if es.HasThrown() {
return es.thrown
}
if asReturn, isReturn := HeadAssertion(res, "System`Return"); isReturn {
if len(asReturn.Parts) < 2 {
return &Symbol{"System`Null"}
}
return asReturn.Parts[1]
}
mis.next()
}
mis.restoreVarSnapshot(es)
return &Symbol{"System`Null"}
}
}
return this
},
})
return
}