Skip to content

Honor the -w flag in REPL mode.#148

Open
csilvers wants to merge 1 commit intocosmos72:masterfrom
Khan:repl-writes-decls
Open

Honor the -w flag in REPL mode.#148
csilvers wants to merge 1 commit intocosmos72:masterfrom
Khan:repl-writes-decls

Conversation

@csilvers
Copy link
Copy Markdown

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
Copy Markdown
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.


var set, clear Options
var repl, forcerepl = true, false
repl, forcerepl := true, false
Copy link
Copy Markdown
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.

1 participant