write C file
This commit is contained in:
parent
2d76d34bc7
commit
2dea7d0bac
44
src/main.cc
44
src/main.cc
@ -5,9 +5,12 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
static char preprocessed_fname[] = "/tmp/cxlppXXXXXX";
|
||||
static char c_fname[] = "/tmp/cxlcXXXXXX";
|
||||
static bool preprocessed_fname_created = false;
|
||||
static bool c_fname_created = false;
|
||||
|
||||
bool preprocess(const char * input_fname)
|
||||
{
|
||||
@ -38,8 +41,47 @@ bool preprocess(const char * input_fname)
|
||||
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[])
|
||||
|
Loading…
x
Reference in New Issue
Block a user