Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor asm ast #1736

Merged
merged 17 commits into from
Sep 24, 2024
Merged

Refactor asm ast #1736

merged 17 commits into from
Sep 24, 2024

Conversation

Schaeff
Copy link
Collaborator

@Schaeff Schaeff commented Aug 30, 2024

No description provided.

@Schaeff Schaeff mentioned this pull request Aug 30, 2024
.filter(|s| {
matches!(
s,
PilStatement::EnumDeclaration(..) | PilStatement::LetStatement(..)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I already envision spending quite some time trying to figure out why something is missing from the output. Is there a safer way to do this check so that we add variants here if the code is extended?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using an exhaustive match instead

})
}

pub fn get_machine(&self, ty: &AbsoluteSymbolPath) -> Option<&Machine> {
let mut path = ty.clone();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I the clone needed here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes since modules are indexed by path and machines by name. To avoid this clone, we'd need slices of AbsoluteSymbolPath which is not implemented in this PR.

@@ -41,7 +41,7 @@ pub struct PILGraph {
pub main: Machine,
pub entry_points: Vec<Operation>,
pub objects: BTreeMap<Location, Object>,
pub definitions: BTreeMap<AbsoluteSymbolPath, TypeOrExpression>,
pub definitions: BTreeMap<AbsoluteSymbolPath, Vec<PilStatement>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the key here the module name?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, comment added

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should also rename this to statements?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

ModuleStatement::TraitImplementation(_) => None,
ModuleStatement::SymbolDefinition(d) => Box::new(once(&d.name)),
ModuleStatement::PilStatement(s) => {
Box::new(s.symbol_definition_names().map(|(name, _)| name))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should call symbol_definition_names_and_contained here, but I'm not sure. .._and_contained also returns the variants in an enum for example.

Copy link
Collaborator Author

@Schaeff Schaeff Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... if we have

enum Foo {
    Bar
}

Then the defined names should be ["Foo", "Bar"]? Or ["Foo", "Foo::Bar"]?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[("Foo", None), ("Foo", Some("Bar"))]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, what should this function return? Currently it returns all names defined in a given statement, as strings. Should the signature then change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least before your PR this was essentially only used by the call to local_names() inside check_machine to see if there is a name clash, so I think it's not needed to return the nested names.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used only in the importer to prevent duplicates, and to detect the standard library. I think we do not need the nested names for that. Then the current code is correct?

@@ -97,7 +81,7 @@ pub enum SymbolValueRef<'a> {
/// A module definition
Module(ModuleRef<'a>),
/// A generic symbol / function.
Expression(&'a TypedExpression),
Expression(&'a Option<Expression>, &'a Option<TypeScheme<Expression>>),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not keep TypedExpression? Lifetime issues?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's constructed from a reference over PilStatement::LetStatement which is

    LetStatement(
        SourceRef,
        String,
        Option<TypeScheme<Expression>>,
        Option<Expression>,
    ),

My understanding is that SymbolValueRef::Expression also covers statements like let a: Type; which seem to be allowed. TypedExpression would not cover that.

}
}
PilStatement::TraitImplementation(_, trait_impl) => {
for f in &mut trait_impl.functions {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should also call canonicalize_inside_type_scheme for trait_impl.type_scheme.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted to https://github.com/powdr-labs/powdr/pull/1750/files because I'm not sure

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merged

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following discussion, removed the commit and instead used generics on the existing data structures

Comment on lines 190 to 202
PilStatement::Include(..) => false,
PilStatement::Namespace(..) => false,
PilStatement::PolynomialDefinition(..) => false,
PilStatement::PublicDeclaration(..) => false,
PilStatement::PolynomialConstantDeclaration(..) => false,
PilStatement::PolynomialConstantDefinition(..) => false,
PilStatement::PolynomialCommitDeclaration(..) => false,
PilStatement::PlookupIdentity(..) => false,
PilStatement::PermutationIdentity(..) => false,
PilStatement::ConnectIdentity(..) => false,
PilStatement::TraitImplementation(..) => false,
PilStatement::TraitDeclaration(..) => false,
PilStatement::Expression(..) => false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PilStatement::Include(..) => false,
PilStatement::Namespace(..) => false,
PilStatement::PolynomialDefinition(..) => false,
PilStatement::PublicDeclaration(..) => false,
PilStatement::PolynomialConstantDeclaration(..) => false,
PilStatement::PolynomialConstantDefinition(..) => false,
PilStatement::PolynomialCommitDeclaration(..) => false,
PilStatement::PlookupIdentity(..) => false,
PilStatement::PermutationIdentity(..) => false,
PilStatement::ConnectIdentity(..) => false,
PilStatement::TraitImplementation(..) => false,
PilStatement::TraitDeclaration(..) => false,
PilStatement::Expression(..) => false,
PilStatement::Include(..) |
PilStatement::Namespace(..) |
PilStatement::PolynomialDefinition(..) |
PilStatement::PublicDeclaration(..) |
PilStatement::PolynomialConstantDeclaration(..) |
PilStatement::PolynomialConstantDefinition(..) |
PilStatement::PolynomialCommitDeclaration(..) |
PilStatement::PlookupIdentity(..) |
PilStatement::PermutationIdentity(..) |
PilStatement::ConnectIdentity(..) |
PilStatement::TraitImplementation(..) |
PilStatement::TraitDeclaration(..) |
PilStatement::Expression(..) => false,

@Schaeff Schaeff force-pushed the refactor-parsed-and-asm-analysis-ast branch from 5a50e2e to af73301 Compare September 6, 2024 23:14
@Schaeff Schaeff force-pushed the refactor-parsed-and-asm-analysis-ast branch from af73301 to 1928b49 Compare September 13, 2024 15:44
@Schaeff Schaeff force-pushed the refactor-parsed-and-asm-analysis-ast branch from c04d0d7 to 24550f5 Compare September 15, 2024 22:12

/// A trait to operate the possible types for the array type lengths
pub trait ArrayLength: std::fmt::Display + std::fmt::Debug {
fn try_to_expression_mut(&mut self) -> Option<&mut Expression>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try_as_expression_mut?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

/// A trait to operate the possible types for the array type lengths
pub trait ArrayLength: std::fmt::Display + std::fmt::Debug {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExpressionInArrayLength?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@chriseth chriseth added this pull request to the merge queue Sep 24, 2024
Merged via the queue into main with commit 64e7bf1 Sep 24, 2024
14 checks passed
@chriseth chriseth deleted the refactor-parsed-and-asm-analysis-ast branch September 24, 2024 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants