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

Conversation

csilvers
Copy link

Right now, -w (write-decls-and-statements) is only used when gomacro is called with file arguments. But it might be useful to save a record of what was run for interactive mode as well. For instance, you might want to play around with some code interactively, save the results, and then use that as the basis of writing a script.

This PR makes it so -w is respected when run as a REPL. In the existing (file-based) usage, the output of -w is based on the filename. For the new, REPL case there's nothing to base the ouptut filename on, so I just hard-code a name.

Test plan:
I ran the following:

gomacro% go run . -w
// Welcome to gomacro. Type :help for help, :copy for copyright and license.
// This is free software with ABSOLUTELY NO WARRANTY.
gomacro> func() { return 5; }
repl.go:1:8: expected 'IDENT', found '{'
gomacro> func a() { return 5; }
return: expecting 0 expressions, found 1: return 5
gomacro> a := func() int { return 5 }
gomacro> :exit

gomacro% cat repl.go
// -------------------------------------------------------------
// DO NOT EDIT! this file was generated automatically by gomacro
// Any change will be lost when the file is re-generated
// -------------------------------------------------------------

// REPL run: 2023-09-21T13:42:27-07:00
package main

func a() { return 5 }
var a = func() { return 5 }
var a = func() int { return 5 }

which shows that the declarations are being saved and emitted as expected.

Right now, `-w` (write-decls-and-statements) is only used when gomacro
is called with file arguments.  But it might be useful to save a
record of what was run for interactive mode as well.  For instance,
you might want to play around with some code interactively, save the
results, and then use that as the basis of writing a script.

This PR makes it so `-w` is respected when run as a REPL.  In the
existing (file-based) usage, the output of `-w` is based on the
filename.  For the new, REPL case there's nothing to base the ouptut
filename on, so I just hard-code a name.

Test plan:
I ran the following:
```
gomacro% go run . -w
// Welcome to gomacro. Type :help for help, :copy for copyright and license.
// This is free software with ABSOLUTELY NO WARRANTY.
gomacro> func() { return 5; }
repl.go:1:8: expected 'IDENT', found '{'
gomacro> func a() { return 5; }
return: expecting 0 expressions, found 1: return 5
gomacro> a := func() int { return 5 }
gomacro> :exit

gomacro% cat repl.go
// -------------------------------------------------------------
// DO NOT EDIT! this file was generated automatically by gomacro
// Any change will be lost when the file is re-generated
// -------------------------------------------------------------

// REPL run: 2023-09-21T13:42:27-07:00
package main

func a() { return 5 }
var a = func() { return 5 }
var a = func() int { return 5 }
```
which shows that the declarations are being saved and emitted as
expected.
@csilvers
Copy link
Author

For the new, REPL case there's nothing to base the ouptut filename on, so I just hard-code a name.

I didn't know what the best behavior here might be -- I'm happy to do here whatever you think is best.

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant