-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vmtranslator: Extract
class AssemblyFile
to output
module
- Better logging and code comments
- Loading branch information
1 parent
5776a20
commit 0a033d8
Showing
5 changed files
with
79 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "output.h" | ||
|
||
#include <string_view> | ||
|
||
#include "absl/log/check.h" | ||
#include "absl/log/log.h" | ||
|
||
#include "commands.h" | ||
|
||
AssemblyFile::AssemblyFile(std::string_view path, bool source_is_multi_file) | ||
: file_(path.data()), source_is_multi_file_(source_is_multi_file) { | ||
QCHECK(file_.is_open()) << "Could not open output file: " << path; | ||
LOG(INFO) << "Output: " << path; | ||
|
||
// Bootstrap code. | ||
file_ << "@256\n" | ||
<< "D=A\n" | ||
<< "@SP\n" | ||
<< "M=D\n"; | ||
|
||
if (source_is_multi_file_) { | ||
file_ << CallCommand("Sys.init", 0, "END").ToAssembly(); | ||
// Although `Sys.init` is expected to enter an infinite loop, we still add | ||
// an infinite loop in case `Sys.init` returns. | ||
file_ << "@END\n" | ||
<< "(END)\n" | ||
<< "0;JMP\n"; | ||
} | ||
} | ||
|
||
AssemblyFile::~AssemblyFile() { | ||
if (!source_is_multi_file_) { | ||
file_ << "@END\n" | ||
<< "(END)\n" | ||
<< "0;JMP\n"; | ||
} | ||
file_.close(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef NAND2TETRIS_VMTRANSLATOR_OUTPUT_H_ | ||
#define NAND2TETRIS_VMTRANSLATOR_OUTPUT_H_ | ||
|
||
#include <fstream> | ||
#include <string_view> | ||
|
||
class AssemblyFile { | ||
public: | ||
AssemblyFile(std::string_view path, bool source_is_multi_file); | ||
~AssemblyFile(); | ||
|
||
template <class T> | ||
AssemblyFile &operator<<(const T &value) { | ||
file_ << value; | ||
return *this; | ||
} | ||
|
||
private: | ||
std::ofstream file_; | ||
bool source_is_multi_file_ = false; | ||
}; | ||
|
||
#endif // NAND2TETRIS_VMTRANSLATOR_OUTPUT_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters