From 0d24c782e37ffe4841c5c8c52fe7b3c5517224f2 Mon Sep 17 00:00:00 2001 From: Andreev Gregory Date: Sat, 26 Apr 2025 23:02:17 +0300 Subject: [PATCH] Daily update. Fixing some bugs in mtgott --- mtgott/src/parser.rs | 10 ++-- mtgott/src/runtime.rs | 6 ++- mtgott/tests/april_24.rs | 101 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 108 insertions(+), 9 deletions(-) diff --git a/mtgott/src/parser.rs b/mtgott/src/parser.rs index 8b814b8..b2e94bf 100644 --- a/mtgott/src/parser.rs +++ b/mtgott/src/parser.rs @@ -428,9 +428,9 @@ fn fix_whitespaces_in_element(subels: &mut Vec) { should_ignore_gap = false; /* We handle trailing whitespaces case here */ while let Some(&ch) = res.last() { - if is_lnspace(ch as char) { res. pop(); } else { break } + if is_lnspace(ch as char) { res.pop(); } else { break } } - } else if p - line_bg < min_offset { + } else if p - line_bg < min_offset && !should_ignore_gap{ continue } res.push(ch); @@ -453,9 +453,7 @@ impl<'a> Parser<'a> { let mut tp1 = self.p; let fin_static = |p: &Parser, tp1: usize, res: &mut Vec| { - if tp1 < p.p { - res.push(SubElement::Static(String::from(&p.text[tp1..p.p]))) - } + res.push(SubElement::Static(String::from(&p.text[tp1..p.p]))) }; /* Fixes whitespaces in static sub-elements */ @@ -802,7 +800,7 @@ impl<'a> Parser<'a> { Ok(Some(Expression::Lambda(NewLambdaExpression{ local_var_array: used_by_lambda.iter().map( |iig| -> usize { if *iig == argument_iig { - usize::MAX + usize::MAX // We assume usize::MAX never appears as an actual index in used_local_consts } else { used_local_consts.iter().position(|iig_of_this| iig_of_this == iig).unwrap() } diff --git a/mtgott/src/runtime.rs b/mtgott/src/runtime.rs index 743e852..41e8195 100644 --- a/mtgott/src/runtime.rs +++ b/mtgott/src/runtime.rs @@ -306,11 +306,13 @@ impl<'m> RuntimeEnv<'m> { let local_var_array: Vec = captured.iter().map(|oid| { if *oid == usize::MAX { Value::Int(0) } else { local[*oid].clone() } }).collect(); - let where_arg_goes = captured.iter().position(|oid| *oid == usize::MAX).unwrap(); + let where_arg_goes = captured.iter().position(|oid| *oid == usize::MAX); let body_expr_id: usize = *body_expr_id; Ok(Value::Fn(Rc::new(move |re, x: Value, counter| -> Result { let mut local_var_array: Vec = local_var_array.clone(); - local_var_array[where_arg_goes] = x; + if let Some(arg_local_ind) = where_arg_goes { + local_var_array[arg_local_ind] = x; + } re.execute_expression(&re.mtgott.source_expressions[body_expr_id], &local_var_array, counter - 1) }))) } diff --git a/mtgott/tests/april_24.rs b/mtgott/tests/april_24.rs index 612a806..934c572 100644 --- a/mtgott/tests/april_24.rs +++ b/mtgott/tests/april_24.rs @@ -1,7 +1,7 @@ extern crate mtgott; use mtgott::dirsearch::*; use mtgott::runtime::*; - +use std::rc::Rc; macro_rules! assert_gave { ($a:expr, $str:literal) => { @@ -123,3 +123,102 @@ fn t010(){ let r = get_root_html_from_dir_text_html(i).unwrap(); assert_gave!(r.render(Value::Int(0), "index", 50).unwrap(), "12"); } + +#[test] +fn t011(){ + let i = MtgottDirContent{mtgott: vec![ + FileWithPath{v_path: "file".into(), text: "{@el a @} {{a[1]}} {@} ".into()} + ], imtgott: vec![ + FileWithPath{v_path: "index".into(), text: "{[file.el $]}".into()} + ], plain: vec![]}; + let r = get_root_html_from_dir_text_html(i).unwrap(); + assert_gave!(r.render( + Value::Arr(Rc::new(vec![Value::Int(10), Value::Int(20), Value::Int(30)])), + "index", 50 + ).unwrap(), "20"); +} + + +#[test] +fn t012(){ + let i = MtgottDirContent{mtgott: vec![ + FileWithPath{v_path: "file".into(), text: "{@el a @} {{a[1]}} {@} ".into()} + ], imtgott: vec![ + FileWithPath{v_path: "index".into(), text: "{[file[\"el\"] $]}".into()} + ], plain: vec![]}; + let r = get_root_html_from_dir_text_html(i).unwrap(); + assert_gave!(r.render( + Value::Arr(Rc::new(vec![Value::Int(10), Value::Int(20), Value::Int(30)])), + "index", 50 + ).unwrap(), "20"); +} + +#[test] +fn t013(){ + let i = MtgottDirContent{mtgott: vec![ + ], imtgott: vec![ + FileWithPath{v_path: "index".into(), text: "{{}} {{}}".into()} + ], plain: vec![]}; + let r = get_root_html_from_dir_text_html(i).unwrap(); + assert_gave!(r.render( + Value::Int(0), + "index", 50 + ).unwrap(), " "); +} + +#[test] +fn t014(){ + let i = MtgottDirContent{mtgott: vec![ + ], imtgott: vec![ + FileWithPath{v_path: "index".into(), text: " {{ $[0 ]}} {{$ [1]}} {{$[ 2]}} ".into()} + ], plain: vec![]}; + let r = get_root_html_from_dir_text_html(i).unwrap(); + assert_gave!(r.render( + Value::Arr(Rc::new(vec![Value::Int(10), Value::Int(20), Value::Int(30)])), + "index", 50 + ).unwrap(), "10 20 30"); +} + +#[test] +fn t015(){ + let i = MtgottDirContent{mtgott: vec![ + FileWithPath{v_path: "help".into(), text: " {@www $ a b@} {{$[a]}} {{$[b]}} {@}".into()} + ], imtgott: vec![ + FileWithPath{v_path: "index".into(), text: " {[help.www $ 2 1 ]}".into()} + ], plain: vec![]}; + let r = get_root_html_from_dir_text_html(i).unwrap(); + assert_gave!(r.render( + Value::Arr(Rc::new(vec![Value::Int(10), Value::Int(11), Value::Int(12)])), + "index", 50 + ).unwrap(), "12 11"); +} + + +#[test] +fn t016(){ + let i = MtgottDirContent{mtgott: vec![ + FileWithPath{v_path: "__".into(), text: "".into()} + ], imtgott: vec![ + FileWithPath{v_path: "index".into(), text: " {{ (x:x) 10 }}".into()} + ], plain: vec![]}; + let r = get_root_html_from_dir_text_html(i).unwrap(); + assert_gave!(r.render( + Value::Int(0), + "index", 50 + ).unwrap(), "10"); +} + + +#[test] +fn t017(){ + let i = MtgottDirContent{mtgott: vec![ + FileWithPath{v_path: "__".into(), text: "".into()} + ], imtgott: vec![ + FileWithPath{v_path: "index".into(), text: " {{ (x:y:x) 0 1 }}".into()} + ], plain: vec![]}; + let r = get_root_html_from_dir_text_html(i).unwrap(); + assert_gave!(r.render( + Value::Int(0), + "index", 50 + ).unwrap(), "0"); +}