diff --git a/src/bytecode.rs b/src/bytecode.rs index c87bc0a..b5b3c11 100644 --- a/src/bytecode.rs +++ b/src/bytecode.rs @@ -21,7 +21,7 @@ pub struct CodeObject { pub varargs: Varargs, // *args or * pub kwonlyarg_names: Vec, pub varkeywords: Varargs, // **kwargs or ** - pub source_path: String, + pub source_path: Option, 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, varkeywords: Varargs, - source_path: String, + source_path: Option, first_line_number: usize, obj_name: String, ) -> CodeObject { diff --git a/src/compile.rs b/src/compile.rs index 8b806b1..0efa3ad 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -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 { +pub fn compile( + source: &str, + mode: &Mode, + source_path: Option, +) -> Result { 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, f: impl FnOnce(&mut Compiler) -> Result<(), CompileError>, ) -> Result { let mut compiler = Compiler::new(); - compiler.source_path = Some(source_path); + compiler.source_path = source_path; compiler.push_new_code_object("".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 { +pub fn compile_program( + ast: ast::Program, + source_path: Option, +) -> Result { 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, - source_path: String, + source_path: Option, ) -> Result { 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, ) -> Result { 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(), ));