From 88d6926f786874060b378110329181817572c157 Mon Sep 17 00:00:00 2001 From: coolreader18 <33094578+coolreader18@users.noreply.github.com> Date: Thu, 21 Mar 2019 20:25:16 -0500 Subject: [PATCH 1/3] Make the REPL handle continuation better --- src/main.rs | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1b92a5ee46..a7dffb02d7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,6 +159,14 @@ fn get_history_path() -> PathBuf { xdg_dirs.place_cache_file("repl_history.txt").unwrap() } +fn get_prompt(vm: &mut VirtualMachine, prompt_name: &str) -> String { + vm.sys_module + .get_attr(prompt_name) + .as_ref() + .map(objstr::get_value) + .unwrap_or_else(String::new) +} + fn run_shell(vm: &mut VirtualMachine) -> PyResult { println!( "Welcome to the magnificent Rust Python {} interpreter", @@ -176,25 +184,36 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult { println!("No previous history."); } - let ps1 = &objstr::get_value(&vm.sys_module.get_attr("ps1").unwrap()); - let ps2 = &objstr::get_value(&vm.sys_module.get_attr("ps2").unwrap()); - let mut prompt = ps1; + let mut prompt = get_prompt(vm, "ps1"); + + let mut continuing = false; loop { - match repl.readline(prompt) { + if !continuing { + prompt = get_prompt(vm, "ps1"); + } + match repl.readline(&prompt) { Ok(line) => { debug!("You entered {:?}", line); input.push_str(&line); - input.push_str("\n"); + input.push('\n'); repl.add_history_entry(line.trim_end()); + if continuing { + if line.is_empty() { + continuing = false; + } else { + continue; + } + } + match shell_exec(vm, &input, vars.clone()) { Err(CompileError::Parse(ParseError::EOF(_))) => { - prompt = ps2; + prompt = get_prompt(vm, "ps2"); + continuing = true; continue; } _ => { - prompt = ps1; input = String::new(); } } @@ -202,7 +221,8 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult { Err(ReadlineError::Interrupted) => { // TODO: Raise a real KeyboardInterrupt exception println!("^C"); - break; + continuing = false; + continue; } Err(ReadlineError::Eof) => { break; From 2c93c79ac8c127b9390520f07095759476b36417 Mon Sep 17 00:00:00 2001 From: coolreader18 <33094578+coolreader18@users.noreply.github.com> Date: Fri, 22 Mar 2019 01:53:55 -0500 Subject: [PATCH 2/3] Immutable prompt variable --- src/main.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index a7dffb02d7..87ed80868e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -184,14 +184,14 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult { println!("No previous history."); } - let mut prompt = get_prompt(vm, "ps1"); - let mut continuing = false; loop { - if !continuing { - prompt = get_prompt(vm, "ps1"); - } + let prompt = if continuing { + get_prompt(vm, "ps2") + } else { + get_prompt(vm, "ps1") + }; match repl.readline(&prompt) { Ok(line) => { debug!("You entered {:?}", line); @@ -209,7 +209,6 @@ fn run_shell(vm: &mut VirtualMachine) -> PyResult { match shell_exec(vm, &input, vars.clone()) { Err(CompileError::Parse(ParseError::EOF(_))) => { - prompt = get_prompt(vm, "ps2"); continuing = true; continue; } From 36ff4e37d360fe8dad35409b25e65aef34fe53d2 Mon Sep 17 00:00:00 2001 From: coolreader18 <33094578+coolreader18@users.noreply.github.com> Date: Fri, 22 Mar 2019 07:29:14 -0500 Subject: [PATCH 3/3] Use vm.get_attribute --- src/main.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index 87ed80868e..4393946273 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,14 +10,8 @@ extern crate rustyline; use clap::{App, Arg}; use rustpython_parser::error::ParseError; use rustpython_vm::{ - compile, - error::CompileError, - frame::Scope, - import, - obj::objstr, - print_exception, - pyobject::{AttributeProtocol, PyResult}, - util, VirtualMachine, + compile, error::CompileError, frame::Scope, import, obj::objstr, print_exception, + pyobject::PyResult, util, VirtualMachine, }; use rustyline::{error::ReadlineError, Editor}; use std::path::{Path, PathBuf}; @@ -160,8 +154,8 @@ fn get_history_path() -> PathBuf { } fn get_prompt(vm: &mut VirtualMachine, prompt_name: &str) -> String { - vm.sys_module - .get_attr(prompt_name) + vm.get_attribute(vm.sys_module.clone(), prompt_name) + .ok() .as_ref() .map(objstr::get_value) .unwrap_or_else(String::new)