Skip to content

Commit d9667bc

Browse files
committed
repl: implement multi line code input with continuation lines
1 parent e352670 commit d9667bc

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

repl/repl.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,14 @@ func Run() {
136136
fmt.Printf("Gpython 3.4.0\n")
137137
prog := "<stdin>"
138138
module.Globals["__file__"] = py.String(prog)
139+
continuation := false
140+
previous := ""
139141
for {
140-
line, err := rl.Prompt(">>> ")
142+
prompt := ">>> "
143+
if continuation {
144+
prompt = "... "
145+
}
146+
line, err := rl.Prompt(prompt)
141147
if err != nil {
142148
if err == io.EOF {
143149
fmt.Printf("\n")
@@ -146,9 +152,30 @@ func Run() {
146152
fmt.Printf("Problem reading line: %v\n", err)
147153
continue
148154
}
149-
rl.AppendHistory(line)
150-
// FIXME need +"\n" because "single" is broken
151-
obj, err := compile.Compile(string(line)+"\n", prog, "single", 0, true)
155+
if line != "" {
156+
rl.AppendHistory(line)
157+
}
158+
if continuation {
159+
if line != "" {
160+
previous += string(line) + "\n"
161+
continue
162+
}
163+
164+
}
165+
// need +"\n" because "single" expects \n terminated input
166+
obj, err := compile.Compile(previous+string(line)+"\n", prog, "single", 0, true)
167+
if err != nil {
168+
// Detect that we should start a continuation line
169+
// FIXME detect EOF properly!
170+
errText := err.Error()
171+
if strings.Contains(errText, "unexpected EOF while parsing") {
172+
continuation = true
173+
previous = string(line) + "\n"
174+
continue
175+
}
176+
}
177+
continuation = false
178+
previous = ""
152179
if err != nil {
153180
fmt.Printf("Compile error: %v\n", err)
154181
continue

0 commit comments

Comments
 (0)