Skip to content

Commit

Permalink
Merge pull request #113 from lambdaclass/array-parsing
Browse files Browse the repository at this point in the history
Array parsing
  • Loading branch information
igaray authored Apr 25, 2024
2 parents 383c46e + 2029ae1 commit 9b50130
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/concrete_ast/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub enum Expression {
UnaryOp(UnaryOp, Box<Self>),
BinaryOp(Box<Self>, BinaryOp, Box<Self>),
StructInit(StructInitExpr),
ArrayInit(ArrayInitExpr),
Deref(Box<Self>, Span),
AsRef(Box<Self>, bool, Span),
Cast(Box<Self>, TypeSpec, Span),
Expand Down Expand Up @@ -43,6 +44,12 @@ pub struct StructInitField {
pub span: Span,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ArrayInitExpr {
pub values: Vec<Expression>,
pub span: Span,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum UnaryOp {
ArithNeg,
Expand Down
2 changes: 2 additions & 0 deletions crates/concrete_ir/src/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ fn find_expression_type(builder: &mut FnBodyBuilder, info: &Expression) -> Optio
})
}
Expression::Cast(_, _, _) => todo!(),
Expression::ArrayInit(_) => todo!(),
}
}

Expand Down Expand Up @@ -913,6 +914,7 @@ fn lower_expression(

(rvalue, new_ty, *span)
}
Expression::ArrayInit(_) => todo!(),
})
}

Expand Down
9 changes: 9 additions & 0 deletions crates/concrete_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ pub(crate) StructInitExpr: ast::expressions::StructInitExpr = {
}
}


pub(crate) ArrayInitExpr: ast::expressions::ArrayInitExpr = {
<lo:@L> "[" <values:Comma<Expression>> "]" <hi:@R> => ast::expressions::ArrayInitExpr {
values: values.into_iter().collect(),
span: Span::new(lo, hi),
}
}

// Expressions

pub(crate) Term: ast::expressions::Expression = {
Expand Down Expand Up @@ -391,6 +399,7 @@ pub(crate) Expression: ast::expressions::Expression = {
#[precedence(level="5")] #[assoc(side="left")]
<lo:@L> <a:Expression> "as" <b: TypeSpec> <hi:@R> => ast::expressions::Expression::Cast(Box::new(a), b, Span::new(lo, hi)),
"(" <StructInitExpr> ")" => ast::expressions::Expression::StructInit(<>),
<ArrayInitExpr> => ast::expressions::Expression::ArrayInit(<>),
}

pub BinaryFirstLvlOp: ast::expressions::BinaryOp = {
Expand Down
28 changes: 28 additions & 0 deletions crates/concrete_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,34 @@ mod ModuleName {
return result;
}
}"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
parser.parse(lexer).unwrap();
}

#[test]
fn parse_array() {
let source = r##"mod MyMod {
fn hello() {
let mut arr: [u32; 3] = [1, 2, 3];
return arr[1];
}
}"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
parser.parse(lexer).unwrap();
}

#[test]
fn parse_nested_array() {
let source = r##"mod MyMod {
fn hello() {
let mut arr: [[u32; 2]; 2] = [[1, 2], [3, 4]];
return arr[1][0];
}
}"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
Expand Down

0 comments on commit 9b50130

Please sign in to comment.