Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ edition = "2024"

[dependencies]
clap = { version = "4.5.30", features = ["derive"] }
color-eyre = { version = "0.6", default-features = false }
thiserror = "2.0.17"

[[bin]]
name = "bin_creator"
path = "src/bin_creator.rs"

[[bin]]
name = "rmc_assemble"
path = "src/rmc_assemble.rs"
path = "src/assembler.rs"
20 changes: 15 additions & 5 deletions src/rmc_assemble.rs → src/assembler.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Parser;
use std::{collections::HashMap, fmt, fs, io, path::PathBuf};
use thiserror::Error;

use rusty_man_computer::value::Value;

Expand Down Expand Up @@ -55,7 +56,7 @@ impl fmt::Display for ParseErrorType {
}

#[derive(Debug)]
struct ParseError {
pub struct ParseError {
error: ParseErrorType,
line: usize,
}
Expand Down Expand Up @@ -206,10 +207,15 @@ fn generate_machine_code(lines: Vec<Line>) -> Result<Vec<Value>, &'static str> {
Ok(output)
}

enum AssemblerError {
#[derive(Error)]
pub enum AssemblerError {
#[error("{0}")]
ParseError(ParseError),
#[error("Machine code error: {0}")]
MachineCodeError(&'static str),
#[error("Failed to read input file: {0}")]
ReadError(io::Error),
#[error("Failed to write to output file: {0}")]
WriteError(io::Error),
}

Expand All @@ -224,7 +230,7 @@ impl fmt::Debug for AssemblerError {
}
}

fn assemble(program: &str) -> Result<Vec<Value>, AssemblerError> {
pub fn assemble(program: &str) -> Result<Vec<Value>, AssemblerError> {
let parsed = parse_assembly(program);
let mut valid_lines: Vec<Line> = Vec::new();
// Only go forward with non-empty lines, and raise an error if we encounter an invalid line
Expand Down Expand Up @@ -254,8 +260,7 @@ pub struct Args {
output: PathBuf,
}

fn main() -> Result<(), AssemblerError> {
let args = Args::parse();
fn assemble_from_file(args: Args) -> Result<(), AssemblerError> {
let program =
std::fs::read_to_string(args.program).map_err(|e| AssemblerError::ReadError(e))?;
let assembler_result = assemble(&program);
Expand All @@ -269,6 +274,11 @@ fn main() -> Result<(), AssemblerError> {
}
}

fn main() -> Result<(), AssemblerError> {
let args = Args::parse();
assemble_from_file(args)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading