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:
parent
b686d15705
commit
7b8436acb4
14
main/Compiler.cc
Normal file
14
main/Compiler.cc
Normal 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
18
main/Compiler.h
Normal 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
|
@ -53,7 +53,8 @@ string compile(const char * filename)
|
|||||||
string obj_filename = string(filename) + ".o";
|
string obj_filename = string(filename) + ".o";
|
||||||
FILE * out = fopen(out_filename.c_str(), "w");
|
FILE * out = fopen(out_filename.c_str(), "w");
|
||||||
refptr<Node> tree = parse(filename);
|
refptr<Node> tree = parse(filename);
|
||||||
tree->process(out);
|
refptr<Compiler> compiler = new Compiler(out);
|
||||||
|
tree->process(compiler);
|
||||||
fclose(out);
|
fclose(out);
|
||||||
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());
|
||||||
|
@ -3,10 +3,11 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include "Node.h"
|
#include "Node.h"
|
||||||
#include "util/refptr.h"
|
#include "util/refptr.h"
|
||||||
|
#include "main/Compiler.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void CStatementNode::process(FILE * out)
|
void CStatementNode::process(refptr<Compiler> compiler)
|
||||||
{
|
{
|
||||||
fprintf(out, "%s", m_string.c_str());
|
compiler->write(m_string);
|
||||||
fprintf(out, "\n");
|
compiler->write("\n");
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,10 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "Node.h"
|
#include "Node.h"
|
||||||
#include "parser/parser.h"
|
#include "parser/parser.h"
|
||||||
|
#include "main/Compiler.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void FunctionNode::process(FILE * out)
|
void FunctionNode::process(refptr<Compiler> compiler)
|
||||||
{
|
{
|
||||||
refptr<Node> params = m_children[2];
|
refptr<Node> params = m_children[2];
|
||||||
refptr<Node> type = m_children[1];
|
refptr<Node> type = m_children[1];
|
||||||
|
@ -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();
|
for (vector< refptr<Node> >::const_iterator it = m_children.begin();
|
||||||
it != m_children.end();
|
it != m_children.end();
|
||||||
it++)
|
it++)
|
||||||
{
|
{
|
||||||
(*it)->process(out);
|
(*it)->process(compiler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "util/refptr.h"
|
#include "util/refptr.h"
|
||||||
|
#include "main/Compiler.h"
|
||||||
|
|
||||||
class Node
|
class Node
|
||||||
{
|
{
|
||||||
@ -19,7 +20,7 @@ class Node
|
|||||||
virtual double getDouble() { return m_double; }
|
virtual double getDouble() { return m_double; }
|
||||||
virtual uint64_t getInteger() { return m_integer; }
|
virtual uint64_t getInteger() { return m_integer; }
|
||||||
virtual std::string getString() { return m_string; }
|
virtual std::string getString() { return m_string; }
|
||||||
virtual void process(FILE * out);
|
virtual void process(refptr<Compiler> compiler);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::vector< refptr<Node> > m_children;
|
std::vector< refptr<Node> > m_children;
|
||||||
@ -41,7 +42,7 @@ class CStatementNode : public Node
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CStatementNode(const std::string & str) { m_string = str; }
|
CStatementNode(const std::string & str) { m_string = str; }
|
||||||
virtual void process(FILE * out);
|
virtual void process(refptr<Compiler> compiler);
|
||||||
};
|
};
|
||||||
|
|
||||||
class DoubleNode : public Node
|
class DoubleNode : public Node
|
||||||
@ -53,7 +54,7 @@ class DoubleNode : public Node
|
|||||||
class FunctionNode : public Node
|
class FunctionNode : public Node
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void process(FILE * out);
|
void process(refptr<Compiler> compiler);
|
||||||
};
|
};
|
||||||
|
|
||||||
class IdentifierNode : public Node
|
class IdentifierNode : public Node
|
||||||
|
Loading…
x
Reference in New Issue
Block a user