pipeline_script/core/
app.rs1use crate::core::engine::Engine;
2use crate::plugin::Plugin;
3use crate::postprocessor::Visitor;
4use std::ffi::c_void;
5use std::path::{Path, PathBuf};
6
7#[macro_export]
48macro_rules! app {
49 ($entry_file:expr, [$($plugin:expr),* $(,)?]) => {
50 {
51 let app = $crate::core::app::App::new()
52 .set_entry_file($entry_file);
53
54 $(
55 let app = app.add_plugin($plugin);
56 )*
57
58 app
59 }
60 };
61}
62
63pub struct App {
64 engine: Engine,
65 path: PathBuf,
66}
67impl App {
68 pub fn new() -> Self {
69 Self {
70 engine: Engine::default(),
71 path: PathBuf::default(),
72 }
73 }
74 #[allow(unused)]
75 pub fn register_external_function(mut self, name: &str, func: *mut c_void) -> Self {
76 self.engine.register_external_function(name, func);
77 self
78 }
79 pub fn set_test_llvm(mut self, test: bool) -> Self {
80 self.engine.set_test_llvm(test);
81 self
82 }
83 pub fn add_test_llvm_file(mut self, path: impl AsRef<Path>) -> Self {
84 self.engine.add_test_llvm_file(path);
85 self
86 }
87 #[allow(unused)]
88 pub fn register_visitor(mut self, visitor: impl Visitor + 'static) -> Self {
89 self.engine.register_visitor(visitor);
90 self
91 }
92 pub fn add_plugin(mut self, plugin: impl Plugin) -> Self {
93 plugin.apply(&mut self.engine);
94 self
95 }
96 pub fn run(&mut self) {
97 if self.engine.test_llvm {
98 for file in self.engine.test_llvm_files.iter() {
99 self.engine.run_llvm_file(file);
100 }
101 } else {
102 self.engine.run_file(self.path.clone())
103 }
104 }
105 pub fn set_entry_file(mut self, path: impl AsRef<Path>) -> Self {
106 self.path = path.as_ref().to_path_buf();
107 self
108 }
109}
110
111impl Default for App {
112 fn default() -> Self {
113 Self::new()
114 }
115}
116
117#[cfg(test)]
119mod tests {
120 use super::*;
121 use crate::plugin::builtin::BuiltinPlugin;
122 use crate::plugin::format_string::FormatStringPlugin;
123 use crate::plugin::math::MathPlugin;
124
125 #[test]
127 fn test_app_macro_basic() {
128 let _app = app!("test.ppl", [BuiltinPlugin]);
130
131 assert!(true); }
134
135 #[test]
137 fn test_app_macro_multiple_plugins() {
138 let _app = app!("main.ppl", [BuiltinPlugin, MathPlugin, FormatStringPlugin]);
140
141 assert!(true); }
144
145 #[test]
147 fn test_app_macro_empty_plugins() {
148 let _app = app!("empty.ppl", []);
150
151 assert!(true); }
154
155 #[test]
157 fn test_app_macro_with_chaining() {
158 let _app = app!("chain.ppl", [BuiltinPlugin, MathPlugin])
160 .set_test_llvm(true)
161 .add_test_llvm_file("test.ll");
162
163 assert!(true); }
166
167 #[test]
169 fn test_app_macro_with_variable_entry() {
170 let entry_file = "variable.ppl";
171
172 let _app = app!(entry_file, [BuiltinPlugin]);
174
175 assert!(true); }
178
179 #[test]
181 fn test_app_macro_type_inference() {
182 let _app: App = app!("type_test.ppl", [BuiltinPlugin]);
184
185 assert!(true);
187 }
188
189 #[test]
191 fn test_app_macro_trailing_comma() {
192 let _app = app!("trailing.ppl", [BuiltinPlugin, MathPlugin,]);
194
195 assert!(true);
197 }
198}