diff --git a/main/Compiler.cc b/main/Compiler.cc new file mode 100644 index 0000000..610757e --- /dev/null +++ b/main/Compiler.cc @@ -0,0 +1,14 @@ + +#include "Compiler.h" +#include +using namespace std; + +Compiler::Compiler(FILE * output_file) +{ + m_outfile = output_file; +} + +void Compiler::write(const string & str) +{ + fputs(str.c_str(), m_outfile); +} diff --git a/main/Compiler.h b/main/Compiler.h new file mode 100644 index 0000000..4f689ce --- /dev/null +++ b/main/Compiler.h @@ -0,0 +1,18 @@ + +#ifndef COMPILER_H +#define COMPILER_H + +#include +#include + +class Compiler +{ + public: + Compiler(FILE * output_file); + void write(const std::string & str); + + protected: + FILE * m_outfile; +}; + +#endif diff --git a/main/jtlc.cc b/main/jtlc.cc index ebe9cf0..35ff726 100644 --- a/main/jtlc.cc +++ b/main/jtlc.cc @@ -53,7 +53,8 @@ string compile(const char * filename) string obj_filename = string(filename) + ".o"; FILE * out = fopen(out_filename.c_str(), "w"); refptr tree = parse(filename); - tree->process(out); + refptr compiler = new Compiler(out); + tree->process(compiler); fclose(out); ccompile(out_filename.c_str(), obj_filename.c_str()); // unlink(out_filename.c_str()); diff --git a/nodes/CStatementNode.cc b/nodes/CStatementNode.cc index 9ce67c3..00d007a 100644 --- a/nodes/CStatementNode.cc +++ b/nodes/CStatementNode.cc @@ -3,10 +3,11 @@ #include #include "Node.h" #include "util/refptr.h" +#include "main/Compiler.h" using namespace std; -void CStatementNode::process(FILE * out) +void CStatementNode::process(refptr compiler) { - fprintf(out, "%s", m_string.c_str()); - fprintf(out, "\n"); + compiler->write(m_string); + compiler->write("\n"); } diff --git a/nodes/FunctionNode.cc b/nodes/FunctionNode.cc index c9177e7..4043744 100644 --- a/nodes/FunctionNode.cc +++ b/nodes/FunctionNode.cc @@ -2,9 +2,10 @@ #include #include "Node.h" #include "parser/parser.h" +#include "main/Compiler.h" using namespace std; -void FunctionNode::process(FILE * out) +void FunctionNode::process(refptr compiler) { refptr params = m_children[2]; refptr type = m_children[1]; diff --git a/nodes/Node.cc b/nodes/Node.cc index 6999caf..e9e80a0 100644 --- a/nodes/Node.cc +++ b/nodes/Node.cc @@ -17,13 +17,13 @@ void Node::addChildren(refptr other) } } -void Node::process(FILE * out) +void Node::process(refptr compiler) { for (vector< refptr >::const_iterator it = m_children.begin(); it != m_children.end(); it++) { - (*it)->process(out); + (*it)->process(compiler); } } diff --git a/nodes/Node.h b/nodes/Node.h index a2077c7..b567e13 100644 --- a/nodes/Node.h +++ b/nodes/Node.h @@ -7,6 +7,7 @@ #include #include #include "util/refptr.h" +#include "main/Compiler.h" class Node { @@ -19,7 +20,7 @@ class Node virtual double getDouble() { return m_double; } virtual uint64_t getInteger() { return m_integer; } virtual std::string getString() { return m_string; } - virtual void process(FILE * out); + virtual void process(refptr compiler); protected: std::vector< refptr > m_children; @@ -41,7 +42,7 @@ class CStatementNode : public Node { public: CStatementNode(const std::string & str) { m_string = str; } - virtual void process(FILE * out); + virtual void process(refptr compiler); }; class DoubleNode : public Node @@ -53,7 +54,7 @@ class DoubleNode : public Node class FunctionNode : public Node { public: - void process(FILE * out); + void process(refptr compiler); }; class IdentifierNode : public Node