/
builtin.go
210 lines (197 loc) · 6.51 KB
/
builtin.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package expreduce
import "log"
import "fmt"
type Rule struct {
Lhs string
Rhs string
}
type Definition struct {
// The symbol name, like "Mean", and "Total"
Name string
Usage string
// Currently used for SetDelayed, since other definitions depend on
// SetDelayed, we define it first.
Bootstrap bool
OmitDocumentation bool
ExpreduceSpecific bool
Details string
// Map symbol to Eval() function
legacyEvalFn (func(*Expression, *EvalState) Ex)
SimpleExamples []TestInstruction
FurtherExamples []TestInstruction
Tests []TestInstruction
KnownFailures []TestInstruction
KnownDangerous []TestInstruction
toString ToStringFnType
Attributes []string
Default string
}
func ToTestInstructions(tc *Expression) []TestInstruction {
instructions := []TestInstruction{}
for _, tiEx := range tc.Parts[1:] {
if st, isSt := HeadAssertion(tiEx, "System`ESameTest"); isSt {
if len(st.Parts) != 3 {
log.Fatalf("Invalid test case: %v\n", tiEx)
continue
}
instructions = append(instructions, &SameTestEx{
st.Parts[1], st.Parts[2]})
continue
}
if st, isSt := HeadAssertion(tiEx, "System`EStringTest"); isSt {
if len(st.Parts) != 3 {
log.Fatalf("Invalid test case: %v\n", tiEx)
continue
}
instructions = append(instructions, &StringTest{
st.Parts[1].(*String).Val, st.Parts[2].(*String).Val})
continue
}
if st, isSt := HeadAssertion(tiEx, "System`EExampleOnlyInstruction"); isSt {
if len(st.Parts) != 3 {
log.Fatalf("Invalid test case: %v\n", tiEx)
continue
}
instructions = append(instructions, &ExampleOnlyInstruction{
st.Parts[1].(*String).Val, st.Parts[2].(*String).Val})
continue
}
if st, isSt := HeadAssertion(tiEx, "System`EComment"); isSt {
if len(st.Parts) != 2 {
log.Fatalf("Invalid test case: %v\n", tiEx)
continue
}
comStr, comIsStr := st.Parts[1].(*String)
if !comIsStr {
log.Fatalf("Invalid test case: %v\n", tiEx)
continue
}
instructions = append(instructions, &TestComment{
comStr.Val})
continue
}
if st, isSt := HeadAssertion(tiEx, "System`EResetState"); isSt {
if len(st.Parts) != 1 {
log.Fatalf("Invalid test case: %v\n", tiEx)
continue
}
instructions = append(instructions, &ResetState{})
continue
}
if _, isSt := HeadAssertion(tiEx, "System`List"); isSt {
fmt.Printf("Ignoring unfilled test: %v\n", tiEx)
continue
}
log.Fatalf("Invalid test case: %v\n", tiEx)
}
return instructions
}
func (def *Definition) AnnotateWithDynamicTests(es *EvalState) {
tests, testsDef := es.GetSymDef("Tests`" + def.Name)
if !testsDef {
return
}
testsList, testsIsList := HeadAssertion(tests, "System`List")
if !testsIsList {
return
}
for _, testCol := range testsList.Parts[1:] {
testColExpr, testColIsExpr := testCol.(*Expression)
if !testColIsExpr {
continue
}
headSym, headIsSym := testColExpr.Parts[0].(*Symbol)
if !headIsSym {
continue
}
if headSym.Name == "System`ESimpleExamples" {
def.SimpleExamples = append(
def.SimpleExamples,
ToTestInstructions(testColExpr)...)
} else if headSym.Name == "System`EFurtherExamples" {
def.FurtherExamples = append(
def.FurtherExamples,
ToTestInstructions(testColExpr)...)
} else if headSym.Name == "System`ETests" {
def.Tests = append(
def.Tests,
ToTestInstructions(testColExpr)...)
} else if headSym.Name == "System`EKnownFailures" {
def.KnownFailures = append(
def.KnownFailures,
ToTestInstructions(testColExpr)...)
} else if headSym.Name == "System`EKnownDangerous" {
def.KnownDangerous = append(
def.KnownDangerous,
ToTestInstructions(testColExpr)...)
} else {
log.Fatalf("Invalid test collection: %v\n", testColExpr)
}
}
}
func (def *Definition) AnnotateWithDynamicUsage(es *EvalState) {
if len(def.Usage) > 0 {
return
}
lhs := NewExpression([]Ex{
NewSymbol("System`MessageName"),
NewSymbol("System`" + def.Name),
NewString("usage"),
})
usage, usageIsDef, _ := es.GetDef("System`MessageName", lhs)
if !usageIsDef {
return
}
if usageStr, usageIsStr := usage.(*String); usageIsStr {
def.Usage = usageStr.Val
}
}
func (def *Definition) AnnotateWithDynamic(es *EvalState) {
def.AnnotateWithDynamicTests(es)
def.AnnotateWithDynamicUsage(es)
}
type NamedDefSet struct {
Name string
Defs []Definition
}
func GetAllDefinitions() (defs []NamedDefSet) {
defs = append(defs, NamedDefSet{"combinatorics", getCombinatoricsDefinitions()})
defs = append(defs, NamedDefSet{"calculus", getCalculusDefinitions()})
defs = append(defs, NamedDefSet{"comparison", getComparisonDefinitions()})
defs = append(defs, NamedDefSet{"atoms", getAtomsDefinitions()})
defs = append(defs, NamedDefSet{"functional", getFunctionalDefinitions()})
defs = append(defs, NamedDefSet{"expression", GetExpressionDefinitions()})
defs = append(defs, NamedDefSet{"solve", GetSolveDefinitions()})
defs = append(defs, NamedDefSet{"flowcontrol", GetFlowControlDefinitions()})
defs = append(defs, NamedDefSet{"list", GetListDefinitions()})
defs = append(defs, NamedDefSet{"matrix", GetMatrixDefinitions()})
defs = append(defs, NamedDefSet{"arithmetic", getArithmeticDefinitions()})
defs = append(defs, NamedDefSet{"specialsyms", getSpecialSymsDefinitions()})
defs = append(defs, NamedDefSet{"power", GetPowerDefinitions()})
defs = append(defs, NamedDefSet{"random", GetRandomDefinitions()})
defs = append(defs, NamedDefSet{"replacement", getReplacementDefinitions()})
defs = append(defs, NamedDefSet{"sort", GetSortDefinitions()})
defs = append(defs, NamedDefSet{"system", GetSystemDefinitions()})
defs = append(defs, NamedDefSet{"trig", GetTrigDefinitions()})
defs = append(defs, NamedDefSet{"plot", GetPlotDefinitions()})
defs = append(defs, NamedDefSet{"string", GetStringDefinitions()})
defs = append(defs, NamedDefSet{"time", GetTimeDefinitions()})
defs = append(defs, NamedDefSet{"pattern", GetPatternDefinitions()})
defs = append(defs, NamedDefSet{"boolean", GetBooleanDefinitions()})
defs = append(defs, NamedDefSet{"simplify", GetSimplifyDefinitions()})
defs = append(defs, NamedDefSet{"numbertheory", GetNumberTheoryDefinitions()})
defs = append(defs, NamedDefSet{"manip", GetManipDefinitions()})
defs = append(defs, NamedDefSet{"rubi", GetRubiDefinitions()})
// Check for duplicate definitions
definedNames := make(map[string]bool)
for _, defSet := range defs {
for _, def := range defSet.Defs {
_, alreadyDefined := definedNames[def.Name]
if alreadyDefined {
log.Fatalf("Found duplicate definition: %v\n", def.Name)
}
definedNames[def.Name] = true
}
}
return
}