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

For loop syntax #102

Merged
merged 15 commits into from
Apr 18, 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: 4 additions & 3 deletions crates/concrete_ast/src/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ pub struct Binding {

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ForStmt {
pub name: Ident,
pub from: Expression,
pub to: Expression,
pub init: Option<LetStmt>,
pub condition: Option<Expression>,
pub post: Option<AssignStmt>,
pub contents: Vec<Statement>,
pub span: Span,
}

#[derive(Clone, Debug, Eq, PartialEq)]
Expand Down
32 changes: 32 additions & 0 deletions crates/concrete_parser/src/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ pub(crate) Statement: ast::statements::Statement = {
<MatchExpr> ";"? => ast::statements::Statement::Match(<>),
<IfExpr> ";"? => ast::statements::Statement::If(<>),
<WhileStmt> ";"? => ast::statements::Statement::While(<>),
<ForStmt> ";"? => ast::statements::Statement::For(<>),
<LetStmt> ";" => ast::statements::Statement::Let(<>),
<AssignStmt> ";" => ast::statements::Statement::Assign(<>),
<FnCallOp> ";" => ast::statements::Statement::FnCall(<>),
Expand Down Expand Up @@ -574,3 +575,34 @@ pub(crate) WhileStmt: ast::statements::WhileStmt = {
}
}
}


pub(crate) ForStmt: ast::statements::ForStmt = {
<lo:@L> "for" "(" <init:LetStmt?> ";" <condition:Expression?> ";" <post:AssignStmt?> ")" "{" <contents:StatementList> "}" <hi:@R> => {
ast::statements::ForStmt {
init,
condition,
post,
contents,
span: Span::new(lo, hi)
}
},
<lo:@L> "for" "(" <condition:Expression> ")" "{" <contents:StatementList> "}" <hi:@R> => {
ast::statements::ForStmt {
init: None,
condition: Some(condition),
post: None,
contents,
span: Span::new(lo, hi)
}
},
<lo:@L> "for" "{" <contents:StatementList> "}" <hi:@R> => {
ast::statements::ForStmt {
init: None,
condition: None,
post: None,
contents,
span: Span::new(lo, hi)
}
}
}
69 changes: 69 additions & 0 deletions crates/concrete_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,75 @@ 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();
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;
}

n = 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;
}

for {
result = result + n;
n = n + 1;
}

return result;
}
}"##;
let lexer = Lexer::new(source);
let parser = grammar::ProgramParser::new();
Expand Down
15 changes: 15 additions & 0 deletions examples/for.con
Original file line number Diff line number Diff line change
@@ -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: i64 = 1; n <= limit; n = n + 1) {
result = result + n;
}

return result;
}
}
22 changes: 22 additions & 0 deletions examples/for_loop.con
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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;
}

for {
result = result + n;
n = n + 1;
}

return result;
}
}
23 changes: 23 additions & 0 deletions examples/for_while.con
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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;
}

n = 1;
for (n <= limit) {
result = result + n;
n = n + 1;
}

return result;
}
}
Loading