diff --git a/main/Compiler.cc b/main/Compiler.cc index 239ca7f..c00b70c 100644 --- a/main/Compiler.cc +++ b/main/Compiler.cc @@ -1,16 +1,56 @@ #include "Compiler.h" #include +#include using namespace std; -Compiler::Compiler(FILE * output_file) +Compiler::Compiler(const string & out_filename) { - m_outfile = output_file; - write("#include \n"); - write("\n"); + m_outfile = fopen(out_filename.c_str(), "w"); + m_file_open = (m_outfile != NULL); + + /* standard header information */ + writeHeader("#include \n"); } -void Compiler::write(const string & str) +Compiler::~Compiler() { - fputs(str.c_str(), m_outfile); + close(); +} + +void Compiler::close() +{ + if (m_file_open) + { + fputs(m_header.c_str(), m_outfile); + fputs("\n", m_outfile); + for (map::const_iterator it = m_functions.begin(); + it != m_functions.end(); + it++) + { + fputs(it->second.c_str(), m_outfile); + fputs("\n", m_outfile); + } + fclose(m_outfile); + m_file_open = false; + } +} + +void Compiler::writeHeader(const string & str) +{ + m_header += str; +} + +void Compiler::writeFunction(const string & str) +{ + if (m_functions.find(m_current_function) == m_functions.end()) + { + m_functions[m_current_function] = ""; + } + m_functions[m_current_function] += str; +} + +void Compiler::beginFunction(const string & name) +{ + m_current_function = name; } diff --git a/main/Compiler.h b/main/Compiler.h index 4f689ce..093691f 100644 --- a/main/Compiler.h +++ b/main/Compiler.h @@ -4,15 +4,25 @@ #include #include +#include class Compiler { public: - Compiler(FILE * output_file); - void write(const std::string & str); + Compiler(const std::string & out_filename); + ~Compiler(); + + void writeHeader(const std::string & str); + void writeFunction(const std::string & str); + void beginFunction(const std::string & name); + void close(); protected: FILE * m_outfile; + std::string m_header; + std::map m_functions; + std::string m_current_function; + bool m_file_open; }; #endif diff --git a/main/jtlc.cc b/main/jtlc.cc index 35ff726..a658704 100644 --- a/main/jtlc.cc +++ b/main/jtlc.cc @@ -51,11 +51,10 @@ string compile(const char * filename) { string out_filename = string(filename) + ".c"; string obj_filename = string(filename) + ".o"; - FILE * out = fopen(out_filename.c_str(), "w"); refptr tree = parse(filename); - refptr compiler = new Compiler(out); + refptr compiler = new Compiler(out_filename); tree->process(compiler); - fclose(out); + compiler->close(); ccompile(out_filename.c_str(), obj_filename.c_str()); // unlink(out_filename.c_str()); return obj_filename; diff --git a/nodes/CStatementNode.cc b/nodes/CStatementNode.cc index 00d007a..d1c9e02 100644 --- a/nodes/CStatementNode.cc +++ b/nodes/CStatementNode.cc @@ -8,6 +8,6 @@ using namespace std; void CStatementNode::process(refptr compiler) { - compiler->write(m_string); - compiler->write("\n"); + compiler->writeFunction(m_string); + compiler->writeFunction("\n"); } diff --git a/nodes/DoubleNode.cc b/nodes/DoubleNode.cc index 216e639..f95864c 100644 --- a/nodes/DoubleNode.cc +++ b/nodes/DoubleNode.cc @@ -9,5 +9,5 @@ void DoubleNode::process(refptr compiler) { char buff[50]; sprintf(buff, "%lf", m_double); - compiler->write(buff); + compiler->writeFunction(buff); } diff --git a/nodes/FunctionNode.cc b/nodes/FunctionNode.cc index 5a222f6..930d87e 100644 --- a/nodes/FunctionNode.cc +++ b/nodes/FunctionNode.cc @@ -10,23 +10,24 @@ void FunctionNode::process(refptr compiler) refptr name = m_children[0]; refptr params = m_children[1]; refptr type = m_children[2]; - compiler->write(type->getString()); - compiler->write(" "); - compiler->write(name->getString()); - compiler->write("("); + compiler->beginFunction(name->getString()); + compiler->writeFunction(type->getString()); + compiler->writeFunction(" "); + compiler->writeFunction(name->getString()); + compiler->writeFunction("("); for (int i = 0, sz = params->getChildren().size(); i < sz; i++) { if (i > 0) { - compiler->write(", "); + compiler->writeFunction(", "); } - compiler->write(params->getChildren()[i]->getString()); + compiler->writeFunction(params->getChildren()[i]->getString()); } - compiler->write(")\n"); - compiler->write("{\n"); + compiler->writeFunction(")\n"); + compiler->writeFunction("{\n"); for (int i = 3, sz = m_children.size(); i < sz; i++) { m_children[i]->process(compiler); } - compiler->write("}\n\n"); + compiler->writeFunction("}\n\n"); } diff --git a/nodes/IntegerNode.cc b/nodes/IntegerNode.cc index 8d81829..60c4522 100644 --- a/nodes/IntegerNode.cc +++ b/nodes/IntegerNode.cc @@ -9,5 +9,5 @@ void IntegerNode::process(refptr compiler) { char buff[30]; sprintf(buff, "%llu", (long long unsigned int) m_integer); - compiler->write(buff); + compiler->writeFunction(buff); } diff --git a/nodes/ReturnStatementNode.cc b/nodes/ReturnStatementNode.cc index 183c632..4819ae5 100644 --- a/nodes/ReturnStatementNode.cc +++ b/nodes/ReturnStatementNode.cc @@ -7,7 +7,7 @@ using namespace std; void ReturnStatementNode::process(refptr compiler) { - compiler->write("return ("); + compiler->writeFunction("return ("); m_children[0]->process(compiler); - compiler->write(");\n"); + compiler->writeFunction(");\n"); } diff --git a/nodes/StringNode.cc b/nodes/StringNode.cc index 4cf65b5..697340e 100644 --- a/nodes/StringNode.cc +++ b/nodes/StringNode.cc @@ -7,5 +7,5 @@ using namespace std; void StringNode::process(refptr compiler) { - compiler->write(m_string); + compiler->writeFunction(m_string); }