mers_lib/program/parsed/
include_mers.rs

1use std::sync::{Arc, Mutex};
2
3use crate::{
4    data::{self, Data},
5    errors::{CheckError, EColor, SourceRange},
6    info,
7    parsing::Source,
8    program,
9};
10
11use super::{CompInfo, MersStatement};
12
13#[derive(Debug)]
14pub struct IncludeMers {
15    pub pos_in_src: SourceRange,
16    pub include: Box<dyn MersStatement>,
17    pub inner_src: Source,
18}
19impl MersStatement for IncludeMers {
20    fn has_scope(&self) -> bool {
21        true
22    }
23    fn compile_custom(
24        &self,
25        info: &mut info::Info<super::Local>,
26        comp: CompInfo,
27    ) -> Result<Box<dyn program::run::MersStatement>, CheckError> {
28        let mut inc_info = info.duplicate();
29        inc_info.global.enable_hooks = false;
30        let compiled: Arc<Box<dyn crate::program::run::MersStatement>> =
31            match self.include.compile(&mut inc_info, comp) {
32                Ok(v) => Arc::new(v),
33                Err(e) => {
34                    return Err(CheckError::new()
35                        .src(vec![(
36                            self.pos_in_src.clone(),
37                            Some(EColor::HashIncludeErrorInIncludedFile),
38                        )])
39                        .msg(vec![(
40                            "Error in #include! (note: inner errors may refer to a different file)"
41                                .to_owned(),
42                            Some(EColor::HashIncludeErrorInIncludedFile),
43                        )])
44                        .err_with_diff_src(e))
45                }
46            };
47        let compiled2 = Arc::clone(&compiled);
48        Ok(Box::new(program::run::chain::Chain {
49            pos_in_src: self.pos_in_src.clone(),
50            first: Box::new(program::run::value::Value {
51                pos_in_src: self.pos_in_src.clone(),
52                val: Data::empty_tuple(),
53            }),
54            chained: Box::new(program::run::function::Function {
55                pos_in_src: self.pos_in_src.clone(),
56                func_no_info: data::function::Function {
57                    info: info::Info::neverused(),
58                    info_check: Arc::new(Mutex::new(info::Info::neverused())),
59                    fixed_type: None,
60                    fixed_type_out: Arc::new(Mutex::new(None)),
61                    out: Ok(Arc::new(move |_, i| {
62                        compiled.check(&mut i.duplicate(), None)
63                    })),
64                    run: Arc::new(move |_, i| compiled2.run(&mut i.duplicate())),
65                    inner_statements: None,
66                },
67            }),
68            as_part_of_include: Some(self.inner_src.clone()),
69        }))
70    }
71    fn source_range(&self) -> SourceRange {
72        self.pos_in_src.clone()
73    }
74    fn inner_statements(&self) -> Vec<&dyn MersStatement> {
75        vec![]
76    }
77    fn as_any(&self) -> &dyn std::any::Any {
78        self
79    }
80}