successfully compiling tests/Initial.jtl!!

git-svn-id: svn://anubis/jtlc/trunk@17 f5bc74b8-7b62-4e90-9214-7121d538519f
This commit is contained in:
josh 2010-01-13 22:06:05 +00:00
parent 3efad6cc0f
commit d44b77d240
2 changed files with 57 additions and 4 deletions

View File

@ -2,6 +2,8 @@
#include <stdio.h> /* tmpfile() */
#include <stdlib.h> /* exit() */
#include <unistd.h> /* unlink() */
#include <sys/types.h>
#include <sys/wait.h> /* waitpid() */
#include <vector>
#include "util/refptr.h"
#include "parser/parser.h"
@ -9,7 +11,9 @@
using namespace std;
int usage();
void compile(const char * filename);
string compile(const char * filename);
void ccompile(const char * c_file, const char * obj_file);
void link(vector<string> & obj_files, const char * out_file);
int usage()
{
@ -33,20 +37,69 @@ int main(int argc, char * argv[])
usage();
}
vector<string> obj_files;
for (int i = 0, num = source_files.size(); i < num; i++)
{
compile(source_files[i]);
obj_files.push_back(compile(source_files[i]));
}
link(obj_files, "a.out");
return 0;
}
void compile(const char * filename)
string compile(const char * filename)
{
string out_filename = string(filename) + ".c";
string obj_filename = string(filename) + ".o";
FILE * out = fopen(out_filename.c_str(), "w");
refptr<Node> tree = parse(filename);
tree->process(out);
fclose(out);
ccompile(out_filename.c_str(), obj_filename.c_str());
// unlink(out_filename.c_str());
return obj_filename;
}
void ccompile(const char * c_file, const char * obj_file)
{
int id = fork();
const char * const args[] = {"cc", "-c", "-o", obj_file, c_file, NULL};
if (id == 0)
{
execvp("cc", (char * const *) args);
}
else if (id > 0)
{
waitpid(id, NULL, 0);
}
}
void link(vector<string> & obj_files, const char * out_file)
{
vector<const char *> args;
args.push_back("-o");
args.push_back(out_file);
for (int i = 0, sz = obj_files.size(); i < sz; i++)
{
args.push_back(obj_files[i].c_str());
}
int sz = args.size();
const char ** args_static = new const char * [sz + 2];
args_static[0] = "cc";
for (int i = 0; i < sz; i++)
{
args_static[i + 1] = args[i];
}
args_static[sz + 1] = NULL;
int id = fork();
if (id == 0)
{
execvp("cc", (char * const *) args_static);
}
else if (id > 0)
{
waitpid(id, NULL, 0);
}
delete[] args_static;
}

View File

@ -1,2 +1,2 @@
c("This is C to be output by my compiler");
c("int main() { return 42; }");