Skip to content

Commit

Permalink
feat: add while loops to the formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Yag000 committed Aug 14, 2023
1 parent 16f397f commit 99f2265
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/formatter/formatter_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,42 @@ let a = 10;
let expected = r#"puts("Hello, Monkey!");
let arr = [1, 2, 3];
let length = len(arr);
"#;

assert_eq!(format(input), expected);
}

#[test]
fn test_while() {
let input = r#"
let a = 1;
while (a<3){
let a = a + 1,
puts(21);
}
let a = fn (x){
let a = 1;
while (x > 0){
let a = a * 2;
}
a
};
a(12);
"#;

let expected = r#"let a = 1;
while (a < 3) {
let a = a + 1;
puts(21);
}
let a = fn (x) {
let a = 1;
while (x > 0) {
let a = a * 2;
}
a
};
a(12);
"#;

assert_eq!(format(input), expected);
Expand Down
10 changes: 10 additions & 0 deletions src/formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ impl Formatter {
self.push(";");
}
}
Statement::While(wh) => {
self.push("while (");
self.visit_expression(&wh.condition);
self.push(") {\n");
self.indent += 1;
self.visit_block_statement(&wh.body);
self.indent -= 1;
self.push_indent();
self.push("}");
}
}
self.push("\n");
self.last_expression = None;
Expand Down

0 comments on commit 99f2265

Please sign in to comment.