/
expreduce.go
107 lines (97 loc) · 2.2 KB
/
expreduce.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
package main
import (
"flag"
"fmt"
"github.com/corywalker/expreduce/expreduce"
"gopkg.in/readline.v1"
"log"
"os"
"runtime/pprof"
"net/http"
_ "net/http/pprof"
)
var debug = flag.Bool("debug", false, "Debug mode. No initial definitions.")
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var netprofile = flag.Bool("netprofile", false, "Enable live profiling at http://localhost:8080/debug/pprof/")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if *netprofile {
go http.ListenAndServe(":8080", nil)
}
rl, err := readline.NewEx(&readline.Config{
HistoryFile: "/tmp/readline.tmp",
ForceUseInteractive: true,
})
if err != nil {
panic(err)
}
defer rl.Close()
es := expreduce.NewEvalState()
if *debug {
es.NoInit = true
es.ClearAll()
}
fmt.Printf("Welcome to Expreduce!\n\n")
promptNum := 1
for {
rl.SetPrompt(fmt.Sprintf("In[%d]:= ", promptNum))
line, err := rl.Readline()
if err != nil { // io.EOF, readline.ErrInterrupt
break
}
if line == "" {
continue
}
fmt.Printf("\n")
exp := expreduce.Interp(line, es)
res := exp.Eval(es)
res = es.ProcessTopLevelResult(res)
isNull := false
asSym, isSym := res.(*expreduce.Symbol)
if isSym {
if asSym.Name == "System`Null" {
isNull = true
}
}
if !isNull {
// Print formatted result
context, contextPath := expreduce.ActualStringFormArgs(es)
specialForms := []string{
"System`FullForm",
"System`OutputForm",
}
wasSpecialForm := false
for _, specialForm := range specialForms {
asSpecialForm, isSpecialForm := expreduce.HeadAssertion(
res, specialForm)
if !isSpecialForm {
continue
}
if len(asSpecialForm.Parts) != 2 {
continue
}
fmt.Printf(
"Out[%d]//%s= %s\n\n",
promptNum,
specialForm[7:],
asSpecialForm.Parts[1].StringForm(
specialForm[7:], context, contextPath),
)
wasSpecialForm = true
}
if !wasSpecialForm {
fmt.Printf("Out[%d]= %s\n\n", promptNum, res.StringForm(
"InputForm", context, contextPath))
}
}
promptNum += 1
}
}