1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! `rhasm` is a simple assembler for the Hack Assembly Language from the book
//! "*The Elements of Computing Systems*" by Noam Nisan and Shimon Schocken.
//! This project is the 6th project from their [Nand2Tetris course](https://www.nand2tetris.org/course).
//!
//! # Usage
//!
//! rhasm can be used as both a Rust library and as a standalone binary.
//!
//! ## As A Binary
//!
//!
//! To install the binary, you can run the following command:
//!
//! ```bash
//! cargo install rhasm
//! ```
//!
//! To then use the binary, you can run the following command:
//!
//! ```bash
//! rhasm <input_file> <output_file>
//! ```
//! ## As A Library
//!
//! To install rhasm as a library, you can add the following to your `Cargo.toml` file:
//!
//! ```toml
//! [dependencies]
//! rhasm = "0.1.0"
//! ```
//!
//! or use the following command:
//!
//! ```bash
//! cargo add rhasm
//! ```
//!
//! then import the library in your code:
//!
//! ```rust
//! use rhasm::*;
//! ```
//!
//! As a library rhasm exposes both the [`Assembler`] struct and the [`Instruction`] enum.
//!
//! By using the [`Assembler`] struct you can build an assembler instance and call the [`Assembler::advance_to_end`] method to assemble the entire bin file or use [`Assembler::advance_once`] to write to the file one line at a time.
//!
//! ```rust
//! use rhasm::*;
//! use std::fs::File;
//!
//! let in_file = File::open("sample.asm").unwrap();
//! let out_file = File::create("sample.hack").unwrap();
//! let mut assembler_result = Assembler::build(&in_file, &out_file);
//! if let Ok(mut assembler) = assembler_result {
//! assembler.advance_once();
//! assembler.advance_to_end();
//! }
//! ```
//!
//! Alternatively you can call the [`Assembler::get_next_encoded_instruction`] method to return the next encoded instruction as a string that you can use as you see fit.
//!
//! ```rust
//! use rhasm::*;
//! use std::fs::File;
//!
//! let in_file = File::open("sample.asm").unwrap();
//! let out_file = File::create("sample.hack").unwrap();
//! let mut assembler = Assembler::build(&in_file, &out_file).unwrap();
//! let mut buffer = String::new();
//!
//! while let Some(encoded_instruction) = assembler.get_next_encoded_instruction() {
//! buffer.push_str(&encoded_instruction);
//! }
//! // Do something with the buffer
//! ```
//!
// Define our library structure here
// Here we declare what parts of the library are exposed to the user
// Namely the Assembler Struct and the Instruction Enum
pub use ;