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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#[macro_use]
extern crate thiserror;
pub mod annotation;
pub use self::annotation::*;
pub mod circuits;
pub use self::circuits::*;
pub mod chars;
pub use self::chars::*;
pub mod common;
pub use self::common::*;
pub mod errors;
pub use self::errors::*;
pub mod expression;
pub use self::expression::*;
pub mod functions;
pub use self::functions::*;
pub mod groups;
pub use self::groups::*;
pub mod imports;
pub use self::imports::*;
pub mod input;
pub use self::input::*;
pub mod program;
pub use self::program::*;
pub mod reducer;
pub use self::reducer::*;
pub mod statements;
pub use self::statements::*;
pub mod types;
pub use self::types::*;
mod node;
pub use node::*;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ast {
ast: Program,
}
impl Ast {
pub fn new(program: Program) -> Self {
Self { ast: program }
}
pub fn canonicalize(&mut self) -> Result<(), AstError> {
self.ast = ReconstructingDirector::new(Canonicalizer::default()).reduce_program(self.as_repr())?;
Ok(())
}
pub fn as_repr(&self) -> &Program {
&self.ast
}
pub fn into_repr(self) -> Program {
self.ast
}
pub fn to_json_string(&self) -> Result<String, AstError> {
Ok(serde_json::to_string_pretty(&self.ast)?)
}
pub fn to_json_file(&self, mut path: std::path::PathBuf, file_name: &str) -> Result<(), AstError> {
path.push(file_name);
let file = std::fs::File::create(path)?;
let writer = std::io::BufWriter::new(file);
serde_json::to_writer_pretty(writer, &self.ast)?;
Ok(())
}
pub fn from_json_string(json: &str) -> Result<Self, AstError> {
let ast: Program = serde_json::from_str(json)?;
Ok(Self { ast })
}
pub fn from_json_file(path: std::path::PathBuf) -> Result<Self, AstError> {
let data = std::fs::read_to_string(path)?;
Self::from_json_string(&data)
}
}
impl AsRef<Program> for Ast {
fn as_ref(&self) -> &Program {
&self.ast
}
}