Skip to content

Commit

Permalink
Merge #171
Browse files Browse the repository at this point in the history
171: parse parenthesized expressions r=nikomatsakis a=vemoo

As per doc comments tuples are of `len != 1` https://github.com/dada-lang/dada/blob/0793cdf392f8028b7f00e63cef304e51b77f7da5/components/dada-ir/src/code/syntax.rs#L135-L136

so I fixed parsing to match that.

fixes #141

Co-authored-by: Bernardo Uriarte <[email protected]>
  • Loading branch information
bors[bot] and vemoo authored May 12, 2022
2 parents 23c1e10 + d46cd45 commit 4b516b9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions components/dada-ir/src/code/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ pub enum ExprData {
/// `[shared|var|atomic] x = expr`
Var(LocalVariableDecl, Expr),

/// `expr`
/// `(expr)`
Parenthesized(Expr),

/// `(expr)` of len != 1
/// `()` or `(a, b, ...)` (i.e., expr seq cannot have length 1)
Tuple(Vec<Expr>),

/// `if condition { block } [else { block }]`
Expand Down
10 changes: 9 additions & 1 deletion components/dada-parse/src/parser/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,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(
if expr.len() == 1 {
ExprData::Parenthesized(expr[0])
} else {
ExprData::Tuple(expr)
},
span,
))
} else {
None
}
Expand Down
16 changes: 2 additions & 14 deletions components/dada-validate/src/validate/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,24 +189,12 @@ impl<'me> Validator<'me> {

#[tracing::instrument(level = "debug", skip(self, expr))]
fn give_validated_expr(&mut self, expr: syntax::Expr) -> validated::Expr {
let result = self.validate_expr_in_mode(expr, ExprMode::give());

// 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);

result
self.validate_expr_in_mode(expr, ExprMode::give())
}

#[tracing::instrument(level = "debug", skip(self, expr))]
pub(crate) fn reserve_validated_expr(&mut self, expr: syntax::Expr) -> validated::Expr {
let result = self.validate_expr_in_mode(expr, ExprMode::Reserve);

// 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);

result
self.validate_expr_in_mode(expr, ExprMode::Reserve)
}

fn validate_expr_in_mode(&mut self, expr: syntax::Expr, mode: ExprMode) -> validated::Expr {
Expand Down
7 changes: 7 additions & 0 deletions dada_tests/parser/parenthesized_expr.dada
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
async fn main() {
x = (1 + 1)
print(x).await #! OUTPUT 2
x = (1)
print(x).await #! OUTPUT 1
print((3)).await #! OUTPUT 3
}
Empty file.
3 changes: 3 additions & 0 deletions dada_tests/parser/parenthesized_expr/stdout.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
2
1
3

0 comments on commit 4b516b9

Please sign in to comment.