michelson_ast/
program.rs

1use crate::formatter::format;
2use crate::ty::Ty;
3use crate::wrapped_instruction::WrappedInstruction;
4use std::string::ToString;
5
6pub struct Program {
7    pub storage: Ty,
8    pub parameter: Ty,
9    pub code: Vec<WrappedInstruction>,
10}
11
12impl ToString for Program {
13    fn to_string(&self) -> String {
14        format!(
15            r#"parameter {};
16storage {};
17code {{
18{}
19     }}"#,
20            self.parameter.to_string(),
21            self.storage.to_string(),
22            format(&self.code, 7)
23        )
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use crate::instruction::Instruction;
30    use crate::program::Program;
31    use crate::ty::Ty;
32    use crate::wrapped_instruction::WrappedInstruction;
33    #[test]
34    fn it_works() {
35        let program = Program {
36            storage: Ty::Unit,
37            parameter: Ty::Unit,
38            code: vec![
39                WrappedInstruction {
40                    comment: Some("=> Unit".to_string()),
41                    instruction: Instruction::Cdr,
42                },
43                WrappedInstruction {
44                    comment: Some("=> {} : Unit".to_string()),
45                    instruction: Instruction::Nil { ty: Ty::Operation },
46                },
47                WrappedInstruction {
48                    comment: Some("=> (Pair {} Unit)".to_string()),
49                    instruction: Instruction::Pair,
50                },
51            ],
52        };
53
54        let result = program.to_string();
55        println!("{}", result);
56        assert_eq!(result, String::from("storage unit;\nparameter unit;\ncode {\n       CDR; # => Unit\n       NIL operation; # => {} : Unit\n       PAIR; # => (Pair {} Unit)\n     }"));
57    }
58}