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
use crate::ast::Stmt;
use crate::compiler::Compiler;
use crate::error::ParseError;
use crate::instruction::InstructionWithDebug;
use crate::interner::{Interner, Symbol};
use crate::parser::parse;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
pub struct Program {
    interner: Interner,
    instructions: Vec<InstructionWithDebug>,
}

impl Program {
    pub fn from_ast(program: &[Stmt], interner: Interner) -> Self {
        Self {
            interner,
            instructions: Compiler::new().compile(program),
        }
    }

    pub fn from_source(source: &str) -> Result<Self, ParseError> {
        let mut interner = Interner::new();
        let ast = parse(source, &mut interner)?;

        Ok(Self::from_ast(&ast, interner))
    }

    #[inline]
    pub fn instructions(&self) -> &[InstructionWithDebug] {
        &self.instructions
    }

    #[inline]
    pub fn resolve(&self, symbol: Symbol) -> Option<&str> {
        self.interner.resolve(symbol)
    }
}

#[cfg(test)]
mod tests {
    use super::Program;
    use pretty_assertions::assert_eq;

    #[test]
    fn test_serde() {
        let prev = Program::from_source("만약 1 { ㅇ(1+2*3, 4); } 그외 { 123; }").unwrap();
        let bytes = bincode::serialize(&prev).unwrap();
        let cur = bincode::deserialize::<Program>(&bytes).unwrap();
        assert_eq!(prev, cur);
    }
}