~pixelherodev/c3lib

ref: ddd789667e0b3f1f92a636752601943924d590c9 c3lib/dump.c -rw-r--r-- 2.5 KiB
ddd78966Noam Preil tmp 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "tags.h"
#include "c3.h"
#include "util.h"
#include "stb.h"

int just_tags = 1;

void
c3nodedump(c3ctx ctx, uint32_t index, uint32_t indent, int recurse)
{
	c3tag tag = c3nodetag(ctx, index);
	uint16_t kid_count = c3nodekids(ctx, index);
	int i;
	int j;
	char *buf;
	if(index == -1)
    		return;
	if(index == 0){
		ERROR("Internal error: recursive dump!", 0);
		return;
	}
	switch(tag){
	case C3_DEP:
		if(recurse){
			if(c3nodechild(ctx, index, 0) != -1)
				c3nodedump(ctx, c3nodechild(ctx, index, 0), indent, 0);
		}
		else
			printf("%u, ", index);
		if(!recurse)
			break;
		if(kid_count > 1)
			printf(", depends on: ");
		for(i = 1; i < kid_count; i += 1)
			c3nodedump(ctx, c3nodechild(ctx, index, i), indent, 0);
		break;
	case C3_LITERAL_STRING:
	case C3_IDENT:
		buf = h_c3str(ctx, c3nodechild(ctx, index, 0));
		printf("\"%s\"", buf);
		free(buf);
		break;
	default:
		if(tag == C3_VALUE_FN)
			printf("function");
		else
			printf("%s", c3tagstr(tag));
		printf("[%d]", index);
		if(kid_count == 0)
			return;
		if(!recurse)
			break;
		printf("{");
		if(tag == C3_CONST || tag == C3_VAR){
			for(i = 0; i < kid_count; i += 1){
    				if(c3nodechild(ctx, index, i) == -1)
        				break;
    				if(kid_count > 1){
        				if(i >= 1)
            					printf(",");
        				printf("\n");
        				for(j = 0; j < indent + 1; j += 1)
        					printf("\t");
    				}
				printf("{%d}%s", index+1+i, recurse ? "=" : "");
				c3nodedump(ctx, c3nodechild(ctx, index, i), indent + 1, recurse);
			}
			printf("\n");
			for(j = 0; j < indent; j += 1)
				printf("\t");
			printf("}");
		}
		else if(tag == C3_EXPORT || tag == C3_PUB){
			c3nodedump(ctx, c3nodechild(ctx, index, 0), indent, recurse);
			printf("}");
		}
		else{
			for(i = 0; i < kid_count; i += 1){
				if(tag == C3_EXPR_RETURN && i == 0)
					continue;
    				if(c3nodechild(ctx, index, i) == -1)
        				break;
    				if(kid_count > 1){
        				if(i >= 1)
            					printf(",");
        				printf("\n");
        				for(j = 0; j < indent + 1; j += 1)
        					printf("\t");
    				}
				c3nodedump(ctx, c3nodechild(ctx, index, i), indent + (kid_count > 1 ? 1 : 0), recurse);
			}
			if(kid_count > 1){
        			printf("\n");
        			for(i = 0; i < indent; i += 1)
        				printf("\t");
			}
			printf("}");
		}
		break;
	}
}

void
c3dump(c3ctx ctx)
{
	if(stb_sb_count(ctx.ast.nodes) == 0)
    		FATAL("missing root", 0);
	c3nodedump(ctx, ctx.ast.nodes[1].node_index, 0, 1);
	printf("\n");
}