-
Notifications
You must be signed in to change notification settings - Fork 25
/
builtin_system.go
788 lines (759 loc) · 21.1 KB
/
builtin_system.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
package expreduce
import "math/big"
import "time"
import "fmt"
import "os"
import "strings"
import "runtime/pprof"
import "log"
import "io/ioutil"
import "github.com/op/go-logging"
import "flag"
var mymemprofile = flag.String("mymemprofile", "", "write memory profile to this file")
func hashEx(e Ex) uint64 {
return e.Hash()
}
func exprToN(es *EvalState, e Ex) Ex {
asInt, isInt := e.(*Integer)
if isInt {
toReturn, _ := IntegerToFlt(asInt)
return toReturn
}
asRat, isRat := e.(*Rational)
if isRat {
toReturn, _ := RationalToFlt(asRat)
return toReturn
}
_, isSym := e.(*Symbol)
if isSym {
toReturn, defined, _ := es.GetDef(
"System`N",
NewExpression([]Ex{NewSymbol("System`N"), e}),
)
if defined {
return toReturn
}
}
asExpr, isExpr := e.(*Expression)
if isExpr {
toReturn, defined, _ := es.GetDef(
"System`N",
NewExpression([]Ex{NewSymbol("System`N"), e}),
)
if defined {
return toReturn
}
exToReturn := NewEmptyExpression()
for _, part := range asExpr.Parts {
toAdd, defined, _ := es.GetDef(
"System`N",
NewExpression([]Ex{NewSymbol("System`N"), part}),
)
if !defined {
toAdd = exprToN(es, part)
}
exToReturn.Parts = append(exToReturn.Parts, toAdd)
}
return exToReturn
}
return e.DeepCopy()
}
func TryReadFile(fn Ex, es *EvalState) (string, string, bool) {
pathSym := NewSymbol("System`$Path")
path, isDef, _ := es.GetDef("System`$Path", pathSym)
if !isDef {
return "", "", false
}
pathL, pathIsList := HeadAssertion(path, "System`List")
if !pathIsList {
return "", "", false
}
filenameString, fnIsStr := fn.(*String)
if !fnIsStr {
return "", "", false
}
rawFn := filenameString.Val
// Handle resource file reads
if strings.HasPrefix(rawFn, "__res__/") {
fileData, err := Asset("resources/" + rawFn[8:])
if err == nil {
return string(fileData), rawFn, true
}
}
pathsToTry := []string{}
for _, pathEx := range pathL.Parts[1:] {
pathString, pathIsString := pathEx.(*String)
if !pathIsString {
fmt.Printf("Invalid path: %v\n", pathEx)
continue
}
rawDir := pathString.Val
rawPath := rawDir + string(os.PathSeparator) + rawFn
pathsToTry = append(pathsToTry, rawPath)
}
pathsToTry = append(pathsToTry, rawFn)
for _, rawPath := range pathsToTry {
dat, err := ioutil.ReadFile(rawPath)
if err != nil {
continue
}
fileData := string(dat)
return fileData, rawPath, true
}
return "", "", false
}
func snagUnique(context string, prefix string, es *EvalState) (string, bool) {
mnExpr, mnIsDef := es.GetSymDef("System`$ModuleNumber")
if !mnIsDef {
return "", false
}
mnInteger, mnIsInt := mnExpr.(*Integer)
if !mnIsInt {
return "", false
}
mn := mnInteger.Val.Int64()
// Find the next ModuleNumber to use.
for {
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
}
mn += 1
}
return "", false
}
func applyModuleFn(this *Expression, es *EvalState) (Ex, bool) {
// Coarse parsing of arguments.
if len(this.Parts) != 3 {
return nil, false
}
locals, localsIsList := HeadAssertion(this.Parts[1], "System`List")
if !localsIsList {
return nil, false
}
// Parse locals into a struct
type parsedLocal struct {
sym *Symbol
uniqueName string
setValue Ex
isSet bool
isSetDelayed bool
}
var parsedLocals []parsedLocal
for _, localEx := range locals.Parts[1:] {
pl := parsedLocal{}
symEx := localEx
localSet, localIsSet := HeadAssertion(localEx, "System`Set")
pl.isSet = localIsSet
localSetDelayed, localIsSetDelayed := HeadAssertion(localEx, "System`SetDelayed")
pl.isSetDelayed = localIsSetDelayed
if localIsSet && len(localSet.Parts) == 3 {
symEx = localSet.Parts[1]
pl.setValue = localSet.Parts[2]
}
if localIsSetDelayed && len(localSetDelayed.Parts) == 3 {
symEx = localSetDelayed.Parts[1]
pl.setValue = localSetDelayed.Parts[2]
}
localSym, localIsSym := symEx.(*Symbol)
pl.sym = localSym
if !localIsSym {
return nil, false
}
parsedLocals = append(parsedLocals, pl)
}
for i := range parsedLocals {
unique, ok := snagUnique(parsedLocals[i].sym.Name, "$", es)
if !ok {
log.Fatal("Error snagging unique.")
}
parsedLocals[i].uniqueName = unique
}
toReturn := this.Parts[2]
pm := EmptyPD()
for _, pl := range parsedLocals {
if pl.isSet || pl.isSetDelayed {
rhs := pl.setValue
if pl.isSet {
rhs = rhs.Eval(es)
}
es.defined[pl.uniqueName] = Def{
downvalues: []DownValue{
DownValue{
rule: NewExpression([]Ex{
NewSymbol("System`Rule"),
E(S("HoldPattern"), NewSymbol(pl.uniqueName)),
rhs,
}),
},
},
}
} else {
es.defined[pl.uniqueName] = Def{}
}
pm.LazyMakeMap()
pm.patternDefined[pl.sym.Name] = NewSymbol(pl.uniqueName)
}
toReturn = ReplacePD(toReturn, es, pm)
return toReturn, true
}
func GetSystemDefinitions() (defs []Definition) {
defs = append(defs, Definition{
Name: "ExpreduceSetLogging",
Details: "Logging output prints to the console. There can be a lot of logging output, especially for more complicated pattern matches. Valid levels are `Debug`, `Info`, `Notice`, `Warning`, `Error`, and `Critical`.",
ExpreduceSpecific: true,
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 3 {
return this
}
sym, ok := this.Parts[1].(*Symbol)
if ok {
if sym.Name == "System`True" {
errorStr := "Invalid level. Must be one of {Debug, Info, Notice}."
levelSym, lsOk := this.Parts[2].(*Symbol)
if !lsOk {
return NewExpression([]Ex{NewSymbol("System`Error"), NewString(errorStr)})
}
if levelSym.Name == "System`Debug" {
es.DebugOn(logging.DEBUG)
} else if levelSym.Name == "System`Info" {
es.DebugOn(logging.INFO)
} else if levelSym.Name == "System`Notice" {
es.DebugOn(logging.NOTICE)
} else {
return NewExpression([]Ex{NewSymbol("System`Error"), NewString(errorStr)})
}
return NewSymbol("System`Null")
} else if sym.Name == "System`False" {
es.DebugOff()
return NewSymbol("System`Null")
}
}
return this
},
})
defs = append(defs, Definition{
Name: "ExpreduceDefinitionTimes",
Details: "For timing information to record, debug mode must be enabled through `ExpreduceSetLogging`.",
ExpreduceSpecific: true,
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if *mymemprofile != "" {
f, err := os.Create(*mymemprofile)
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
}
fmt.Println(es.timeCounter.String())
return NewSymbol("System`Null")
},
})
defs = append(defs, Definition{
Name: "Attributes",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
sym, isSym := this.Parts[1].(*Symbol)
if !isSym {
return this
}
def, isDef := es.defined[sym.Name]
if isDef {
return def.attributes.toSymList()
}
return NewExpression([]Ex{NewSymbol("System`List")})
},
})
defs = append(defs, Definition{
Name: "Default",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
sym, isSym := this.Parts[1].(*Symbol)
if !isSym {
return this
}
def := sym.Default(&es.defined)
if def != nil {
return def
}
return this
},
})
defs = append(defs, Definition{
Name: "Clear",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
for _, arg := range this.Parts[1:] {
es.Debugf("arg: %v", arg)
sym, isSym := arg.(*Symbol)
if isSym {
es.Clear(sym.Name)
}
}
return NewSymbol("System`Null")
},
})
defs = append(defs, Definition{
Name: "Definition",
toString: func(this *Expression, params ToStringParams) (bool, string) {
if len(this.Parts) != 2 {
return false, ""
}
if params.form != "InputForm" && params.form != "OutputForm" {
return false, ""
}
sym, ok := this.Parts[1].(*Symbol)
if !ok {
return false, ""
}
def, isd := params.es.defined[sym.Name]
if !isd {
return true, "Null"
}
stringParams := params
stringParams.context, stringParams.contextPath =
DefinitionComplexityStringFormArgs()
stringParams.previousHead = "<TOPLEVEL>"
return true, def.StringForm(sym, stringParams)
},
})
defs = append(defs, Definition{
Name: "DownValues",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
sym, ok := this.Parts[1].(*Symbol)
if !ok {
return this
}
res := NewExpression([]Ex{NewSymbol("System`List")})
def, isd := es.defined[sym.Name]
if !isd {
return res
}
for _, dv := range def.downvalues {
_, isLhsExpr := dv.rule.Parts[1].(*Expression).Parts[1].(*Expression)
if !isLhsExpr {
continue
}
res.Parts = append(res.Parts, NewExpression([]Ex{
NewSymbol("System`RuleDelayed"),
dv.rule.Parts[1],
dv.rule.Parts[2],
}))
}
return res
},
})
defs = append(defs, Definition{
Name: "Set",
Usage: "`lhs = rhs` sets `lhs` to stand for `rhs`.",
Attributes: []string{"HoldFirst", "SequenceHold"},
toString: func(this *Expression, params ToStringParams) (bool, string) {
if len(this.Parts) != 3 {
return false, ""
}
return ToStringInfixAdvanced(this.Parts[1:], " = ", "System`Set", false, "", "", params)
},
Bootstrap: true,
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 3 {
return this
}
es.Define(this.Parts[1], this.Parts[2])
return this.Parts[2]
},
SimpleExamples: []TestInstruction{
&StringTest{"3", "x=1+2"},
&StringTest{"3", "x"},
&StringTest{"4", "x+1"},
// To make sure the result does not change
&StringTest{"4", "x+1"},
&StringTest{"3", "x=1+2"},
&StringTest{"6", "x*2"},
// To make sure the result does not change
&StringTest{"6", "x=x*2"},
&StringTest{"36", "x=x*x"},
},
FurtherExamples: []TestInstruction{
&TestComment{"`Set` has the `HoldFirst` attribute, meaning `rhs` is evaluated before assignment:"},
&SameTest{"{HoldFirst, Protected, SequenceHold}", "Attributes[Set]"},
&TestComment{"`SetDelayed` has the `HoldAll` attribute, meaning `rhs` is not evaluated during assignment:"},
&SameTest{"{HoldAll, Protected, SequenceHold}", "Attributes[SetDelayed]"},
},
KnownFailures: []TestInstruction{
// Set up for the known failure:
&SameTest{"a^2", "y=a*a"},
&SameTest{"a^4", "y=y*y"},
&SameTest{"2", "a=2"},
// Known failure until we fix the re-evaluation issue.
&SameTest{"16", "y"},
&SameTest{"Null", "Clear[a]"},
},
})
defs = append(defs, Definition{
Name: "SetDelayed",
Usage: "`lhs := rhs` sets `lhs` to stand for `rhs`, with `rhs` not being evaluated until it is referenced by `lhs`.",
Attributes: []string{"HoldAll", "SequenceHold"},
toString: func(this *Expression, params ToStringParams) (bool, string) {
if len(this.Parts) != 3 {
return false, ""
}
return ToStringInfixAdvanced(this.Parts[1:], " := ", "System`SetDelayed", false, "", "", params)
},
Bootstrap: true,
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 3 {
return this
}
lhs, lhsIsExpr := this.Parts[1].(*Expression)
if lhsIsExpr {
for i := range lhs.Parts {
lhs.Parts[i] = lhs.Parts[i].Eval(es)
}
es.Define(lhs, this.Parts[2])
}
es.Define(this.Parts[1], this.Parts[2])
return NewSymbol("System`Null")
},
SimpleExamples: []TestInstruction{
&TestComment{"`SetDelayed` can be used to define functions:"},
&SameTest{"Null", "testa[x_] := x*2"},
&SameTest{"Null", "testa[x_Integer] := x*3"},
&SameTest{"Null", "testa[x_Real] := x*4"},
&TestComment{"The more \"specific\" definitions match first:"},
&SameTest{"8.", "testa[2.]"},
&SameTest{"6", "testa[2]"},
&TestComment{"There is no specific match for `testa[k]`, so the general case matches:"},
&SameTest{"2 * k", "testa[k]"},
},
FurtherExamples: []TestInstruction{
&TestComment{"`Set` has the `HoldFirst` attribute, meaning `rhs` is evaluated before assignment:"},
&SameTest{"{HoldFirst, Protected, SequenceHold}", "Attributes[Set]"},
&TestComment{"`SetDelayed` has the `HoldAll` attribute, meaning `rhs` is not evaluated during assignment:"},
&SameTest{"{HoldAll, Protected, SequenceHold}", "Attributes[SetDelayed]"},
},
Tests: []TestInstruction{
// Test function definitions
&SameTest{"Null", "testa[x_] := x*2"},
&SameTest{"Null", "testa[x_Integer] := x*3"},
&SameTest{"Null", "testa[x_Real] := x*4"},
&SameTest{"8.", "testa[2.]"},
&SameTest{"6", "testa[2]"},
&SameTest{"2 * k", "testa[k]"},
&SameTest{"Null", "testb[x_Real] := x*4"},
&SameTest{"Null", "testb[x_Integer] := x*3"},
&SameTest{"Null", "testb[x_] := x*2"},
&SameTest{"8.", "testb[2.]"},
&SameTest{"6", "testb[2]"},
&SameTest{"2 * k", "testb[k]"},
&SameTest{"testa", "testa"},
&SameTest{"testb", "testb"},
&SameTest{"Null", "testb[x_] := x*5"},
&SameTest{"5 * k", "testb[k]"},
&SameTest{"8.", "testb[2.]"},
&SameTest{"Null", "testb[x_Real + sym] := 5"},
&SameTest{"5", "testb[2.+sym]"},
&SameTest{"5", "testb[sym+2.]"},
&SameTest{"Null", "testb[x_Real + sym] := 6"},
&SameTest{"6", "testb[2.+sym]"},
&SameTest{"6", "testb[sym+2.]"},
&SameTest{"Null", "dist[x_, y_]:=(x^2 + y^2)^.5"},
&SameTest{"(j^2+k^2)^.5", "dist[j,k]"},
// Test pattern name conflicts.
&SameTest{"Null", "foo[k_, m_] := bar[k, m]"},
&SameTest{"bar[m, 2]", "foo[m, 2]"},
&SameTest{"Null", "fizz[m_, k_] := buzz[m, k]"},
&SameTest{"buzz[m, 2]", "fizz[m, 2]"},
},
})
defs = append(defs, Definition{
Name: "Timing",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
start := time.Now()
res := this.Parts[1].Eval(es)
elapsed := time.Since(start).Seconds()
return NewExpression([]Ex{NewSymbol("System`List"), NewReal(big.NewFloat(elapsed)), res})
},
})
defs = append(defs, Definition{
Name: "Print",
Usage: "`Print[expr1, expr2, ...]` prints the string representation of the expressions to the console and returns `Null`.",
Bootstrap: true,
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) < 2 {
return this
}
for i := 1; i < len(this.Parts); i++ {
fmt.Printf("%s", this.Parts[i].String())
}
fmt.Printf("\n")
return NewSymbol("System`Null")
},
})
defs = append(defs, Definition{
Name: "MessageName",
})
defs = append(defs, Definition{
Name: "Trace",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
// TODO: this will prevent nested Trace calls. Figure out a better
// way.
// Put system in trace mode:
es.trace = NewExpression([]Ex{NewSymbol("System`List")})
// Evaluate first argument in trace mode:
this.Parts[1].Eval(es)
if es.trace != nil && len(es.trace.Parts) > 2 {
// Take system out of trace mode:
toReturn := es.trace.DeepCopy()
es.trace = nil
return toReturn
}
es.trace = nil
return NewExpression([]Ex{NewSymbol("System`List")})
},
})
defs = append(defs, Definition{
Name: "N",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
return exprToN(es, this.Parts[1])
},
})
defs = append(defs, Definition{
Name: "Listable",
})
defs = append(defs, Definition{
Name: "ExpreduceFlatFn",
OmitDocumentation: true,
ExpreduceSpecific: true,
Attributes: []string{"Flat"},
})
defs = append(defs, Definition{
Name: "ExpreduceOrderlessFn",
OmitDocumentation: true,
ExpreduceSpecific: true,
Attributes: []string{"Orderless"},
})
defs = append(defs, Definition{
Name: "ExpreduceOneIdentityFn",
OmitDocumentation: true,
ExpreduceSpecific: true,
Attributes: []string{"OneIdentity"},
})
defs = append(defs, Definition{
Name: "ExpreduceFlatFn2",
OmitDocumentation: true,
ExpreduceSpecific: true,
Attributes: []string{"Flat"},
})
defs = append(defs, Definition{
Name: "ExpreduceFlOrFn",
OmitDocumentation: true,
ExpreduceSpecific: true,
Attributes: []string{"Flat", "Orderless"},
})
defs = append(defs, Definition{
Name: "ExpreduceFlOiFn",
OmitDocumentation: true,
ExpreduceSpecific: true,
Attributes: []string{"Flat", "OneIdentity"},
})
defs = append(defs, Definition{
Name: "ExpreduceFlOrOiFn",
OmitDocumentation: true,
ExpreduceSpecific: true,
Attributes: []string{"Flat", "Orderless", "OneIdentity"},
})
defs = append(defs, Definition{
Name: "ExpreduceLikePlus",
Default: "0",
OmitDocumentation: true,
ExpreduceSpecific: true,
Attributes: []string{"Flat", "Listable", "NumericFunction", "OneIdentity", "Orderless"},
})
defs = append(defs, Definition{
Name: "Get",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
fileData, rawPath, ok := TryReadFile(this.Parts[1], es)
if !ok {
return NewSymbol("System`$Failed")
}
return EvalInterpMany(fileData, rawPath, es)
},
})
defs = append(defs, Definition{
Name: "Module",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
res, ok := applyModuleFn(this, es)
if !ok {
return this
}
return res
},
})
defs = append(defs, Definition{
Name: "Block",
OmitDocumentation: true,
})
defs = append(defs, Definition{
Name: "ESameTest",
OmitDocumentation: true,
ExpreduceSpecific: true,
Bootstrap: true,
Attributes: []string{"HoldAll", "SequenceHold"},
})
defs = append(defs, Definition{
Name: "Hash",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
i := big.NewInt(0)
i.SetUint64(hashEx(this.Parts[1]))
return NewInteger(i)
},
})
defs = append(defs, Definition{
Name: "ReadList",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
fileData, rawPath, ok := TryReadFile(this.Parts[1], es)
if !ok {
return NewSymbol("System`$Failed")
}
return ReadList(fileData, rawPath, es)
},
})
defs = append(defs, Definition{Name: "BeginPackage"})
defs = append(defs, Definition{Name: "EndPackage"})
defs = append(defs, Definition{Name: "Begin"})
defs = append(defs, Definition{Name: "End"})
defs = append(defs, Definition{Name: "PrintTemporary"})
defs = append(defs, Definition{Name: "SetAttributes"})
defs = append(defs, Definition{Name: "ClearAttributes"})
defs = append(defs, Definition{Name: "Protect"})
defs = append(defs, Definition{Name: "Unprotect"})
defs = append(defs, Definition{Name: "ClearAll"})
defs = append(defs, Definition{
Name: "Quiet",
OmitDocumentation: true,
})
defs = append(defs, Definition{
Name: "TimeConstrained",
OmitDocumentation: true,
})
defs = append(defs, Definition{
Name: "Throw",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
es.Throw(this)
return this
},
})
defs = append(defs, Definition{
Name: "Catch",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
res := this.Parts[1].Eval(es)
if es.HasThrown() {
toReturn := es.thrown.Parts[1]
es.Throw(nil)
return toReturn
}
return res
},
})
defs = append(defs, Definition{
Name: "ExpreduceMaskNonConditional",
OmitDocumentation: true,
ExpreduceSpecific: true,
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) != 2 {
return this
}
return NewExpression([]Ex{
NewSymbol("System`Hold"),
maskNonConditional(this.Parts[1]),
})
},
})
defs = append(defs, Definition{
Name: "Unique",
legacyEvalFn: func(this *Expression, es *EvalState) Ex {
if len(this.Parts) > 3 {
return this
}
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))
// If I set this to nil, Int[((A + B*Cos[x])*(a + b*Sin[x])^-1), x]
// will not work for an unknown reason.
es.reapSown = E(S("List"))
return res
},
})
defs = append(defs, Definition{
Name: "Defer",
OmitDocumentation: true,
})
return
}