Skip to content

Commit

Permalink
add REPL command :unload PACKAGE-PATH. fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmos72 committed Apr 9, 2018
1 parent 80a07f7 commit 568bd2e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
22 changes: 22 additions & 0 deletions base/global.go
Expand Up @@ -34,6 +34,7 @@ import (
r "reflect"
"strings"

"github.com/cosmos72/gomacro/imports"
mp "github.com/cosmos72/gomacro/parser"
mt "github.com/cosmos72/gomacro/token"

Expand Down Expand Up @@ -140,6 +141,27 @@ func (g *Globals) ParseBytes(src []byte) []ast.Node {
return nodes
}

func (g *Globals) UnloadImport(path string) {
if n := len(path); n > 1 && path[0] == '"' && path[n-1] == '"' {
path = path[1 : n-1] // remove quotes
}
slash := strings.IndexByte(path, '/')
if _, found := imports.Packages[path]; !found {
if slash < 0 {
g.Debugf("nothing to unload: cannot find imported package %q. Remember to specify the full package path, not only its name", path)
} else {
g.Debugf("nothing to unload: cannot find imported package %q", path)
}
}
delete(imports.Packages, path)
dot := strings.IndexByte(path, '.')
if slash < 0 || dot > slash {
g.Warnf("unloaded standard library package %q. attempts to import it again may trigger a reload", path)
return
}
g.Debugf("unloaded package %q. attempts to import it again may trigger a reload", path)
}

// CollectAst accumulates declarations in ir.Decls and statements in ir.Stmts
// allows generating a *.go file on user request
func (g *Globals) CollectAst(form Ast) {
Expand Down
5 changes: 5 additions & 0 deletions classic/interpreter.go
Expand Up @@ -211,6 +211,11 @@ func (ir *Interp) parseEvalPrint(src string, in Readline) (callAgain bool) {
return true
case strings.HasPrefix(":quit", cmd):
return false
case strings.HasPrefix(":unload", cmd):
if len(args) > 1 {
g.UnloadImport(args[1])
}
return true
case strings.HasPrefix(":write", cmd):
if len(args) <= 1 {
env.WriteDeclsToStream(env.Stdout)
Expand Down
5 changes: 3 additions & 2 deletions classic/output.go
Expand Up @@ -45,13 +45,14 @@ func (ir *ThreadGlobals) ShowHelp(out io.Writer) {
fmt.Fprint(out, `// type Go code to execute it. example: func add(x, y int) int { return x + y }
// interpreter commands:
:classic CODE execute CODE using the classic interpreter
:classic [CODE] execute CODE using the classic interpreter
:env [name] show available functions, variables and constants
in current package, or from imported package "name"
:fast CODE execute CODE using the fast interpreter (default)
:fast [CODE] execute CODE using the fast interpreter (default)
:help show this help
:inspect EXPR inspect expression interactively
:options [OPTS] show or toggle interpreter options
:unload PACKAGE unload PACKAGE. Later attempts to import it may still reload it.
:quit quit the interpreter
:write [FILE] write collected declarations and/or statements to standard output or to FILE
use :o Declarations and/or :o Statements to start collecting them
Expand Down

0 comments on commit 568bd2e

Please sign in to comment.