Skip to content

Commit bcb3785

Browse files
committed
Fix comments in REPL - fixes #78
Before this change, entering a comment in the REPL caused the REPL to read the comment indefinitely effectively breaking it. After this change the behaviour should be exactly the same as python3/
1 parent 7bc4005 commit bcb3785

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

repl/repl.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ func (r *REPL) Run(line string) {
8282
// FIXME detect EOF properly!
8383
errText := err.Error()
8484
if strings.Contains(errText, "unexpected EOF while parsing") || strings.Contains(errText, "EOF while scanning triple-quoted string literal") {
85-
r.continuation = true
86-
r.previous += string(line) + "\n"
85+
stripped := strings.TrimSpace(toCompile)
86+
isComment := len(stripped) > 0 && stripped[0] == '#'
87+
if !isComment {
88+
r.continuation = true
89+
r.previous += string(line) + "\n"
90+
}
8791
r.term.SetPrompt(ContinuationPrompt)
8892
return
8993
}

repl/repl_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ func TestREPL(t *testing.T) {
6767

6868
r.Run("if")
6969
rt.assert(t, "compileError", NormalPrompt, "Compile error: \n File \"<string>\", line 1, offset 2\n if\n\n\nSyntaxError: 'invalid syntax'")
70+
71+
// test comments in the REPL work properly
72+
r.Run("# this is a comment")
73+
rt.assert(t, "comment", ContinuationPrompt, "")
74+
r.Run("a = 42")
75+
rt.assert(t, "comment continuation", NormalPrompt, "")
76+
r.Run("a")
77+
rt.assert(t, "comment check", NormalPrompt, "42")
7078
}
7179

7280
func TestCompleter(t *testing.T) {

0 commit comments

Comments
 (0)