From 61b141838c71afc840b46adb8bbc1ddcc85f4e2a Mon Sep 17 00:00:00 2001 From: Jacob Johannsen Date: Fri, 1 Sep 2023 15:33:49 +0200 Subject: [PATCH] Emit code to designated file --- tools/midenc/src/compiler/emitter.rs | 53 +++++----------------------- 1 file changed, 9 insertions(+), 44 deletions(-) diff --git a/tools/midenc/src/compiler/emitter.rs b/tools/midenc/src/compiler/emitter.rs index 59eb9b12e..0b3baaa61 100644 --- a/tools/midenc/src/compiler/emitter.rs +++ b/tools/midenc/src/compiler/emitter.rs @@ -1,3 +1,4 @@ +use std::{fs::File, io::prelude::*, path::Path}; use miden_assembly::ast::{self as masm}; /// The emitter for MASM output. @@ -7,52 +8,16 @@ pub enum MASMAst { Module(masm::ModuleAst), } -pub struct MASMEmitter { - writer: BufferWriter, -} - -impl miden_diagnostics::Emitter for MASMEmitter { - - fn buffer(&self) -> Buffer { - self.writer.buffer() - } - - fn print(&self, buffer: Buffer) -> std::io::Result<()> { - self.writer.print(&buffer) - } -} - -impl MASMEmitter { - - pub fn new () -> Self { - Self { - writer: BufferWriter::new(ColorChoice::Auto), +impl fmt::Display for MASMAst { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Program(p) => write!("{}", p), + Module(m) => write!("{}", m), } } } -impl Pass for Emitter { - type Input = MASMAst; - type Output = (); - - /// Runs the emitter on the AST - /// - /// Errors should be reported via the registered error handler, - /// Passes should return `Err` to signal that the pass has failed - /// and compilation should be aborted - fn run(&mut self, input: Self::Input) -> anyhow::Result { - self.writer.write!("{}", input); - } - - /// Implementation of Pass::chain - /// - /// # Panics - /// Panics if called. No chaining is possible after the emitter. - fn chain

(self, pass: P) -> Chain - where - Self: Sized, - P: for Pass - { - panic!("Attempting to chain a pass after the emitter."); - } +fn write_ast_to_file>(ast: &MASMAst, path: P) -> io::Result<()> { + let mut file = File::create(path); + file.write_fmt(format_args!("{}", ast)) }