diff --git a/adana-script/src/parser.rs b/adana-script/src/parser.rs index f9e09b1..3fbdfa4 100644 --- a/adana-script/src/parser.rs +++ b/adana-script/src/parser.rs @@ -889,7 +889,13 @@ where F: Fn(&str) -> Res>, { 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) } } diff --git a/adana-script/src/tests/empty_block.rs b/adana-script/src/tests/empty_block.rs new file mode 100644 index 0000000..842846b --- /dev/null +++ b/adana-script/src/tests/empty_block.rs @@ -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); +} diff --git a/adana-script/src/tests/mod.rs b/adana-script/src/tests/mod.rs index 1708f7b..cba5943 100644 --- a/adana-script/src/tests/mod.rs +++ b/adana-script/src/tests/mod.rs @@ -4,6 +4,7 @@ mod builtin; mod chaining; mod drop; mod dynload; +mod empty_block; mod examples; mod file; mod foreach;