1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Greetings traveler, this is the nonexistent api wrapper for the citadel toolchain

pub use citadel_frontend as frontend;
pub use citadel_middleend as middleend;
pub use citadel_backend as backend;

use std::{fs, io, marker::PhantomData, path::PathBuf};

use citadel_backend::experimental::api::{Backend, Target};

#[macro_export]
macro_rules! compile {
    ($backend:expr, $ir_stream:expr) => {{
        use citadel_api::Output;
        use citadel_api::backend::experimental::api::Backend;

        Output::new($backend, $backend.generate($ir_stream))
    }};
}

pub struct Output<T, B>
where
    T: Target,
    B: Backend<Target = T>,
{
    pub backend: B,
    pub stream: B::Output,
    phantom: PhantomData<T>,
}

impl<T, B> Output<T, B>
where
    T: Target,
    B: Backend<Target = T>,
{
    pub fn new(backend: B, stream: B::Output) -> Self {
        Self {
            backend,
            stream,
            phantom: PhantomData::default(),
        }
    }

    pub fn to_file(self, path: PathBuf) -> io::Result<()> {
        if let Some(res) = self.backend.to_file() {
            return res;
        }
        let contents: String = self
            .stream
            .into_iter()
            .map(|elem| elem.to_string())
            .collect::<Vec<String>>()
            .join("\n");
        fs::write(path, contents)
    }
}