Skip to content

Commit d13383c

Browse files
committed
vm: make PrintExpr hook for steering the output of PRINT_EXPR in the REPL
1 parent d05bbcc commit d13383c

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

vm/eval.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ objects so they can be GCed
2222

2323
import (
2424
"fmt"
25+
"os"
2526
"runtime/debug"
2627
"strings"
2728

@@ -444,6 +445,12 @@ func do_DELETE_SUBSCR(vm *Vm, arg int32) error {
444445

445446
// Miscellaneous opcodes.
446447

448+
// PrintExpr controls where the output of PRINT_EXPR goes which is
449+
// used in the REPL
450+
var PrintExpr = func(out string) {
451+
_, _ = os.Stdout.WriteString(out + "\n")
452+
}
453+
447454
// Implements the expression statement for the interactive mode. TOS
448455
// is removed from the stack and printed. In non-interactive mode, an
449456
// expression statement is terminated with POP_STACK.
@@ -460,7 +467,7 @@ func do_PRINT_EXPR(vm *Vm, arg int32) error {
460467
if err != nil {
461468
return err
462469
}
463-
fmt.Printf("%s\n", repr)
470+
PrintExpr(fmt.Sprint(repr))
464471
}
465472
vm.frame.Globals["_"] = value
466473
return nil

0 commit comments

Comments
 (0)