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