Untitled

mail@pastecode.io avatar
unknown
c_cpp
15 days ago
2.4 kB
1
Indexable
Never
enum node_type {
	NODE_TYPE_PREPRO,
	NODE_TYPE_INCL,
	NODE_TYPE_PROG,
	NODE_TYPE_TOK
};

struct prepro_node {
	enum node_type type;
	union node *incl;
};

struct incl_node {
	enum node_type type;
	char *file;
};

struct tok_node {
	enum node_type type;
	char *start;
	int  len;
};

struct prog_node {
	enum node_type type;
	size_t nprepros;
	union node *prepros;
};

struct node_header {
	enum node_type type;
};

union node {
	struct node_header header;
	struct prepro_node prepro;
	struct incl_node incl;
	struct tok_node tok;
	struct prog_node prog;
};

static char *error_get(void);
static void error_set(char *msg);
void node_incl_print(struct incl_node *incl, int depth);
void node_tok_print(struct tok_node *tok, int depth);
void node_prog_print(struct prog_node *prog, unsigned int depth);
void node_prepro_print(struct prepro_node *prepro, int depth);
void node_print(union node *node, unsigned int depth);
static int parse_tok(struct tok_node *node);
static int parse_incl(struct incl_node *node);
static int parse_prepro(struct prepro_node *node);
static int parse_fun(void);
static int parse_decl(void);
static int parse_prog(struct prog_node *prog);
static void test_parsing_works(void);

void node_incl_print(struct incl_node *incl, int depth) {
	char *type = "incl";
	printf("%*s (file: \"%s\")\n", (int)strlen(type) + depth, type, incl->file);
}

void node_tok_print(struct tok_node *tok, int depth) {
	char *type = "tok";
	printf("%*s\n", (int)strlen(type) + depth, type);
}

void node_prog_print(struct prog_node *prog, unsigned int depth) {
	char *type = "prog";
	size_t i = 0 ;

	printf("%*s\n", (int)strlen(type) + depth, type);

	for (; i<prog->nprepros; i++) {
		node_print(&prog->prepros[i], depth+1);
	}
}

void node_prepro_print(struct prepro_node *prepro, int depth) {
	char *type = "prepro";
	int i = 0 ;

	printf("%*s\n", (int)strlen(type) + depth, type);
	node_print(prepro->incl, depth+1);
}

void node_print(union node *node, unsigned int depth) {
	char *type;
	void *fnprint(union node *node);

	switch (node->header.type) {
		case NODE_TYPE_PREPRO:
			node_prepro_print(&node->prepro, depth);
			break;
		case NODE_TYPE_INCL:
			node_incl_print(&node->incl, depth);
			break;
		case NODE_TYPE_TOK:
			node_tok_print(&node->tok, depth);
			break;
		case NODE_TYPE_PROG:
			node_prog_print(&node->prog, depth);
			break;
	}
}
Leave a Comment