main.cpp: Write the output file in Logisim format
This commit is contained in:
81
src/main.cpp
81
src/main.cpp
@ -3,6 +3,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <boost/program_options.hpp>
|
||||
#include <boost/format.hpp>
|
||||
#include <iomanip>
|
||||
#include "lexer/Lexer.h"
|
||||
#include "ast/Node.h"
|
||||
@ -12,6 +13,24 @@
|
||||
|
||||
namespace po = boost::program_options;
|
||||
|
||||
auto read(const std::string& input_file, std::string &input_string) -> bool {
|
||||
std::ifstream ifs;
|
||||
ifs.open(input_file, std::ios_base::in);
|
||||
if (!ifs) {
|
||||
std::cout << "Failed to open input stream!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
while (!ifs.eof()) {
|
||||
std::string line;
|
||||
getline(ifs, line);
|
||||
input_string += line + '\n';
|
||||
}
|
||||
|
||||
ifs.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
auto lex(std::string_view input_string, std::vector<Token> &tokens) -> bool {
|
||||
Lexer lexer(input_string);
|
||||
while (true) {
|
||||
@ -35,13 +54,43 @@ auto parse(std::vector<Token> &tokens) -> std::unique_ptr<Node> {
|
||||
return std::move(parser.parse());
|
||||
}
|
||||
|
||||
auto write(const std::string &output_file, const std::vector<std::string> &output_string) -> bool {
|
||||
if (output_string.size() > 255) {
|
||||
std::cout << "Program too large!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::ofstream ofs;
|
||||
ofs.open(output_file, std::ios_base::out);
|
||||
if (!ofs) {
|
||||
std::cout << "Failed to open output stream!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
ofs << "v3.0 hex words addressed";
|
||||
for (uint32_t i = 0; i <= 255; ++i) {
|
||||
if (i % 16 == 0) {
|
||||
ofs << std::endl;
|
||||
// Print Address
|
||||
std::string address = (boost::format("%x") % i).str();
|
||||
ofs << (address == "0" ? "00" : address) << ": ";
|
||||
}
|
||||
|
||||
if (i < output_string.size()) {
|
||||
ofs << output_string[i] << " ";
|
||||
} else {
|
||||
ofs << "00 ";
|
||||
}
|
||||
}
|
||||
|
||||
ofs.flush();
|
||||
ofs.close();
|
||||
return true;
|
||||
}
|
||||
|
||||
auto main(int argc, char **argv) -> int {
|
||||
// Argument parsing straight from the Boost manual: https://www.boost.org/doc/libs/1_60_0/doc/html/program_options/tutorial.html
|
||||
|
||||
// Declare the supported options.
|
||||
po::options_description desc("Allowed options");
|
||||
|
||||
// TODO: What the fuck is this syntax?
|
||||
desc.add_options()
|
||||
("help,h", "Show this help message")
|
||||
("input,i", po::value<std::string>(), "Input file")
|
||||
@ -68,19 +117,11 @@ auto main(int argc, char **argv) -> int {
|
||||
std::cout << "Output File: " << vm["output"].as<std::string>() << std::endl;
|
||||
|
||||
// Read the input file
|
||||
std::ifstream ifs;
|
||||
std::string input_string;
|
||||
ifs.open(vm["input"].as<std::string>(), std::ios_base::in);
|
||||
if (!ifs) {
|
||||
std::cout << "Failed to read input file!" << std::endl;
|
||||
if (!read(vm["input"].as<std::string>(), input_string)) {
|
||||
std::cout << "File Error: Couldn't read file!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
while (!ifs.eof()) {
|
||||
std::string line;
|
||||
getline(ifs, line);
|
||||
input_string += line + '\n';
|
||||
}
|
||||
ifs.close();
|
||||
|
||||
// Lexing
|
||||
std::cout << "Lexing:" << std::endl;
|
||||
@ -97,9 +138,17 @@ auto main(int argc, char **argv) -> int {
|
||||
|
||||
// Codegen
|
||||
std::cout << "Codegen:" << std::endl;
|
||||
std::string output_string;
|
||||
std::vector<std::string> output_string;
|
||||
CodegenObserver(*ast, output_string).Observer::traverse();
|
||||
std::cout << std::hex << output_string << std::endl;
|
||||
for (const auto &instruction : output_string) {
|
||||
std::cout << instruction << std::endl;
|
||||
}
|
||||
|
||||
// Write the output file
|
||||
if (!write(vm["output"].as<std::string>(), output_string)) {
|
||||
std::cout << "File Error: Couldn't write file!" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user