Add format (roundtrip) button
This commit is contained in:
+33
@@ -1,5 +1,7 @@
|
|||||||
use eframe::egui;
|
use eframe::egui;
|
||||||
|
use myc::ast::compiler::emitter;
|
||||||
use myc::ast::environment::Environment;
|
use myc::ast::environment::Environment;
|
||||||
|
use myc::ast::parser::Parser;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
fn main() -> eframe::Result {
|
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) {
|
fn save(&mut self) {
|
||||||
if let Some(path) = &self.current_example_path {
|
if let Some(path) = &self.current_example_path {
|
||||||
match std::fs::write(path, &self.source_code) {
|
match std::fs::write(path, &self.source_code) {
|
||||||
@@ -334,6 +355,10 @@ impl eframe::App for CompilerApp {
|
|||||||
self.dump_ast();
|
self.dump_ast();
|
||||||
ui.close();
|
ui.close();
|
||||||
}
|
}
|
||||||
|
if ui.button("Format (Roundtrip)").clicked() {
|
||||||
|
self.roundtrip();
|
||||||
|
ui.close();
|
||||||
|
}
|
||||||
ui.separator();
|
ui.separator();
|
||||||
ui.checkbox(&mut self.debug_enabled, "Debug Mode");
|
ui.checkbox(&mut self.debug_enabled, "Debug Mode");
|
||||||
});
|
});
|
||||||
@@ -410,6 +435,14 @@ impl eframe::App for CompilerApp {
|
|||||||
self.dump_ast();
|
self.dump_ast();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ui
|
||||||
|
.button("🔄 Format")
|
||||||
|
.on_hover_text("Format/Roundtrip source code")
|
||||||
|
.clicked()
|
||||||
|
{
|
||||||
|
self.roundtrip();
|
||||||
|
}
|
||||||
|
|
||||||
ui.separator();
|
ui.separator();
|
||||||
|
|
||||||
ui.toggle_value(&mut self.debug_enabled, "🐛 Debug");
|
ui.toggle_value(&mut self.debug_enabled, "🐛 Debug");
|
||||||
|
|||||||
Reference in New Issue
Block a user