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

feat: support binary operations for function return type #184

Merged
merged 6 commits into from
Sep 21, 2024
Merged

Conversation

katat
Copy link
Collaborator

@katat katat commented Sep 18, 2024

It is quite common to see scenarios like:

fn bigint_add(lhs: [Field; LEN], rhs: [Field; LEN]) -> [Field; LEN + 1]

fn bigint_mul(lhs: [Field; LHS], rhs: [Field; RHS]) -> [Field; LHS + RHS]

fn bigint_mul_nocarry(lhs: [Field; LEN], rhs: [Field; LEN]) -> [Field; LHS * 2 -1]

So this PR is to support the binary operations over symbolic size for function return type.

@katat katat requested a review from mimoo September 18, 2024 09:13
Copy link
Collaborator

@mimoo mimoo left a comment

Choose a reason for hiding this comment

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

in the middle of it, but I'm wondering why the addition of all that code when we can already rely on the parser to warn when there's no parenthesis in an expression of binary operations. I know we discussed that before but it's still not clear to me (again) why we don't just parse an expression there and then convert it later to something we can use

token
} else {
tokens.bump(ctx).unwrap()
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe buff_token.or_else(|| tokens.bump(ctx)).unwrap()?

src/parser/types.rs Show resolved Hide resolved
@katat
Copy link
Collaborator Author

katat commented Sep 20, 2024

I think the short answer for now is that the signatures parsing is for constructing types, while the expression parsing is not about the type.

@mimoo
Copy link
Collaborator

mimoo commented Sep 20, 2024

mm I'm not sure I follow, can't you just parse the whole expression at this point with Expr::parse? What is wrong with that approach?

Copy link
Collaborator

@mimoo mimoo left a comment

Choose a reason for hiding this comment

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

Looks good tho!

@katat
Copy link
Collaborator Author

katat commented Sep 21, 2024

Let me try to reason through the differences.

The Ty::parse is for generating this Ty struct:

pub struct Ty {
    pub kind: TyKind,
    pub span: Span,
}

While the Expr::parse is for generating Expr:

pub struct Expr {
    pub node_id: usize,
    pub kind: ExprKind,
    pub span: Span,
}

Because Symbolic is of TyKind::GenericSizedArray, so it is in Ty::parse. Thus it needs to update the Ty::parse to handle the binary operations, although the similar parsing already exists in the Expr::parse.

What your question inspired me to think of is: what if we can use Expr::parse to parse binary expressions in the Ty::parse for the Symbolic. This may reuse the parsing of parenthesis from Expr. Then Symbolic can be constructed from the Expr containing recursive binary operations, which already order the expression node properly based on the parenthesis.

So yeah, looks like the use of Expr::parse would avoid the addition of binary operation parsing in Ty::parse, and simplify the code a lot.

@katat
Copy link
Collaborator Author

katat commented Sep 21, 2024

yeah, it really simplifies the things a lot!

@katat katat merged commit b90489f into main Sep 21, 2024
1 check passed
@mimoo
Copy link
Collaborator

mimoo commented Sep 22, 2024

nice! (I would even suggest changing the Symbolic::parse function to a TryFrom<Expr> implementation which share the same signature but is more explicit)

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