Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honor the -w flag in REPL mode. #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 26 additions & 9 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io/ioutil"
"os"
"strings"
"time"

. "github.com/cosmos72/gomacro/base"
"github.com/cosmos72/gomacro/base/genimport"
Expand Down Expand Up @@ -69,7 +70,7 @@ func (cmd *Cmd) Main(args []string) (err error) {
g := &ir.Comp.Globals

var set, clear Options
var repl, forcerepl = true, false
repl, forcerepl := true, false
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this was my editor auto-fixing things. I can revert if you have strong feelings.

cmd.WriteDeclsAndStmts = false
cmd.OverwriteFiles = false

Expand Down Expand Up @@ -147,9 +148,20 @@ func (cmd *Cmd) Main(args []string) (err error) {
args = args[1:]
}
if repl || forcerepl {
if cmd.WriteDeclsAndStmts {
g.Options |= OptCollectDeclarations | OptCollectStatements
}
g.Options |= OptShowPrompt | OptShowEval | OptShowEvalType // set by default, overridden by -s, -v and -vv
g.Options = (g.Options | set) &^ clear
ir.ReplStdin()

if cmd.WriteDeclsAndStmts {
cmd.writeDeclAndStatements(
"repl.go",
fmt.Sprintf("// REPL run: %v\n", time.Now().Format(time.RFC3339)),
)
}

}
return nil
}
Expand Down Expand Up @@ -238,6 +250,18 @@ const disclaimer = `// ---------------------------------------------------------

`

func (cmd *Cmd) writeDeclAndStatements(outname string, comments string) {
g := &cmd.Interp.Comp.Globals
if !cmd.OverwriteFiles {
_, err := os.Stat(outname)
if err == nil {
g.Warnf("file exists already, use -f to force overwriting: %v", outname)
return
}
}
g.WriteDeclsToFile(outname, disclaimer, comments)
}

func (cmd *Cmd) EvalFile(filename string) error {
g := &cmd.Interp.Comp.Globals
g.Declarations = nil
Expand All @@ -257,14 +281,7 @@ func (cmd *Cmd) EvalFile(filename string) error {
}
}
outname += ".go"
if !cmd.OverwriteFiles {
_, err := os.Stat(outname)
if err == nil {
g.Warnf("file exists already, use -f to force overwriting: %v", outname)
return nil
}
}
g.WriteDeclsToFile(outname, disclaimer, comments)
cmd.writeDeclAndStatements(outname, comments)

if g.Options&OptShowEval != 0 {
fmt.Fprintf(g.Stdout, "// processed file: %v\t-> %v\n", filename, outname)
Expand Down