mirror of
https://github.com/avinal/The-VM-to-HACK-Translator.git
synced 2026-01-10 07:08:32 +05:30
cleaned code
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
#include "parser.hpp"
|
#include "parser.hpp"
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -30,6 +29,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
code_writer(lots_of_files name);
|
code_writer(lots_of_files name);
|
||||||
void write_assembly();
|
void write_assembly();
|
||||||
|
|
||||||
void write_push(std::string segment, int index, bool debug);
|
void write_push(std::string segment, int index, bool debug);
|
||||||
void write_push(std::string segment, bool debug);
|
void write_push(std::string segment, bool debug);
|
||||||
void write_pop(std::string segment, int index, bool debug);
|
void write_pop(std::string segment, int index, bool debug);
|
||||||
@@ -40,17 +40,6 @@ public:
|
|||||||
void write_neg_not(std::string op, bool debug);
|
void write_neg_not(std::string op, bool debug);
|
||||||
void write_eq_lt_gt(std::string op, bool debug);
|
void write_eq_lt_gt(std::string op, bool debug);
|
||||||
|
|
||||||
/*
|
|
||||||
void write_add(bool debug);
|
|
||||||
void write_sub(bool debug);
|
|
||||||
void write_neg(bool debug);
|
|
||||||
void write_and(bool debug);
|
|
||||||
void write_or(bool debug);
|
|
||||||
void write_not(bool debug);
|
|
||||||
void write_eq(bool debug);
|
|
||||||
void write_lt(bool debug);
|
|
||||||
void write_gt(bool debug);
|
|
||||||
*/
|
|
||||||
void write_init(bool debug);
|
void write_init(bool debug);
|
||||||
|
|
||||||
void write_label(std::string label, int index, bool debug);
|
void write_label(std::string label, int index, bool debug);
|
||||||
|
|||||||
@@ -359,167 +359,6 @@ void code_writer::write_eq_lt_gt(std::string op, bool debug)
|
|||||||
this->ins_no += 6;
|
this->ins_no += 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
void code_writer::write_add(bool debug)
|
|
||||||
{
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
outfile << "// add" << std::endl;
|
|
||||||
}
|
|
||||||
outfile << "@SP" << std::endl
|
|
||||||
<< "AM=M-1" << std::endl
|
|
||||||
<< "D=M" << std::endl
|
|
||||||
<< "@SP" << std::endl
|
|
||||||
<< "A=M-1" << std::endl
|
|
||||||
<< "M=D+M" << std::endl;
|
|
||||||
this->ins_no += 6;
|
|
||||||
}
|
|
||||||
|
|
||||||
void code_writer::write_sub(bool debug)
|
|
||||||
{
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
outfile << "// sub" << std::endl;
|
|
||||||
}
|
|
||||||
outfile << "@SP" << std::endl
|
|
||||||
<< "AM=M-1" << std::endl
|
|
||||||
<< "D=M" << std::endl
|
|
||||||
<< "@SP" << std::endl
|
|
||||||
<< "A=M-1" << std::endl
|
|
||||||
<< "M=M-D" << std::endl;
|
|
||||||
this->ins_no += 6;
|
|
||||||
}
|
|
||||||
|
|
||||||
void code_writer::write_neg(bool debug)
|
|
||||||
{
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
outfile << "// neg" << std::endl;
|
|
||||||
}
|
|
||||||
outfile << "@SP" << std::endl
|
|
||||||
<< "A=M-1" << std::endl
|
|
||||||
<< "M=-M" << std::endl;
|
|
||||||
this->ins_no += 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
void code_writer::write_and(bool debug)
|
|
||||||
{
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
outfile << "// and" << std::endl;
|
|
||||||
}
|
|
||||||
outfile << "@SP" << std::endl
|
|
||||||
<< "AM=M-1" << std::endl
|
|
||||||
<< "D=M" << std::endl
|
|
||||||
<< "@SP" << std::endl
|
|
||||||
<< "A=M-1" << std::endl
|
|
||||||
<< "M=D&M" << std::endl;
|
|
||||||
this->ins_no += 6;
|
|
||||||
}
|
|
||||||
|
|
||||||
void code_writer::write_or(bool debug)
|
|
||||||
{
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
outfile << "// or" << std::endl;
|
|
||||||
}
|
|
||||||
outfile << "@SP" << std::endl
|
|
||||||
<< "AM=M-1" << std::endl
|
|
||||||
<< "D=M" << std::endl
|
|
||||||
<< "@SP" << std::endl
|
|
||||||
<< "A=M-1" << std::endl
|
|
||||||
<< "M=D|M" << std::endl;
|
|
||||||
this->ins_no += 6;
|
|
||||||
}
|
|
||||||
|
|
||||||
void code_writer::write_not(bool debug)
|
|
||||||
{
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
outfile << "// not" << std::endl;
|
|
||||||
}
|
|
||||||
outfile << "@SP" << std::endl
|
|
||||||
<< "A=M-1" << std::endl
|
|
||||||
<< "M=!M" << std::endl;
|
|
||||||
this->ins_no += 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
void code_writer::write_eq(bool debug)
|
|
||||||
{
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
outfile << "// eq" << std::endl;
|
|
||||||
}
|
|
||||||
outfile << "@SP" << std::endl
|
|
||||||
<< "AM=M-1" << std::endl
|
|
||||||
<< "D=M" << std::endl
|
|
||||||
<< "A=A-1" << std::endl
|
|
||||||
<< "D=M-D" << std::endl;
|
|
||||||
this->ins_no += 5;
|
|
||||||
outfile << "@" << this->ins_no + 6 << std::endl
|
|
||||||
<< "D;JEQ" << std::endl
|
|
||||||
<< "D=0" << std::endl;
|
|
||||||
this->ins_no += 3;
|
|
||||||
outfile << "@" << this->ins_no + 4 << std::endl
|
|
||||||
<< "0;JMP" << std::endl
|
|
||||||
<< "D=-1" << std::endl
|
|
||||||
<< "@SP" << std::endl
|
|
||||||
<< "A=M-1" << std::endl
|
|
||||||
<< "M=D" << std::endl;
|
|
||||||
this->ins_no += 6;
|
|
||||||
}
|
|
||||||
|
|
||||||
void code_writer::write_lt(bool debug)
|
|
||||||
{
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
outfile << "// lt" << std::endl;
|
|
||||||
}
|
|
||||||
outfile << "@SP" << std::endl
|
|
||||||
<< "AM=M-1" << std::endl
|
|
||||||
<< "D=M" << std::endl
|
|
||||||
<< "A=A-1" << std::endl
|
|
||||||
<< "D=M-D" << std::endl;
|
|
||||||
this->ins_no += 5;
|
|
||||||
outfile << "@" << this->ins_no + 6 << std::endl
|
|
||||||
<< "D;JLT" << std::endl
|
|
||||||
<< "D=0" << std::endl;
|
|
||||||
this->ins_no += 3;
|
|
||||||
outfile << "@" << this->ins_no + 4 << std::endl
|
|
||||||
<< "0;JMP" << std::endl
|
|
||||||
<< "D=-1" << std::endl
|
|
||||||
<< "@SP" << std::endl
|
|
||||||
<< "A=M-1" << std::endl
|
|
||||||
<< "M=D" << std::endl;
|
|
||||||
this->ins_no += 6;
|
|
||||||
}
|
|
||||||
|
|
||||||
void code_writer::write_gt(bool debug)
|
|
||||||
{
|
|
||||||
if (debug)
|
|
||||||
{
|
|
||||||
outfile << "// gt" << std::endl;
|
|
||||||
}
|
|
||||||
outfile << "@SP" << std::endl
|
|
||||||
<< "AM=M-1" << std::endl
|
|
||||||
<< "D=M" << std::endl
|
|
||||||
<< "A=A-1" << std::endl
|
|
||||||
<< "D=M-D" << std::endl;
|
|
||||||
this->ins_no += 5;
|
|
||||||
outfile << "@" << this->ins_no + 6 << std::endl
|
|
||||||
<< "D;JGT" << std::endl
|
|
||||||
<< "D=0" << std::endl;
|
|
||||||
this->ins_no += 3;
|
|
||||||
outfile << "@" << this->ins_no + 4 << std::endl
|
|
||||||
<< "0;JMP" << std::endl
|
|
||||||
<< "D=-1" << std::endl
|
|
||||||
<< "@SP" << std::endl
|
|
||||||
<< "A=M-1" << std::endl
|
|
||||||
<< "M=D" << std::endl;
|
|
||||||
this->ins_no += 6;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
void code_writer::write_init(bool debug)
|
void code_writer::write_init(bool debug)
|
||||||
{
|
{
|
||||||
if (debug)
|
if (debug)
|
||||||
|
|||||||
Reference in New Issue
Block a user