write C file

This commit is contained in:
Josh Holtrop 2018-05-20 16:04:44 -04:00
parent 2d76d34bc7
commit 2dea7d0bac

View File

@ -5,9 +5,12 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h>
static char preprocessed_fname[] = "/tmp/cxlppXXXXXX"; static char preprocessed_fname[] = "/tmp/cxlppXXXXXX";
static char c_fname[] = "/tmp/cxlcXXXXXX";
static bool preprocessed_fname_created = false; static bool preprocessed_fname_created = false;
static bool c_fname_created = false;
bool preprocess(const char * input_fname) bool preprocess(const char * input_fname)
{ {
@ -38,8 +41,47 @@ bool preprocess(const char * input_fname)
return true; return true;
} }
void emit_c(Node * node) void write_node(FILE * file, Node * node)
{ {
switch (node->type)
{
case NODE_TYPE_LIST:
{
bool space = false;
for (auto subnode : *node->list)
{
if (space)
{
fprintf(file, " ");
}
write_node(file, subnode);
space = true;
}
}
break;
case NODE_TYPE_TOKEN:
fprintf(file, "%s", node->token.text->c_str());
if (*node->token.text == ";")
{
fprintf(file, "\n");
}
break;
}
}
bool emit_c(Node * node)
{
int fd = mkstemp(c_fname);
if (fd < 0)
{
perror("mkstemp");
return false;
}
FILE * file = fdopen(fd, "w");
c_fname_created = true;
write_node(file, node);
fclose(file);
return true;
} }
int main(int argc, char * argv[]) int main(int argc, char * argv[])