Implement review comments and improve parsing a bit.

This commit is contained in:
Windel Bouwman 2019-07-25 23:54:31 +02:00
parent 9f7ef2050e
commit 7e9b3ddc1f
2 changed files with 41 additions and 36 deletions

View file

@ -385,35 +385,47 @@ impl Compiler {
} }
self.set_label(end_label); self.set_label(end_label);
} }
With { items, body } => { With {
let end_label = self.new_label(); is_async,
for item in items { items,
self.compile_expression(&item.context_expr)?; body,
self.emit(Instruction::SetupWith { end: end_label }); } => {
match &item.optional_vars { if *is_async {
Some(var) => { unimplemented!("async with");
self.compile_store(var)?; } else {
} let end_label = self.new_label();
None => { for item in items {
self.emit(Instruction::Pop); self.compile_expression(&item.context_expr)?;
self.emit(Instruction::SetupWith { end: end_label });
match &item.optional_vars {
Some(var) => {
self.compile_store(var)?;
}
None => {
self.emit(Instruction::Pop);
}
} }
} }
}
self.compile_statements(body)?; self.compile_statements(body)?;
for _ in 0..items.len() { for _ in 0..items.len() {
self.emit(Instruction::CleanupWith { end: end_label }); self.emit(Instruction::CleanupWith { end: end_label });
}
self.set_label(end_label);
} }
self.set_label(end_label);
} }
For { For {
is_async,
target, target,
iter, iter,
body, body,
orelse, orelse,
} => self.compile_for(target, iter, body, orelse)?, } => {
AsyncFor { .. } => { if *is_async {
unimplemented!("async for"); unimplemented!("async for");
} else {
self.compile_for(target, iter, body, orelse)?
}
} }
Raise { exception, cause } => match exception { Raise { exception, cause } => match exception {
Some(value) => { Some(value) => {
@ -439,14 +451,18 @@ impl Compiler {
finalbody, finalbody,
} => self.compile_try_statement(body, handlers, orelse, finalbody)?, } => self.compile_try_statement(body, handlers, orelse, finalbody)?,
FunctionDef { FunctionDef {
is_async,
name, name,
args, args,
body, body,
decorator_list, decorator_list,
returns, returns,
} => self.compile_function_def(name, args, body, decorator_list, returns)?, } => {
AsyncFunctionDef { .. } => { if *is_async {
unimplemented!("async def"); unimplemented!("async def");
} else {
self.compile_function_def(name, args, body, decorator_list, returns)?
}
} }
ClassDef { ClassDef {
name, name,

View file

@ -240,13 +240,7 @@ impl SymbolTableBuilder {
args, args,
decorator_list, decorator_list,
returns, returns,
} ..
| AsyncFunctionDef {
name,
body,
args,
decorator_list,
returns,
} => { } => {
self.scan_expressions(decorator_list)?; self.scan_expressions(decorator_list)?;
self.register_name(name, SymbolRole::Assigned)?; self.register_name(name, SymbolRole::Assigned)?;
@ -289,12 +283,7 @@ impl SymbolTableBuilder {
iter, iter,
body, body,
orelse, orelse,
} ..
| AsyncFor {
target,
iter,
body,
orelse,
} => { } => {
self.scan_expression(target)?; self.scan_expression(target)?;
self.scan_expression(iter)?; self.scan_expression(iter)?;
@ -346,7 +335,7 @@ impl SymbolTableBuilder {
self.scan_expression(target)?; self.scan_expression(target)?;
self.scan_expression(value)?; self.scan_expression(value)?;
} }
With { items, body } => { With { items, body, .. } => {
for item in items { for item in items {
self.scan_expression(&item.context_expr)?; self.scan_expression(&item.context_expr)?;
if let Some(expression) = &item.optional_vars { if let Some(expression) = &item.optional_vars {