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

Allow parentheses (..) in expressions #66

Closed
mohammadfawaz opened this issue Jun 27, 2023 · 6 comments · Fixed by #148
Closed

Allow parentheses (..) in expressions #66

mohammadfawaz opened this issue Jun 27, 2023 · 6 comments · Fixed by #148

Comments

@mohammadfawaz
Copy link
Contributor

mohammadfawaz commented Jun 27, 2023

Example:

let x = 1 + (2 * 3);

@mohammadfawaz mohammadfawaz changed the title Allow (..) in expressions Allow parentheses (..) in expressions Jun 27, 2023
mohammadfawaz added a commit that referenced this issue Jul 12, 2023
Closes #72 

Fairly simple change:
- Tuple types
- Tuple expressions
- Tuple indexing expressions. Error out on invalid indices such as `0xa`
or anything that is not a `usize`.

Note that `t.0.0` does not currently work, but `t.0 .0`. Once we
implement #66, we can then write `(t.0).0`. In the future, we can
support `t.0.0` which can be done in two ways:
- Special case the lexer to _not_ parse the `0.0` as a real in the case
of a tuple access (this means the lexer now has to concern itself with
the context).
- Break the real `0.0` apart in the pasrer, which kinda what Rust does
(see rust-lang/rust#71322 after an attempt for
the other method in rust-lang/rust#70420)
@lethalgem
Copy link
Contributor

@mohammadfawaz What should a parentheses be called?

Right now this is the test and result

check(
        &run_parser!(expr(), "1 + (2 * 3);"),
        expect_test::expect!["BinaryOp { op: Add, lhs: Immediate(Int(1)), rhs: Tuple([BinaryOp { op: Mul, lhs: Immediate(Int(2)), rhs: Immediate(Int(3)) }]) }"],
    );

Obviously, Tuple is incorrect. Do we just name it Paren? Is there a specific output for parentheses in expressions you're expecting?

@mohammadfawaz
Copy link
Contributor Author

Obviously, Tuple is incorrect. Do we just name it Paren? Is there a specific output for parentheses in expressions you're expecting?

Right, yeah the parser should be careful what actually get prioritized here. I do imagine a expression enum variant called Parens which contains an expression.

@otrho
Copy link
Contributor

otrho commented Jul 17, 2023

The difference between a single element Tuple and a parenthesised expression is subtle. Most languages handle it by putting a comma after the field in the tuple, i.e., (123,).

@mohammadfawaz
Copy link
Contributor Author

mohammadfawaz commented Jul 17, 2023

Yeah makes sense. So should we assume that something like (123) without a , is equivalent to just 123? For example, this does not compile in Rust:

fn main() {
    let x = (1,) + 2;
}

but this does:

fn main() {
    let x = (1) + 2;
}

This also works with a warning that says that the parentheses are unnecessary:

fn main() {
    let x:(u32) = 5;
}

@otrho
Copy link
Contributor

otrho commented Jul 17, 2023

Yep. This works though:

fn main() {
    let x = (1,).0 + 2;
}

@mohammadfawaz
Copy link
Contributor Author

When #138 is merged, there will no longer be any ambiguities here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants