diff --git a/src/protocol/parser/pass_definitions.rs b/src/protocol/parser/pass_definitions.rs index 383d76f7d9ac00efcd9ea11af1bff108e3bcb3cb..16c5af1d7a6c63b60df29b87aa4c7b770bcada19 100644 --- a/src/protocol/parser/pass_definitions.rs +++ b/src/protocol/parser/pass_definitions.rs @@ -280,13 +280,15 @@ impl PassDefinitions { )?; // Consume block and the definition's scope - let body = self.consume_block_statement(module, iter, ctx)?; + let body_id = self.consume_block_statement(module, iter, ctx)?; + let scope_id = ctx.heap.alloc_scope(|this| Scope::new(this, ScopeAssociation::Definition(definition_id))); // Assign everything in the preallocated AST node let function = ctx.heap[definition_id].as_function_mut(); function.return_type = parser_type; function.parameters = parameters; - function.body = body; + function.scope = scope_id; + function.body = body_id; Ok(()) } @@ -315,12 +317,14 @@ impl PassDefinitions { let parameters = parameter_section.into_vec(); // Consume block - let body = self.consume_block_statement(module, iter, ctx)?; + let body_id = self.consume_block_statement(module, iter, ctx)?; + let scope_id = ctx.heap.alloc_scope(|this| Scope::new(this, ScopeAssociation::Definition(definition_id))); // Assign everything in the AST node let component = ctx.heap[definition_id].as_component_mut(); component.parameters = parameters; - component.body = body; + component.scope = scope_id; + component.body = body_id; Ok(()) } @@ -468,7 +472,7 @@ impl PassDefinitions { scope: ScopeId::new_invalid(), }; - let (false_body, false_body_scope_id) = if has_ident(&module.source, iter, KW_STMT_ELSE) { + let false_body = if has_ident(&module.source, iter, KW_STMT_ELSE) { iter.consume(); let false_body = IfStatementCase{ body: self.consume_statement(module, iter, ctx)?, @@ -476,9 +480,9 @@ impl PassDefinitions { }; let false_body_scope_id = false_body.scope; - (Some(false_body), Some(false_body_scope_id)) + Some(false_body) } else { - (None, None) + None }; // Construct AST elements @@ -496,13 +500,17 @@ impl PassDefinitions { next: StatementId::new_invalid(), }); let true_scope_id = ctx.heap.alloc_scope(|this| Scope::new(this, ScopeAssociation::If(if_stmt_id, true))); + let false_scope_id = if false_body.is_some() { + Some(ctx.heap.alloc_scope(|this| Scope::new(this, ScopeAssociation::If(if_stmt_id, false)))) + } else { + None + }; let if_stmt = &mut ctx.heap[if_stmt_id]; if_stmt.end_if = end_if_stmt_id; if_stmt.true_case.scope = true_scope_id; if let Some(false_case) = &mut if_stmt.false_case { - let false_scope_id = ctx.heap.alloc_scope(|this| Scope::new(this, ScopeAssociation::If(if_stmt_id, false))); - false_case.scope = false_scope_id + false_case.scope = false_scope_id.unwrap(); } return Ok(if_stmt_id);