jtlc/main/Compiler.cc
josh d052712d94 broke Compiler::write() into Compiler::writeHeader() and Compiler::writeFunction()
git-svn-id: svn://anubis/jtlc/trunk@26 f5bc74b8-7b62-4e90-9214-7121d538519f
2010-01-19 16:20:27 +00:00

57 lines
1.1 KiB
C++

#include "Compiler.h"
#include <string>
#include <map>
using namespace std;
Compiler::Compiler(const string & out_filename)
{
m_outfile = fopen(out_filename.c_str(), "w");
m_file_open = (m_outfile != NULL);
/* standard header information */
writeHeader("#include <stdint.h>\n");
}
Compiler::~Compiler()
{
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;
}