Skip to content

Commit

Permalink
fix empty blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
nbittich committed Aug 29, 2024
1 parent e95615e commit b046244
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
8 changes: 7 additions & 1 deletion adana-script/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,13 @@ where
F: Fn(&str) -> Res<Vec<Value>>,
{
move |s| {
preceded(tag_no_space("{"), terminated(&parser, tag_no_space("}")))(s)
map(
preceded(
tag_no_space("{"),
terminated(opt(&parser), tag_no_space("}")),
),
|exprs| if let Some(exprs) = exprs { exprs } else { vec![] },
)(s)
}
}

Expand Down
46 changes: 46 additions & 0 deletions adana-script/src/tests/empty_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::collections::BTreeMap;

use crate::compute;

use adana_script_core::primitive::Primitive;
#[test]
fn empty_while() {
let expr = r#"
x =1024
y = 3
while (x!=1024) {}
x
"#;
let mut ctx = BTreeMap::new();
let res = compute(expr, &mut ctx, "N/A").unwrap();
assert_eq!(Primitive::Int(1024), ctx["x"].read().unwrap().clone());
assert_eq!(Primitive::Int(1024), res);
}
#[test]
fn empty_if() {
let expr = r#"
x =1024
y = 3
if (x==1024) {}
if (x==1024) {}else {}
x
"#;
let mut ctx = BTreeMap::new();
let res = compute(expr, &mut ctx, "N/A").unwrap();
assert_eq!(Primitive::Int(1024), ctx["x"].read().unwrap().clone());
assert_eq!(Primitive::Int(1024), res);
}

#[test]
fn empty_for() {
let expr = r#"
x =1024
y = 3
for i in 0..x {}
x
"#;
let mut ctx = BTreeMap::new();
let res = compute(expr, &mut ctx, "N/A").unwrap();
assert_eq!(Primitive::Int(1024), ctx["x"].read().unwrap().clone());
assert_eq!(Primitive::Int(1024), res);
}
1 change: 1 addition & 0 deletions adana-script/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod builtin;
mod chaining;
mod drop;
mod dynload;
mod empty_block;
mod examples;
mod file;
mod foreach;
Expand Down

0 comments on commit b046244

Please sign in to comment.