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

parse parenthesized expressions #171

Merged
merged 3 commits into from
May 12, 2022
Merged

Conversation

vemoo
Copy link
Contributor

@vemoo vemoo commented May 7, 2022

As per doc comments tuples are of len != 1

/// `(expr)` of len != 1
Tuple(Vec<Expr>),

so I fixed parsing to match that.

fixes #141

@netlify
Copy link

netlify bot commented May 7, 2022

Deploy Preview for dada-lang ready!

Name Link
🔨 Latest commit d46cd45
🔍 Latest deploy log https://app.netlify.com/sites/dada-lang/deploys/627a93c8d71e5c00081065df
😎 Deploy Preview https://deploy-preview-171--dada-lang.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@vemoo
Copy link
Contributor Author

vemoo commented May 7, 2022

print((1))

is causing this assert to fail:

// Check that the validated expression always has the same
// origin as the expression we started with.
assert_eq!(result.origin_in(self.origins).syntax_expr, expr);

I have fixed it by doing:

syntax::ExprData::Parenthesized(parenthesized_expr) => {
    let data = self
        .validate_expr_in_mode(*parenthesized_expr, mode)
        .data(self.tables)
        .clone();
    self.add(data, expr)
}

in

syntax::ExprData::Parenthesized(parenthesized_expr) => {
self.validate_expr_in_mode(*parenthesized_expr, mode)
}

but I don't know if that's the proper fix.

@vemoo
Copy link
Contributor Author

vemoo commented May 8, 2022

Ok, I think I found the proper fix, by adding validated::ExprData::Parenthesized

@@ -129,10 +129,10 @@ pub enum ExprData {
/// `[shared|var|atomic] x = expr`
Var(LocalVariableDecl, Expr),

/// `expr`
/// `(expr)`
Parenthesized(Expr),
Copy link
Member

Choose a reason for hiding this comment

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

We don't seem to have anything parsed as a Parenthsized before 😅 .

Copy link
Member

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

Nice! I think that the assertion is probably just wrong, though.

@@ -258,6 +258,9 @@ pub enum ExprData {
/// `expr.give`
Give(Place),

/// `(expr)`
Parenthesized(Expr),
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I think we shouldn't have this in Validated....

Copy link
Member

Choose a reason for hiding this comment

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

...it belongs in Syntax, but Validated is supposed to be kind of only the things that have meaning to the compiler

Copy link
Member

Choose a reason for hiding this comment

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

If this is causing that assertion to fail, we should probably just remove the assertion.

@@ -398,7 +398,8 @@ impl<'me> Validator<'me> {
}

syntax::ExprData::Parenthesized(parenthesized_expr) => {
self.validate_expr_in_mode(*parenthesized_expr, mode)
let validated_expr = self.validate_expr_in_mode(*parenthesized_expr, mode);
self.add(validated::ExprData::Parenthesized(validated_expr), expr)
Copy link
Member

Choose a reason for hiding this comment

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

...in which case this diff is unnecessary, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to change that because otherwise this assert was failing:

// Check that the validated expression always has the same
// origin as the expression we started with.
assert_eq!(result.origin_in(self.origins).syntax_expr, expr);

Before adding Parenthesized to Validated I did

syntax::ExprData::Parenthesized(parenthesized_expr) => {
let data = self
.validate_expr_in_mode(*parenthesized_expr, mode)
.data(self.tables)
.clone();
self.add(data, expr)
}

but that felt wrong. ¿Maybe that is a better fix?

Copy link
Contributor Author

@vemoo vemoo May 10, 2022

Choose a reason for hiding this comment

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

Ah, you mean the assertion in validator.rs, not the one from the #141, right?
There's another one in give_validated_expr.

vemoo added 2 commits May 10, 2022 18:27
and use same commet as `validated::ExprData::Tuple` in `syntax::ExprData::Tuple`
@vemoo vemoo force-pushed the parenthesized_expr branch from 60986a3 to d46cd45 Compare May 10, 2022 16:33
Copy link
Member

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

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

Looked it over, this seems better, did have one thought...

@@ -449,7 +449,15 @@ impl CodeParser<'_, '_> {
} else if let Some((span, token_tree)) = self.delimited('(') {
let expr =
self.with_sub_parser(token_tree, |subparser| subparser.parse_only_expr_seq());
Some(self.add(ExprData::Tuple(expr), span))

Some(self.add(
Copy link
Member

Choose a reason for hiding this comment

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

For better or worse, I think this change will mean that (a,) also parses as a parenthesized expression. We should check that. Not sure if we want that or not!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will investigate the best way to emit an error for that case.

Copy link
Member

Choose a reason for hiding this comment

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

Probably we want an error? It's not entirely clear to me.

Copy link
Member

Choose a reason for hiding this comment

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

I'd be ok with just opening up an issue to follow-up on later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

created #177

@nikomatsakis
Copy link
Member

bors r+

@bors
Copy link
Contributor

bors bot commented May 12, 2022

@bors bors bot merged commit 4b516b9 into dada-lang:main May 12, 2022
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.

(expr) is not supported
3 participants