Skip to content

Commit

Permalink
Merge pull request #150 from darvin/feature/batch_script_execution
Browse files Browse the repository at this point in the history
Feature/batch script execution
  • Loading branch information
corywalker committed Jun 3, 2018
2 parents 340cfab + 44ba434 commit fd5082e
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 137 deletions.
1 change: 1 addition & 0 deletions examples/example.m
Expand Up @@ -5,3 +5,4 @@
Together[(1/2 + 3/a)^2+b/c]
40!
Solve[x^2-x-2.5==0,x]
Print["3+2 = ? (thats from Print[]): ", 3+2]
121 changes: 79 additions & 42 deletions expreduce.go
Expand Up @@ -7,6 +7,7 @@ import (
"gopkg.in/readline.v1"
"log"
"os"
"bytes"
"runtime/pprof"
"net/http"
_ "net/http/pprof"
Expand All @@ -15,10 +16,15 @@ import (
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/")
var scriptfile = flag.String("script", "", "script `file` to read from")




func main() {
flag.Parse()
if *cpuprofile != "" {

if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
Expand All @@ -30,6 +36,39 @@ func main() {
go http.ListenAndServe(":8080", nil)
}

es := expreduce.NewEvalState()
if *debug {
es.NoInit = true
es.ClearAll()
}

if *scriptfile != "" {
f, err := os.Open(*scriptfile)
if err != nil {
log.Fatal(err)
}
defer f.Close()

buf := new(bytes.Buffer)
buf.ReadFrom(f)
scriptText := buf.String()
scriptSession(es, scriptText, *scriptfile)
} else {
interactiveSession(es)
}
}


func scriptSession(es *expreduce.EvalState, srcText string, srcPath string) {
exp := expreduce.EvalInterpMany(srcText, srcPath, es)
res := exp.Eval(es)
res = es.ProcessTopLevelResult(res, res)


}


func interactiveSession(es *expreduce.EvalState) {
rl, err := readline.NewEx(&readline.Config{
HistoryFile: "/tmp/readline.tmp",
ForceUseInteractive: true,
Expand All @@ -39,12 +78,6 @@ func main() {
}
defer rl.Close()

es := expreduce.NewEvalState()
if *debug {
es.NoInit = true
es.ClearAll()
}

fmt.Printf("Welcome to Expreduce!\n\n")
promptNum := 1
for {
Expand All @@ -62,45 +95,49 @@ func main() {
res := exp.Eval(es)
res = es.ProcessTopLevelResult(exp, res)

isNull := false
asSym, isSym := res.(*expreduce.Symbol)
if isSym {
if asSym.Name == "System`Null" {
isNull = true
}
printFormattedOutput(es, res, true, promptNum)
promptNum += 1
}
}


func printFormattedOutput(es *expreduce.EvalState, res expreduce.Ex, isInteractive bool, promptNum int) {
isNull := false
asSym, isSym := res.(*expreduce.Symbol)
if isSym {
if asSym.Name == "System`Null" {
isNull = true
}
}

if !isNull {
// Print formatted result
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(
expreduce.ActualStringFormArgsFull(specialForm[7:], es)),
)
wasSpecialForm = true
if !isNull {
// Print formatted result
specialForms := []string{
"System`FullForm",
"System`OutputForm",
}
wasSpecialForm := false
for _, specialForm := range specialForms {
asSpecialForm, isSpecialForm := expreduce.HeadAssertion(
res, specialForm)
if !isSpecialForm {
continue
}
if !wasSpecialForm {
fmt.Printf("Out[%d]= %s\n\n", promptNum, res.StringForm(
expreduce.ActualStringFormArgsFull("InputForm", es)))
if len(asSpecialForm.Parts) != 2 {
continue
}
fmt.Printf(
"Out[%d]//%s= %s\n\n",
promptNum,
specialForm[7:],
asSpecialForm.Parts[1].StringForm(
expreduce.ActualStringFormArgsFull(specialForm[7:], es)),
)
wasSpecialForm = true
}
if !wasSpecialForm {
fmt.Printf("Out[%d]= %s\n\n", promptNum, res.StringForm(
expreduce.ActualStringFormArgsFull("InputForm", es)))
}

promptNum += 1
}
}

0 comments on commit fd5082e

Please sign in to comment.