From 21bdfee43fca4c61163b051f2600e16af9869bd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 14:43:57 -0300 Subject: [PATCH 01/14] Change ForStmt fields to follow C-like syntax --- crates/concrete_ast/src/statements.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/concrete_ast/src/statements.rs b/crates/concrete_ast/src/statements.rs index b3de285..b37f086 100644 --- a/crates/concrete_ast/src/statements.rs +++ b/crates/concrete_ast/src/statements.rs @@ -53,9 +53,9 @@ pub struct Binding { #[derive(Clone, Debug, Eq, PartialEq)] pub struct ForStmt { - pub name: Ident, - pub from: Expression, - pub to: Expression, + pub init: LetStmt, + pub condition: Expression, + pub post: AssignStmt, pub contents: Vec, } From 96e96eda1693dafa53776f8d79ff5802a891fc8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 14:53:44 -0300 Subject: [PATCH 02/14] Add for definition to grammar.lalrpop --- crates/concrete_parser/src/grammar.lalrpop | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index 36a372b..d89a45c 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -500,6 +500,7 @@ pub(crate) Statement: ast::statements::Statement = { ";"? => ast::statements::Statement::Match(<>), ";"? => ast::statements::Statement::If(<>), ";"? => ast::statements::Statement::While(<>), + ":"? => ast::statements::Statement::For(<>), ";" => ast::statements::Statement::Let(<>), ";" => ast::statements::Statement::Assign(<>), ";" => ast::statements::Statement::FnCall(<>), @@ -557,3 +558,15 @@ pub(crate) WhileStmt: ast::statements::WhileStmt = { } } } + + +pub(crate) ForStmt: ast::statements::ForStmt = { + "for" ";" ";" "{" "}" => { + ast::statements::WhileStmt { + init, + condition, + post, + contents, + } + } +} From 645fa2d451f59a3dcce0aed9a33f8f038ef83dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 16:20:41 -0300 Subject: [PATCH 03/14] Add optional elements in For definition --- crates/concrete_ast/src/statements.rs | 6 +++--- crates/concrete_parser/src/grammar.lalrpop | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/concrete_ast/src/statements.rs b/crates/concrete_ast/src/statements.rs index b37f086..9a8ea6d 100644 --- a/crates/concrete_ast/src/statements.rs +++ b/crates/concrete_ast/src/statements.rs @@ -53,9 +53,9 @@ pub struct Binding { #[derive(Clone, Debug, Eq, PartialEq)] pub struct ForStmt { - pub init: LetStmt, - pub condition: Expression, - pub post: AssignStmt, + pub init: Option, + pub condition: Option, + pub post: Option, pub contents: Vec, } diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index d89a45c..ffc7507 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -561,8 +561,8 @@ pub(crate) WhileStmt: ast::statements::WhileStmt = { pub(crate) ForStmt: ast::statements::ForStmt = { - "for" ";" ";" "{" "}" => { - ast::statements::WhileStmt { + "for" ? ";" ? ";" ? "{" "}" => { + ast::statements::ForStmt { init, condition, post, From d1462dea4f6f628c262a2046f6e3f59858cbd4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 16:29:10 -0300 Subject: [PATCH 04/14] Fix sintax error --- crates/concrete_parser/src/grammar.lalrpop | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index ffc7507..df825e3 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -561,7 +561,7 @@ pub(crate) WhileStmt: ast::statements::WhileStmt = { pub(crate) ForStmt: ast::statements::ForStmt = { - "for" ? ";" ? ";" ? "{" "}" => { + "for" ";" ";" "{" "}" => { ast::statements::ForStmt { init, condition, From 53b460bda6af1d24bb743fff5691dfc97fe8eca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 16:42:47 -0300 Subject: [PATCH 05/14] Add for example --- examples/for.con | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 examples/for.con diff --git a/examples/for.con b/examples/for.con new file mode 100644 index 0000000..d8155e4 --- /dev/null +++ b/examples/for.con @@ -0,0 +1,15 @@ +mod Example { + fn main() -> i64 { + return sum_to(4); + } + + fn sum_to(limit: i64) -> i64 { + let mut result: i64 = 0; + + for let n: usize = 1; n <= limit; n = n + 1 { + result = result + n; + } + + return result; + } +} From 799e017770d60d60212db4110f40de03dbce30d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 16:52:05 -0300 Subject: [PATCH 06/14] Add temporary parenthesis as a work around --- crates/concrete_parser/src/grammar.lalrpop | 2 +- examples/for.con | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index df825e3..821534f 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -561,7 +561,7 @@ pub(crate) WhileStmt: ast::statements::WhileStmt = { pub(crate) ForStmt: ast::statements::ForStmt = { - "for" ";" ";" "{" "}" => { + "for" "(" ";" ";" ")" "{" "}" => { ast::statements::ForStmt { init, condition, diff --git a/examples/for.con b/examples/for.con index d8155e4..80a0fdb 100644 --- a/examples/for.con +++ b/examples/for.con @@ -6,7 +6,7 @@ mod Example { fn sum_to(limit: i64) -> i64 { let mut result: i64 = 0; - for let n: usize = 1; n <= limit; n = n + 1 { + for (let n: usize = 1; n <= limit; n = n + 1) { result = result + n; } From c8b2e43f277959ed96e0da46ce9715e27760487f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 17:15:14 -0300 Subject: [PATCH 07/14] Add for test --- crates/concrete_parser/src/lib.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index 2881a4a..ff2b940 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -150,6 +150,24 @@ mod ModuleName { fn parse_empty_fn() { let source = r##"mod MyMod { fn hello() {} +}"##; + let lexer = Lexer::new(source); + let parser = grammar::ProgramParser::new(); + parser.parse(lexer).unwrap(); + } + + #[test] + fn parse_for() { + let source = r##"mod MyMod { + fn hello() { + let mut result: i64 = 0; + + for (let n: usize = 1; n <= limit; n = n + 1) { + result = result + n; + } + + return result; + } }"##; let lexer = Lexer::new(source); let parser = grammar::ProgramParser::new(); From 21d34427ca1ae7c81232cb28033e940b3bda7370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 17:25:18 -0300 Subject: [PATCH 08/14] Use i64 for variable type --- examples/for.con | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/for.con b/examples/for.con index 80a0fdb..1f2ddd0 100644 --- a/examples/for.con +++ b/examples/for.con @@ -6,7 +6,7 @@ mod Example { fn sum_to(limit: i64) -> i64 { let mut result: i64 = 0; - for (let n: usize = 1; n <= limit; n = n + 1) { + for (let n: i64 = 1; n <= limit; n = n + 1) { result = result + n; } From 34d5d3b468dfed2c73bccf28267cc26b356f505a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 17:25:30 -0300 Subject: [PATCH 09/14] Add loop and while examples with for loop --- examples/for_loop.con | 17 +++++++++++++++++ examples/for_while.con | 17 +++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 examples/for_loop.con create mode 100644 examples/for_while.con diff --git a/examples/for_loop.con b/examples/for_loop.con new file mode 100644 index 0000000..90b2236 --- /dev/null +++ b/examples/for_loop.con @@ -0,0 +1,17 @@ +mod Example { + fn main() -> i64 { + return sum_to(4); + } + + fn sum_to(limit: i64) -> i64 { + let mut result: i64 = 0; + + let n: i64 = 1; + for (;;) { + result = result + n; + n = n + 1; + } + + return result; + } +} diff --git a/examples/for_while.con b/examples/for_while.con new file mode 100644 index 0000000..d3b536b --- /dev/null +++ b/examples/for_while.con @@ -0,0 +1,17 @@ +mod Example { + fn main() -> i64 { + return sum_to(4); + } + + fn sum_to(limit: i64) -> i64 { + let mut result: i64 = 0; + + let n: i64 = 1; + for (; n <= limit ;) { + result = result + n; + n = n + 1; + } + + return result; + } +} From 82419dc3350d32091046eb9bc4e59c9ba9069376 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 17:35:28 -0300 Subject: [PATCH 10/14] Add for_while and for_loop tests --- crates/concrete_parser/src/lib.rs | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index ff2b940..c33c18c 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -173,4 +173,44 @@ mod ModuleName { let parser = grammar::ProgramParser::new(); parser.parse(lexer).unwrap(); } + + #[test] + fn parse_for_while() { + let source = r##"mod MyMod { + fn hello() { + let mut result: i64 = 0; + + let n: i64 = 1; + for (; n <= limit ;) { + result = result + n; + n = n + 1; + } + + return result; + } +}"##; + let lexer = Lexer::new(source); + let parser = grammar::ProgramParser::new(); + parser.parse(lexer).unwrap(); + } + + #[test] + fn parse_for_loop() { + let source = r##"mod MyMod { + fn hello() { + let mut result: i64 = 0; + + let n: i64 = 1; + for (;;) { + result = result + n; + n = n + 1; + } + + return result; + } +}"##; + let lexer = Lexer::new(source); + let parser = grammar::ProgramParser::new(); + parser.parse(lexer).unwrap(); + } } From 628456f3a80743d006b638458e30ecfbc3538041 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 18:08:40 -0300 Subject: [PATCH 11/14] Add syntax sugar to "for" as while --- crates/concrete_parser/src/grammar.lalrpop | 8 ++++++++ crates/concrete_parser/src/lib.rs | 6 ++++++ examples/for_while.con | 6 ++++++ 3 files changed, 20 insertions(+) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index 821534f..a4dca41 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -568,5 +568,13 @@ pub(crate) ForStmt: ast::statements::ForStmt = { post, contents, } + }, + "for" "(" ")" "{" "}" => { + ast::statements::ForStmt { + init: None, + condition: Some(condition), + post: None, + contents, + } } } diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index c33c18c..22e9123 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -206,6 +206,12 @@ mod ModuleName { n = n + 1; } + n = 1; + for (n <= limit) { + result = result + n; + n = n + 1; + } + return result; } }"##; diff --git a/examples/for_while.con b/examples/for_while.con index d3b536b..80f9d3e 100644 --- a/examples/for_while.con +++ b/examples/for_while.con @@ -12,6 +12,12 @@ mod Example { n = n + 1; } + n = 1; + for (n <= limit) { + result = result + n; + n = n + 1; + } + return result; } } From d52ec32acc2e066f86d4a4c11f15f1f9a0d88269 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Tue, 9 Apr 2024 18:12:23 -0300 Subject: [PATCH 12/14] Add syntax sugar to "for" loop --- crates/concrete_parser/src/grammar.lalrpop | 8 ++++++++ crates/concrete_parser/src/lib.rs | 9 +++++++-- examples/for_loop.con | 5 +++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index a4dca41..9064787 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -576,5 +576,13 @@ pub(crate) ForStmt: ast::statements::ForStmt = { post: None, contents, } + }, + "for" "{" "}" => { + ast::statements::ForStmt { + init: None, + condition: None, + post: None, + contents, + } } } diff --git a/crates/concrete_parser/src/lib.rs b/crates/concrete_parser/src/lib.rs index 22e9123..c41ed18 100644 --- a/crates/concrete_parser/src/lib.rs +++ b/crates/concrete_parser/src/lib.rs @@ -186,6 +186,12 @@ mod ModuleName { n = n + 1; } + n = 1; + for (n <= limit) { + result = result + n; + n = n + 1; + } + return result; } }"##; @@ -206,8 +212,7 @@ mod ModuleName { n = n + 1; } - n = 1; - for (n <= limit) { + for { result = result + n; n = n + 1; } diff --git a/examples/for_loop.con b/examples/for_loop.con index 90b2236..fb57b29 100644 --- a/examples/for_loop.con +++ b/examples/for_loop.con @@ -11,6 +11,11 @@ mod Example { result = result + n; n = n + 1; } + + for { + result = result + n; + n = n + 1; + } return result; } From 1a98b4e11fbdf65e745b43eed5305ccb98637785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 10 Apr 2024 12:50:09 -0300 Subject: [PATCH 13/14] Fix syntax error --- crates/concrete_parser/src/grammar.lalrpop | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index 9064787..1adc088 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -500,7 +500,7 @@ pub(crate) Statement: ast::statements::Statement = { ";"? => ast::statements::Statement::Match(<>), ";"? => ast::statements::Statement::If(<>), ";"? => ast::statements::Statement::While(<>), - ":"? => ast::statements::Statement::For(<>), + ";"? => ast::statements::Statement::For(<>), ";" => ast::statements::Statement::Let(<>), ";" => ast::statements::Statement::Assign(<>), ";" => ast::statements::Statement::FnCall(<>), From d85898308bb79ae5cd512c29018db04eaaf80f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 10 Apr 2024 14:53:27 -0300 Subject: [PATCH 14/14] Add span to for statement --- crates/concrete_ast/src/statements.rs | 1 + crates/concrete_parser/src/grammar.lalrpop | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/concrete_ast/src/statements.rs b/crates/concrete_ast/src/statements.rs index 9a8ea6d..f836281 100644 --- a/crates/concrete_ast/src/statements.rs +++ b/crates/concrete_ast/src/statements.rs @@ -57,6 +57,7 @@ pub struct ForStmt { pub condition: Option, pub post: Option, pub contents: Vec, + pub span: Span, } #[derive(Clone, Debug, Eq, PartialEq)] diff --git a/crates/concrete_parser/src/grammar.lalrpop b/crates/concrete_parser/src/grammar.lalrpop index 1adc088..4e65311 100644 --- a/crates/concrete_parser/src/grammar.lalrpop +++ b/crates/concrete_parser/src/grammar.lalrpop @@ -561,28 +561,31 @@ pub(crate) WhileStmt: ast::statements::WhileStmt = { pub(crate) ForStmt: ast::statements::ForStmt = { - "for" "(" ";" ";" ")" "{" "}" => { + "for" "(" ";" ";" ")" "{" "}" => { ast::statements::ForStmt { init, condition, post, contents, + span: Span::new(lo, hi) } }, - "for" "(" ")" "{" "}" => { + "for" "(" ")" "{" "}" => { ast::statements::ForStmt { init: None, condition: Some(condition), post: None, contents, + span: Span::new(lo, hi) } }, - "for" "{" "}" => { + "for" "{" "}" => { ast::statements::ForStmt { init: None, condition: None, post: None, contents, + span: Span::new(lo, hi) } } }