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

Array parsing #113

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading