Skip to content

Commit

Permalink
Emit code to designated file
Browse files Browse the repository at this point in the history
  • Loading branch information
jjcnn authored and bitwalker committed Sep 7, 2023
1 parent 96eb1d0 commit 61b1418
Showing 1 changed file with 9 additions and 44 deletions.
53 changes: 9 additions & 44 deletions tools/midenc/src/compiler/emitter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::{fs::File, io::prelude::*, path::Path};
use miden_assembly::ast::{self as masm};

/// The emitter for MASM output.
Expand All @@ -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::Output> {
self.writer.write!("{}", input);
}

/// Implementation of Pass::chain
///
/// # Panics
/// Panics if called. No chaining is possible after the emitter.
fn chain<P>(self, pass: P) -> Chain<Self, P>
where
Self: Sized,
P: for Pass<Input = Self::Output>
{
panic!("Attempting to chain a pass after the emitter.");
}
fn write_ast_to_file<P: AsRef<Path>>(ast: &MASMAst, path: P) -> io::Result<()> {
let mut file = File::create(path);
file.write_fmt(format_args!("{}", ast))
}

0 comments on commit 61b1418

Please sign in to comment.