diff --git a/src/main.rs b/src/main.rs index 990bdfc..40ae6a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ use eframe::egui; +use myc::ast::compiler::emitter; use myc::ast::environment::Environment; +use myc::ast::parser::Parser; use std::path::PathBuf; fn main() -> eframe::Result { @@ -171,6 +173,25 @@ impl CompilerApp { } } + fn roundtrip(&mut self) { + let mut parser = Parser::new(&self.source_code); + let ast = parser.parse_expression(); + if parser.diagnostics.has_errors() { + let errors: Vec<_> = parser + .diagnostics + .items + .iter() + .map(|d| d.message.clone()) + .collect(); + self.output_log = format!("Roundtrip failed — parse errors:\n{}", errors.join("\n")); + self.active_tab = AppTab::Output; + return; + } + self.source_code = emitter::emit(&ast); + self.output_log = "Roundtrip complete — source reformatted.".to_string(); + self.active_tab = AppTab::Output; + } + fn save(&mut self) { if let Some(path) = &self.current_example_path { match std::fs::write(path, &self.source_code) { @@ -334,6 +355,10 @@ impl eframe::App for CompilerApp { self.dump_ast(); ui.close(); } + if ui.button("Format (Roundtrip)").clicked() { + self.roundtrip(); + ui.close(); + } ui.separator(); ui.checkbox(&mut self.debug_enabled, "Debug Mode"); }); @@ -410,6 +435,14 @@ impl eframe::App for CompilerApp { self.dump_ast(); } + if ui + .button("🔄 Format") + .on_hover_text("Format/Roundtrip source code") + .clicked() + { + self.roundtrip(); + } + ui.separator(); ui.toggle_value(&mut self.debug_enabled, "🐛 Debug");