From 0c7903be90914ca0bb467db348ce86f8ebcf7285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 22 Apr 2024 11:29:53 -0300 Subject: [PATCH 1/5] Add ArrayInitExpr --- crates/concrete_ast/src/expressions.rs | 7 +++++++ crates/concrete_ir/src/lowering.rs | 2 ++ 2 files changed, 9 insertions(+) diff --git a/crates/concrete_ast/src/expressions.rs b/crates/concrete_ast/src/expressions.rs index 98ee376..818c666 100644 --- a/crates/concrete_ast/src/expressions.rs +++ b/crates/concrete_ast/src/expressions.rs @@ -15,6 +15,7 @@ pub enum Expression { UnaryOp(UnaryOp, Box), BinaryOp(Box, BinaryOp, Box), StructInit(StructInitExpr), + ArrayInit(ArrayInitExpr), Deref(Box, Span), AsRef(Box, bool, Span), Cast(Box, TypeSpec, Span), @@ -43,6 +44,12 @@ pub struct StructInitField { pub span: Span, } +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ArrayInitExpr { + pub values: Vec, + pub span: Span, +} + #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum UnaryOp { ArithNeg, diff --git a/crates/concrete_ir/src/lowering.rs b/crates/concrete_ir/src/lowering.rs index ba54eeb..c0bf5c7 100644 --- a/crates/concrete_ir/src/lowering.rs +++ b/crates/concrete_ir/src/lowering.rs @@ -739,6 +739,7 @@ fn find_expression_type(builder: &mut FnBodyBuilder, info: &Expression) -> Optio }) } Expression::Cast(_, _, _) => todo!(), + Expression::ArrayInit(_) => todo!(), } } @@ -913,6 +914,7 @@ fn lower_expression( (rvalue, new_ty, *span) } + Expression::ArrayInit(_) => todo!(), }) } From 698828a8af5b459765bb7e6d2984d06fa52ec568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 22 Apr 2024 11:44:18 -0300 Subject: [PATCH 2/5] Add grammar definition --- crates/concrete_parser/src/grammar.lalrpop | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index 20bad8e..ea0c008 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -347,6 +347,14 @@ pub(crate) StructInitExpr: ast::expressions::StructInitExpr = { } } + +pub(crate) ArrayInitExpr: ast::expressions::ArrayInitExpr = { + "[" > "]" => ast::expressions::ArrayInitExpr { + values: values.into_iter().collect(), + span: Span::new(lo, hi), + } +} + // Expressions pub(crate) Term: ast::expressions::Expression = { @@ -391,6 +399,7 @@ pub(crate) Expression: ast::expressions::Expression = { #[precedence(level="5")] #[assoc(side="left")] "as" => ast::expressions::Expression::Cast(Box::new(a), b, Span::new(lo, hi)), "(" ")" => ast::expressions::Expression::StructInit(<>), + => ast::expressions::Expression::ArrayInit(<>), } pub BinaryFirstLvlOp: ast::expressions::BinaryOp = { From c28eca5eda8678469e6191497cc40951b724581b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 22 Apr 2024 11:44:24 -0300 Subject: [PATCH 3/5] Test grammar definition --- crates/concrete_parser/src/lib.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index c41ed18..f394af1 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -219,6 +219,18 @@ 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]; + } }"##; let lexer = Lexer::new(source); let parser = grammar::ProgramParser::new(); From cdefe9cd9098de52cc1eb42432eb3ec8b3d87735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 22 Apr 2024 11:50:45 -0300 Subject: [PATCH 4/5] Add array access test --- crates/concrete_parser/src/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index f394af1..ba08d0b 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -230,6 +230,8 @@ mod ModuleName { let source = r##"mod MyMod { fn hello() { let mut arr: [u32; 3] = [1, 2, 3]; + + return arr[1]; } }"##; let lexer = Lexer::new(source); From 2029ae15ccf596ee4c9167be8c25ebae9d6b0bd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Mon, 22 Apr 2024 11:51:15 -0300 Subject: [PATCH 5/5] Add nested array test --- crates/concrete_parser/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index ba08d0b..12796b3 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -233,6 +233,20 @@ mod ModuleName { 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();