Add :resetblocks / :rb to REPL (#920)

This commit is contained in:
John Kerl 2022-02-04 09:53:18 -05:00 committed by GitHub
parent fe8d65fe1a
commit aa960040d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 5 deletions

View file

@ -54,6 +54,7 @@ func init() {
{verbNames: []string{":e", ":end"}, handlerFunc: handleEnd, usageFunc: usageEnd},
{verbNames: []string{":astprint"}, handlerFunc: handleASTPrint, usageFunc: usageASTPrint},
{verbNames: []string{":blocks"}, handlerFunc: handleBlocks, usageFunc: usageBlocks},
{verbNames: []string{":rb", ":resetblocks"}, handlerFunc: handleResetBlocks, usageFunc: usageResetBlocks},
{verbNames: []string{":q", ":quit"}, handlerFunc: nil, usageFunc: usageQuit},
{verbNames: []string{":h", ":help"}, handlerFunc: handleHelp, usageFunc: usageHelp},
}
@ -108,11 +109,16 @@ func (repl *Repl) handleNonDSLLine(trimmedLine string) bool {
// TODO: describe me
if strings.HasPrefix(verbName, "??") {
handleHelpFindSingle(repl, verbName[2:])
return true
}
if strings.HasPrefix(verbName, "?") {
handleHelpSingle(repl, verbName[1:])
if verbName[2:] != "" {
handleHelpFindSingle(repl, verbName[2:])
return true
}
} else if strings.HasPrefix(verbName, "?") {
if verbName[1:] != "" {
handleHelpSingle(repl, verbName[1:])
} else {
usageHelp(repl)
}
return true
}
@ -818,6 +824,21 @@ func handleBlocks(repl *Repl, args []string) bool {
return true
}
// ----------------------------------------------------------------
func usageResetBlocks(repl *Repl) {
fmt.Println(":resetblocks with no arguments.")
fmt.Println("Clears out all begin, main, and end blocks that have been loaded.")
}
func handleResetBlocks(repl *Repl, args []string) bool {
args = args[1:] // strip off verb
if len(args) != 0 {
return false
}
repl.cstRootNode.ResetBlocksForREPL()
return true
}
// ----------------------------------------------------------------
func usageQuit(repl *Repl) {
fmt.Println(":quit with no arguments.")

View file

@ -501,3 +501,10 @@ func (root *RootNode) ShowBlockReport() {
fmt.Printf("#main %d\n", len(root.mainBlock.executables))
fmt.Printf("#end %d\n", len(root.endBlocks))
}
// This is for the REPL's resetblocks command.
func (root *RootNode) ResetBlocksForREPL() {
root.beginBlocks = make([]*StatementBlockNode, 0)
root.mainBlock.executables = make([]IExecutable, 0)
root.endBlocks = make([]*StatementBlockNode, 0)
}