Make CodeObject.source_path an Option<String>

This commit is contained in:
coolreader18 2019-06-15 13:20:16 -05:00
parent 8e3fdd7b36
commit d7b4837e26
2 changed files with 19 additions and 12 deletions

View file

@ -21,7 +21,7 @@ pub struct CodeObject {
pub varargs: Varargs, // *args or *
pub kwonlyarg_names: Vec<String>,
pub varkeywords: Varargs, // **kwargs or **
pub source_path: String,
pub source_path: Option<String>,
pub first_line_number: usize,
pub obj_name: String, // Name of the object that created this code object
pub is_generator: bool,
@ -269,7 +269,7 @@ impl CodeObject {
varargs: Varargs,
kwonlyarg_names: Vec<String>,
varkeywords: Varargs,
source_path: String,
source_path: Option<String>,
first_line_number: usize,
obj_name: String,
) -> CodeObject {

View file

@ -23,7 +23,11 @@ struct Compiler {
}
/// Compile a given sourcecode into a bytecode object.
pub fn compile(source: &str, mode: &Mode, source_path: String) -> Result<CodeObject, CompileError> {
pub fn compile(
source: &str,
mode: &Mode,
source_path: Option<String>,
) -> Result<CodeObject, CompileError> {
match mode {
Mode::Exec => {
let ast = parser::parse_program(source)?;
@ -42,11 +46,11 @@ pub fn compile(source: &str, mode: &Mode, source_path: String) -> Result<CodeObj
/// A helper function for the shared code of the different compile functions
fn with_compiler(
source_path: String,
source_path: Option<String>,
f: impl FnOnce(&mut Compiler) -> Result<(), CompileError>,
) -> Result<CodeObject, CompileError> {
let mut compiler = Compiler::new();
compiler.source_path = Some(source_path);
compiler.source_path = source_path;
compiler.push_new_code_object("<module>".to_string());
f(&mut compiler)?;
let code = compiler.pop_code_object();
@ -55,7 +59,10 @@ fn with_compiler(
}
/// Compile a standard Python program to bytecode
pub fn compile_program(ast: ast::Program, source_path: String) -> Result<CodeObject, CompileError> {
pub fn compile_program(
ast: ast::Program,
source_path: Option<String>,
) -> Result<CodeObject, CompileError> {
with_compiler(source_path, |compiler| {
let symbol_table = make_symbol_table(&ast)?;
compiler.compile_program(&ast, symbol_table)
@ -65,7 +72,7 @@ pub fn compile_program(ast: ast::Program, source_path: String) -> Result<CodeObj
/// Compile a single Python expression to bytecode
pub fn compile_statement_eval(
statement: Vec<ast::LocatedStatement>,
source_path: String,
source_path: Option<String>,
) -> Result<CodeObject, CompileError> {
with_compiler(source_path, |compiler| {
let symbol_table = statements_to_symbol_table(&statement)?;
@ -76,7 +83,7 @@ pub fn compile_statement_eval(
/// Compile a Python program to bytecode for the context of a REPL
pub fn compile_program_single(
ast: ast::Program,
source_path: String,
source_path: Option<String>,
) -> Result<CodeObject, CompileError> {
with_compiler(source_path, |compiler| {
let symbol_table = make_symbol_table(&ast)?;
@ -119,7 +126,7 @@ impl Compiler {
Varargs::None,
Vec::new(),
Varargs::None,
self.source_path.clone().unwrap(),
self.source_path.clone(),
line_number,
obj_name,
));
@ -596,7 +603,7 @@ impl Compiler {
Varargs::from(&args.vararg),
args.kwonlyargs.iter().map(|a| a.arg.clone()).collect(),
Varargs::from(&args.kwarg),
self.source_path.clone().unwrap(),
self.source_path.clone(),
line_number,
name.to_string(),
));
@ -849,7 +856,7 @@ impl Compiler {
Varargs::None,
vec![],
Varargs::None,
self.source_path.clone().unwrap(),
self.source_path.clone(),
line_number,
name.to_string(),
));
@ -1571,7 +1578,7 @@ impl Compiler {
Varargs::None,
vec![],
Varargs::None,
self.source_path.clone().unwrap(),
self.source_path.clone(),
line_number,
name.clone(),
));