Hello,
I am currently in the process of building an assembler for the nand2tetris Hack platform specification. It has been quite challenging for me, but I am happy to have gotten this far!
The program accepts a filename command line argument and reads the file.
For each symbolic command, it carries out the following tasks:
-
Parses the symbolic command into its underlying fields (completed)
-
For each field, generate the corresponding bits into machine language (incomplete)
-
Assemble the binary codes into a complete machine instruction (incomplete)
I am currently working towards step 2. The assembly code has predefined instruction types and symbols (tables.json), however there are also user defined variables and symbols that must be collected before binary translation can occur. To do this, I must make two passes over my assembly code:
For the first pass, I need to remove comments and white spaces. While doing so, this is a great opportunity to collect user defined variables and symbols.
On the second pass, I plan to map each command to its binary translation. (There may be more that I need to do at this step, but for now, I want to focus on the building the first pass.)
I have an idea, and would love to hear your input. I very much open to other ideas as well!
Ok...
For the sake of efficiency, I want to use BufReader
:
lib.rs
pub fn run(filename: String) -> std::io::Result<()> {
let assembly = File::open(filename)?;
let mut buffered = BufReader::new(assembly);
let mut contents = String::new();
buffered.read_to_string(&mut contents)?;
// Return a file with comments and white space removed
// Collect user defined symbols and variables
let filtered_contents = first_pass(contents)?;
// Parser commands and generate binary translation
// etc
second_pass(filtered_contents)?;
// Possibly more code here
Ok(())
}
As you can see, I would like first_pass()
to perform two tasks:
- Filter out comments and white space
- Collect user defined symbols and variables
I am having trouble understanding how to utilize BufReader
and BufWriter
inside of first_pass
.
I started playing around with BufWriter
and BufReader
inside first_pass()
in lib.rs