REPL rewrite

Obsoletes #2669 and #2600. Ping @jashkenas for final approval.

Should we squash some of these commits or just keep them all separate? We still have the somewhat-hacky replacement of newlines with a reserved unicode character when in multiline mode. I don't know a better solution, and it's not terrible as is. We could also use a few more tests, but that can wait. It's already in a better state than the current REPL.