broke Compiler::write() into Compiler::writeHeader() and Compiler::writeFunction()

git-svn-id: svn://anubis/jtlc/trunk@26 f5bc74b8-7b62-4e90-9214-7121d538519f
This commit is contained in:
josh committed 2010-01-19 16:20:27 +00:00
1 parent b23442a2f4
commit d052712d94
9 files changed
+77 -27

No files matched your search

+46 -6
View File
@@ -1,16 +1,56 @@
#include "Compiler.h"
#include <string>
#include <map>
using namespace std;
Compiler::Compiler(FILE * output_file)
Compiler::Compiler(const string & out_filename)
{
m_outfile = output_file;
write("#include <stdint.h>\n");
write("\n");
m_outfile = fopen(out_filename.c_str(), "w");
m_file_open = (m_outfile != NULL);
/* standard header information */
writeHeader("#include <stdint.h>\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<string, string>::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;
}
+12 -2
View File
@@ -4,15 +4,25 @@
#include <stdio.h>
#include <string>
#include <map>
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<std::string, std::string> m_functions;
std::string m_current_function;
bool m_file_open;
};
#endif
+2 -3
View File
@@ -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<Node> tree = parse(filename);
refptr<Compiler> compiler = new Compiler(out);
refptr<Compiler> 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;