Fix bug in codegen for CircuitExpression

This commit is contained in:
Pranav Gaddamadugu 2022-07-20 12:59:23 -07:00
parent eb1f9a561f
commit 3e780dd42b
4 changed files with 9 additions and 7 deletions

View File

@ -28,8 +28,10 @@ pub struct CodeGenerator<'a> {
pub(crate) current_function: Option<&'a Function>,
/// Mapping of variables to registers.
pub(crate) variable_mapping: IndexMap<&'a Symbol, String>,
/// Mapping of composite names to type (`circuit` or `register`).
pub(crate) composite_mapping: IndexMap<&'a Symbol, String>,
/// Mapping of composite names to a tuple containing metadata associated with the name.
/// The first element of the tuple indicate whether the composite is a record or not.
/// The second element of the tuple is a string modifier used for code generation.
pub(crate) composite_mapping: IndexMap<&'a Symbol, (bool, String)>,
}
impl<'a> CodeGenerator<'a> {

View File

@ -155,9 +155,9 @@ impl<'a> CodeGenerator<'a> {
fn visit_circuit_init(&mut self, input: &'a CircuitExpression) -> (String, String) {
// Lookup circuit or record.
let name = if let Some(type_) = self.composite_mapping.get(&input.name.name) {
let name = if let Some((is_record, type_)) = self.composite_mapping.get(&input.name.name) {
let name = input.name.to_string().to_lowercase();
if name.eq("record") {
if *is_record {
// record.private;
format!("{}.{}", name, type_)
} else {

View File

@ -92,7 +92,7 @@ impl<'a> CodeGenerator<'a> {
fn visit_circuit(&mut self, circuit: &'a Circuit) -> String {
// Add private symbol to composite types.
self.composite_mapping
.insert(&circuit.identifier.name, String::from("private")); // todo: private by default here.
.insert(&circuit.identifier.name, (false, String::from("private"))); // todo: private by default here.
let mut output_string = format!("interface {}:\n", circuit.identifier.to_string().to_lowercase()); // todo: check if this is safe from name conflicts.
@ -112,7 +112,7 @@ impl<'a> CodeGenerator<'a> {
// Add record symbol to composite types.
let mut output_string = String::from("record");
self.composite_mapping
.insert(&record.identifier.name, output_string.clone());
.insert(&record.identifier.name, (true, output_string.clone()));
writeln!(output_string, " {}:", record.identifier.to_string().to_lowercase())
.expect("failed to write to string"); // todo: check if this is safe from name conflicts.

View File

@ -39,7 +39,7 @@ impl<'a> CodeGenerator<'a> {
| Type::U64
| Type::U128 => format!("{}", input),
Type::Identifier(ident) => {
if let Some(type_) = self.composite_mapping.get(&ident.name) {
if let Some((_, type_)) = self.composite_mapping.get(&ident.name) {
format!("{}.{}", ident.to_string().to_lowercase(), type_)
} else {
unreachable!("All composite types should be known at this phase of compilation")