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:
parent
b23442a2f4
commit
d052712d94
@ -1,16 +1,56 @@
|
|||||||
|
|
||||||
#include "Compiler.h"
|
#include "Compiler.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <map>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
Compiler::Compiler(FILE * output_file)
|
Compiler::Compiler(const string & out_filename)
|
||||||
{
|
{
|
||||||
m_outfile = output_file;
|
m_outfile = fopen(out_filename.c_str(), "w");
|
||||||
write("#include <stdint.h>\n");
|
m_file_open = (m_outfile != NULL);
|
||||||
write("\n");
|
|
||||||
|
/* 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;
|
||||||
}
|
}
|
||||||
|
@ -4,15 +4,25 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
class Compiler
|
class Compiler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Compiler(FILE * output_file);
|
Compiler(const std::string & out_filename);
|
||||||
void write(const std::string & str);
|
~Compiler();
|
||||||
|
|
||||||
|
void writeHeader(const std::string & str);
|
||||||
|
void writeFunction(const std::string & str);
|
||||||
|
void beginFunction(const std::string & name);
|
||||||
|
void close();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
FILE * m_outfile;
|
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
|
#endif
|
||||||
|
@ -51,11 +51,10 @@ string compile(const char * filename)
|
|||||||
{
|
{
|
||||||
string out_filename = string(filename) + ".c";
|
string out_filename = string(filename) + ".c";
|
||||||
string obj_filename = string(filename) + ".o";
|
string obj_filename = string(filename) + ".o";
|
||||||
FILE * out = fopen(out_filename.c_str(), "w");
|
|
||||||
refptr<Node> tree = parse(filename);
|
refptr<Node> tree = parse(filename);
|
||||||
refptr<Compiler> compiler = new Compiler(out);
|
refptr<Compiler> compiler = new Compiler(out_filename);
|
||||||
tree->process(compiler);
|
tree->process(compiler);
|
||||||
fclose(out);
|
compiler->close();
|
||||||
ccompile(out_filename.c_str(), obj_filename.c_str());
|
ccompile(out_filename.c_str(), obj_filename.c_str());
|
||||||
// unlink(out_filename.c_str());
|
// unlink(out_filename.c_str());
|
||||||
return obj_filename;
|
return obj_filename;
|
||||||
|
@ -8,6 +8,6 @@ using namespace std;
|
|||||||
|
|
||||||
void CStatementNode::process(refptr<Compiler> compiler)
|
void CStatementNode::process(refptr<Compiler> compiler)
|
||||||
{
|
{
|
||||||
compiler->write(m_string);
|
compiler->writeFunction(m_string);
|
||||||
compiler->write("\n");
|
compiler->writeFunction("\n");
|
||||||
}
|
}
|
||||||
|
@ -9,5 +9,5 @@ void DoubleNode::process(refptr<Compiler> compiler)
|
|||||||
{
|
{
|
||||||
char buff[50];
|
char buff[50];
|
||||||
sprintf(buff, "%lf", m_double);
|
sprintf(buff, "%lf", m_double);
|
||||||
compiler->write(buff);
|
compiler->writeFunction(buff);
|
||||||
}
|
}
|
||||||
|
@ -10,23 +10,24 @@ void FunctionNode::process(refptr<Compiler> compiler)
|
|||||||
refptr<Node> name = m_children[0];
|
refptr<Node> name = m_children[0];
|
||||||
refptr<Node> params = m_children[1];
|
refptr<Node> params = m_children[1];
|
||||||
refptr<Node> type = m_children[2];
|
refptr<Node> type = m_children[2];
|
||||||
compiler->write(type->getString());
|
compiler->beginFunction(name->getString());
|
||||||
compiler->write(" ");
|
compiler->writeFunction(type->getString());
|
||||||
compiler->write(name->getString());
|
compiler->writeFunction(" ");
|
||||||
compiler->write("(");
|
compiler->writeFunction(name->getString());
|
||||||
|
compiler->writeFunction("(");
|
||||||
for (int i = 0, sz = params->getChildren().size(); i < sz; i++)
|
for (int i = 0, sz = params->getChildren().size(); i < sz; i++)
|
||||||
{
|
{
|
||||||
if (i > 0)
|
if (i > 0)
|
||||||
{
|
{
|
||||||
compiler->write(", ");
|
compiler->writeFunction(", ");
|
||||||
}
|
}
|
||||||
compiler->write(params->getChildren()[i]->getString());
|
compiler->writeFunction(params->getChildren()[i]->getString());
|
||||||
}
|
}
|
||||||
compiler->write(")\n");
|
compiler->writeFunction(")\n");
|
||||||
compiler->write("{\n");
|
compiler->writeFunction("{\n");
|
||||||
for (int i = 3, sz = m_children.size(); i < sz; i++)
|
for (int i = 3, sz = m_children.size(); i < sz; i++)
|
||||||
{
|
{
|
||||||
m_children[i]->process(compiler);
|
m_children[i]->process(compiler);
|
||||||
}
|
}
|
||||||
compiler->write("}\n\n");
|
compiler->writeFunction("}\n\n");
|
||||||
}
|
}
|
||||||
|
@ -9,5 +9,5 @@ void IntegerNode::process(refptr<Compiler> compiler)
|
|||||||
{
|
{
|
||||||
char buff[30];
|
char buff[30];
|
||||||
sprintf(buff, "%llu", (long long unsigned int) m_integer);
|
sprintf(buff, "%llu", (long long unsigned int) m_integer);
|
||||||
compiler->write(buff);
|
compiler->writeFunction(buff);
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ using namespace std;
|
|||||||
|
|
||||||
void ReturnStatementNode::process(refptr<Compiler> compiler)
|
void ReturnStatementNode::process(refptr<Compiler> compiler)
|
||||||
{
|
{
|
||||||
compiler->write("return (");
|
compiler->writeFunction("return (");
|
||||||
m_children[0]->process(compiler);
|
m_children[0]->process(compiler);
|
||||||
compiler->write(");\n");
|
compiler->writeFunction(");\n");
|
||||||
}
|
}
|
||||||
|
@ -7,5 +7,5 @@ using namespace std;
|
|||||||
|
|
||||||
void StringNode::process(refptr<Compiler> compiler)
|
void StringNode::process(refptr<Compiler> compiler)
|
||||||
{
|
{
|
||||||
compiler->write(m_string);
|
compiler->writeFunction(m_string);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user