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

Structs in PIL #1516

Draft
wants to merge 89 commits into
base: main
Choose a base branch
from
Draft

Structs in PIL #1516

wants to merge 89 commits into from

Conversation

gzanitti
Copy link
Collaborator

@gzanitti gzanitti commented Jul 1, 2024

This PR implements structs based on the issue #1016.

To avoid ambiguity with the grammar, for creating an instance of a struct and for accessing its fields, a temporary variation of the grammar is used which should be discussed.

Currently, an instance of struct X {x: int, y: int} is created using X with {x: 1, y:0}.

Copy link
Member

@chriseth chriseth left a comment

Choose a reason for hiding this comment

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

Only very partial review.

importer/src/path_canonicalizer.rs Show resolved Hide resolved
ast/src/parsed/display.rs Outdated Show resolved Hide resolved
ast/src/parsed/display.rs Outdated Show resolved Hide resolved
ast/src/parsed/display.rs Outdated Show resolved Hide resolved
indent(
self.fields
.iter()
.map(|v| format!("{}: {},\n", v.0, v.1))
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
.map(|v| format!("{}: {},\n", v.0, v.1))
.map(|(name, ty)| format!("{name}: {ty},\n"))

Copy link
Member

Choose a reason for hiding this comment

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

Didn't we want to re-use some struct from the trait definition 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.

TraitFunction would be the ideal option, so I think we should rename them: #1816

ast/src/parsed/display.rs Show resolved Hide resolved
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub enum TypeConstructor {
Enum(Arc<EnumDeclaration>, EnumVariant),
Struct(Arc<StructDeclaration>, (String, Type)),
Copy link
Member

Choose a reason for hiding this comment

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

A type constructor for struct and a struct expression are the same thing. Is this the case here or do you make a distinction? I have the impression that we don't need struct type constructors but I might be wrong.

Copy link
Collaborator Author

@gzanitti gzanitti Sep 19, 2024

Choose a reason for hiding this comment

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

I used it mainly in the Statement processor when processing the Struct declaration. TypeDeclaration declares the new type of the struct and TypeConstructor, constructed the types of the internal functions (similar to enums wth variants), using the type declarations of each one of the functions inside the declaration.

For example: struct Dot { x: int, y: int }
would give us a TypeDeclaration Dot and two TypeConstructor Dot::x and Dot::y.

On the other hand, the Struct expression is used at the time of instantiation, for example let f: int -> Dot = |i| Dot{a: 0, b: i};

is this wrong?

It makes sense that Dot::y doesn't actually declare any new type (it is more of a renaming of the internal int type), but my biggest question here is: somehow I have to process the internal types so that they are available later, but if they are not TypeConstructor, what should they be?

Wait, I just realized that there are some things that don't make sense. I was still mixing up some ideas when I started this PR. I'll let you know when it's ready.

@gzanitti gzanitti marked this pull request as draft September 19, 2024 22:45
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