added Compiler class to pass to Node::process() instead of a FILE *

git-svn-id: svn://anubis/jtlc/trunk@22 f5bc74b8-7b62-4e90-9214-7121d538519f
This commit is contained in:
josh 2010-01-14 20:27:01 +00:00
parent b686d15705
commit 7b8436acb4
7 changed files with 46 additions and 10 deletions

14
main/Compiler.cc Normal file
View File

@ -0,0 +1,14 @@
#include "Compiler.h"
#include <string>
using namespace std;
Compiler::Compiler(FILE * output_file)
{
m_outfile = output_file;
}
void Compiler::write(const string & str)
{
fputs(str.c_str(), m_outfile);
}

18
main/Compiler.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef COMPILER_H
#define COMPILER_H
#include <stdio.h>
#include <string>
class Compiler
{
public:
Compiler(FILE * output_file);
void write(const std::string & str);
protected:
FILE * m_outfile;
};
#endif

View File

@ -53,7 +53,8 @@ string compile(const char * filename)
string obj_filename = string(filename) + ".o";
FILE * out = fopen(out_filename.c_str(), "w");
refptr<Node> tree = parse(filename);
tree->process(out);
refptr<Compiler> compiler = new Compiler(out);
tree->process(compiler);
fclose(out);
ccompile(out_filename.c_str(), obj_filename.c_str());
// unlink(out_filename.c_str());

View File

@ -3,10 +3,11 @@
#include <string>
#include "Node.h"
#include "util/refptr.h"
#include "main/Compiler.h"
using namespace std;
void CStatementNode::process(FILE * out)
void CStatementNode::process(refptr<Compiler> compiler)
{
fprintf(out, "%s", m_string.c_str());
fprintf(out, "\n");
compiler->write(m_string);
compiler->write("\n");
}

View File

@ -2,9 +2,10 @@
#include <iostream>
#include "Node.h"
#include "parser/parser.h"
#include "main/Compiler.h"
using namespace std;
void FunctionNode::process(FILE * out)
void FunctionNode::process(refptr<Compiler> compiler)
{
refptr<Node> params = m_children[2];
refptr<Node> type = m_children[1];

View File

@ -17,13 +17,13 @@ void Node::addChildren(refptr<Node> other)
}
}
void Node::process(FILE * out)
void Node::process(refptr<Compiler> compiler)
{
for (vector< refptr<Node> >::const_iterator it = m_children.begin();
it != m_children.end();
it++)
{
(*it)->process(out);
(*it)->process(compiler);
}
}

View File

@ -7,6 +7,7 @@
#include <vector>
#include <string>
#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> compiler);
protected:
std::vector< refptr<Node> > 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> 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> compiler);
};
class IdentifierNode : public Node