update asdl_rs.py for updated Python.asdl

This commit is contained in:
Jeong YunWon 2022-08-12 03:48:14 +09:00
parent c476c276ab
commit 308c82a935
2 changed files with 28 additions and 49 deletions

View file

@ -552,6 +552,7 @@ impl Compiler {
Import { names } => { Import { names } => {
// import a, b, c as d // import a, b, c as d
for name in names { for name in names {
let name = &name.node;
self.emit_constant(ConstantData::Integer { self.emit_constant(ConstantData::Integer {
value: num_traits::Zero::zero(), value: num_traits::Zero::zero(),
}); });
@ -574,7 +575,7 @@ impl Compiler {
module, module,
names, 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 { let from_list = if import_star {
if self.ctx.in_func() { if self.ctx.in_func() {
@ -588,7 +589,7 @@ impl Compiler {
names names
.iter() .iter()
.map(|n| ConstantData::Str { .map(|n| ConstantData::Str {
value: n.name.to_owned(), value: n.node.name.to_owned(),
}) })
.collect() .collect()
}; };
@ -597,7 +598,7 @@ impl Compiler {
// from .... import (*fromlist) // from .... import (*fromlist)
self.emit_constant(ConstantData::Integer { self.emit_constant(ConstantData::Integer {
value: (*level).into(), value: (*level).unwrap_or(0).into(),
}); });
self.emit_constant(ConstantData::Tuple { self.emit_constant(ConstantData::Tuple {
elements: from_list, elements: from_list,
@ -615,6 +616,7 @@ impl Compiler {
// from mod import a, b as c // from mod import a, b as c
for name in names { for name in names {
let name = &name.node;
let idx = self.name(&name.name); let idx = self.name(&name.name);
// import symbol from module: // import symbol from module:
self.emit(Instruction::ImportFrom { idx }); self.emit(Instruction::ImportFrom { idx });
@ -881,13 +883,11 @@ impl Compiler {
let mut num_kw_only_defaults = 0; let mut num_kw_only_defaults = 0;
for (kw, default) in args.kwonlyargs.iter().zip(&args.kw_defaults) { for (kw, default) in args.kwonlyargs.iter().zip(&args.kw_defaults) {
if let Some(default) = default { self.emit_constant(ConstantData::Str {
self.emit_constant(ConstantData::Str { value: kw.node.arg.clone(),
value: kw.node.arg.clone(), });
}); self.compile_expression(default)?;
self.compile_expression(default)?; num_kw_only_defaults += 1;
num_kw_only_defaults += 1;
}
} }
if num_kw_only_defaults > 0 { if num_kw_only_defaults > 0 {
self.emit(Instruction::BuildMap { self.emit(Instruction::BuildMap {
@ -1930,49 +1930,28 @@ impl Compiler {
fn compile_dict( fn compile_dict(
&mut self, &mut self,
keys: &[Option<Box<ast::Expr>>], keys: &[ast::Expr],
values: &[ast::Expr], values: &[ast::Expr],
) -> CompileResult<()> { ) -> CompileResult<()> {
let mut size = 0; 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()) { let (packed_values, unpacked_values) = values.split_at(keys.len());
if is_unpacking { for (key, value) in keys.iter().zip(packed_values) {
for (_, value) in subpairs { self.compile_expression(key)?;
self.compile_expression(value)?; self.compile_expression(value)?;
size += 1; 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;
}
} }
if size == 0 { self.emit(Instruction::BuildMap {
self.emit(Instruction::BuildMap { size,
size, unpack: false,
unpack: false, for_call: false,
for_call: false, });
});
} for value in unpacked_values {
if size > 1 || has_unpacking { self.compile_expression(value)?;
self.emit(Instruction::BuildMap { self.emit(Instruction::DictUpdate);
size,
unpack: true,
for_call: false,
});
} }
Ok(()) Ok(())
} }

View file

@ -1094,7 +1094,7 @@ impl SymbolTableBuilder {
) -> SymbolTableResult { ) -> SymbolTableResult {
// Evaluate eventual default parameters: // Evaluate eventual default parameters:
self.scan_expressions(&args.defaults, ExpressionContext::Load)?; 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)?; self.scan_expression(expression, ExpressionContext::Load)?;
} }