no circular reference

This commit is contained in:
gluax 2021-03-25 14:51:43 -04:00
parent 99db18167a
commit cf603dbc73
2 changed files with 32 additions and 30 deletions

View File

@ -53,7 +53,7 @@ impl<'a> Node for Circuit<'a> {
} }
impl<'a> Circuit<'a> { impl<'a> Circuit<'a> {
pub(super) fn init(scope: &'a Scope<'a>, value: &leo_ast::Circuit) -> &'a Circuit<'a> { pub(super) fn init(scope: &'a Scope<'a>, value: &leo_ast::Circuit) -> Result<&'a Circuit<'a>, AsgConvertError> {
let new_scope = scope.make_subscope(); let new_scope = scope.make_subscope();
let circuit = scope.context.alloc_circuit(Circuit { let circuit = scope.context.alloc_circuit(Circuit {
@ -66,7 +66,24 @@ impl<'a> Circuit<'a> {
}); });
new_scope.circuit_self.replace(Some(circuit)); new_scope.circuit_self.replace(Some(circuit));
circuit let mut members = circuit.members.borrow_mut();
for member in value.members.iter() {
if let leo_ast::CircuitMember::CircuitVariable(name, type_) = member {
if members.contains_key(&name.name) {
return Err(AsgConvertError::redefined_circuit_member(
&value.circuit_name.name,
&name.name,
&name.span,
));
}
members.insert(
name.name.clone(),
CircuitMember::Variable(new_scope.resolve_ast_type(type_)?),
);
}
}
Ok(circuit)
} }
pub(super) fn init_member( pub(super) fn init_member(
@ -81,35 +98,20 @@ impl<'a> Circuit<'a> {
let mut members = circuit.members.borrow_mut(); let mut members = circuit.members.borrow_mut();
for member in value.members.iter() { for member in value.members.iter() {
match member { if let leo_ast::CircuitMember::CircuitFunction(function) = member {
leo_ast::CircuitMember::CircuitVariable(name, type_) => { if members.contains_key(&function.identifier.name) {
if members.contains_key(&name.name) { return Err(AsgConvertError::redefined_circuit_member(
return Err(AsgConvertError::redefined_circuit_member( &value.circuit_name.name,
&value.circuit_name.name, &function.identifier.name,
&name.name, &function.identifier.span,
&name.span, ));
));
}
members.insert(
name.name.clone(),
CircuitMember::Variable(new_scope.resolve_ast_type(type_)?),
);
} }
leo_ast::CircuitMember::CircuitFunction(function) => { let asg_function = Function::init(new_scope, function)?;
if members.contains_key(&function.identifier.name) { asg_function.circuit.replace(Some(circuit));
return Err(AsgConvertError::redefined_circuit_member( if asg_function.is_test() {
&value.circuit_name.name, return Err(AsgConvertError::circuit_test_function(&function.identifier.span));
&function.identifier.name,
&function.identifier.span,
));
}
let asg_function = Function::init(new_scope, function)?;
asg_function.circuit.replace(Some(circuit));
if asg_function.is_test() {
return Err(AsgConvertError::circuit_test_function(&function.identifier.span));
}
members.insert(function.identifier.name.clone(), CircuitMember::Function(asg_function));
} }
members.insert(function.identifier.name.clone(), CircuitMember::Function(asg_function));
} }
} }

View File

@ -238,7 +238,7 @@ impl<'a> Program<'a> {
// Prepare header-like scope entries. // Prepare header-like scope entries.
for (name, circuit) in program.circuits.iter() { for (name, circuit) in program.circuits.iter() {
assert_eq!(name.name, circuit.circuit_name.name); assert_eq!(name.name, circuit.circuit_name.name);
let asg_circuit = Circuit::init(scope, circuit); let asg_circuit = Circuit::init(scope, circuit)?;
scope.circuits.borrow_mut().insert(name.name.clone(), asg_circuit); scope.circuits.borrow_mut().insert(name.name.clone(), asg_circuit);
} }