diff --git a/src/compile.rs b/src/compile.rs index 8c8438a..efb5e08 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -552,6 +552,7 @@ impl Compiler { Import { names } => { // import a, b, c as d for name in names { + let name = &name.node; self.emit_constant(ConstantData::Integer { value: num_traits::Zero::zero(), }); @@ -574,7 +575,7 @@ impl Compiler { module, names, } => { - let import_star = names.iter().any(|n| n.name == "*"); + let import_star = names.iter().any(|n| n.node.name == "*"); let from_list = if import_star { if self.ctx.in_func() { @@ -588,7 +589,7 @@ impl Compiler { names .iter() .map(|n| ConstantData::Str { - value: n.name.to_owned(), + value: n.node.name.to_owned(), }) .collect() }; @@ -597,7 +598,7 @@ impl Compiler { // from .... import (*fromlist) self.emit_constant(ConstantData::Integer { - value: (*level).into(), + value: (*level).unwrap_or(0).into(), }); self.emit_constant(ConstantData::Tuple { elements: from_list, @@ -615,6 +616,7 @@ impl Compiler { // from mod import a, b as c for name in names { + let name = &name.node; let idx = self.name(&name.name); // import symbol from module: self.emit(Instruction::ImportFrom { idx }); @@ -881,13 +883,11 @@ impl Compiler { let mut num_kw_only_defaults = 0; for (kw, default) in args.kwonlyargs.iter().zip(&args.kw_defaults) { - if let Some(default) = default { - self.emit_constant(ConstantData::Str { - value: kw.node.arg.clone(), - }); - self.compile_expression(default)?; - num_kw_only_defaults += 1; - } + self.emit_constant(ConstantData::Str { + value: kw.node.arg.clone(), + }); + self.compile_expression(default)?; + num_kw_only_defaults += 1; } if num_kw_only_defaults > 0 { self.emit(Instruction::BuildMap { @@ -1930,49 +1930,28 @@ impl Compiler { fn compile_dict( &mut self, - keys: &[Option>], + keys: &[ast::Expr], values: &[ast::Expr], ) -> CompileResult<()> { let mut size = 0; - let mut has_unpacking = false; - for (is_unpacking, subpairs) in &keys.iter().zip(values).group_by(|e| e.0.is_none()) { - if is_unpacking { - for (_, value) in subpairs { - self.compile_expression(value)?; - size += 1; - } - has_unpacking = true; - } else { - let mut subsize = 0; - for (key, value) in subpairs { - if let Some(key) = key { - self.compile_expression(key)?; - self.compile_expression(value)?; - subsize += 1; - } - } - self.emit(Instruction::BuildMap { - size: subsize, - unpack: false, - for_call: false, - }); - size += 1; - } + + let (packed_values, unpacked_values) = values.split_at(keys.len()); + for (key, value) in keys.iter().zip(packed_values) { + self.compile_expression(key)?; + self.compile_expression(value)?; + size += 1; } - if size == 0 { - self.emit(Instruction::BuildMap { - size, - unpack: false, - for_call: false, - }); - } - if size > 1 || has_unpacking { - self.emit(Instruction::BuildMap { - size, - unpack: true, - for_call: false, - }); + self.emit(Instruction::BuildMap { + size, + unpack: false, + for_call: false, + }); + + for value in unpacked_values { + self.compile_expression(value)?; + self.emit(Instruction::DictUpdate); } + Ok(()) } diff --git a/src/symboltable.rs b/src/symboltable.rs index 9a216cd..3876563 100644 --- a/src/symboltable.rs +++ b/src/symboltable.rs @@ -1094,7 +1094,7 @@ impl SymbolTableBuilder { ) -> SymbolTableResult { // Evaluate eventual default parameters: self.scan_expressions(&args.defaults, ExpressionContext::Load)?; - for expression in args.kw_defaults.iter().flatten() { + for expression in args.kw_defaults.iter() { self.scan_expression(expression, ExpressionContext::Load)?; }