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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/*
* Copyright (c) 2026 Nakidai Perumenei <nakidai at disroot dot org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* These functions are called for debugging if needed
*/
#include "thac.h"
void walknode(struct node *x, ulong depth)
{
int i;
for (i = 0; i < depth; ++i)
fputs(" ", stderr);
fprintf(stderr, "%s", nodename(x->type));
switch (x->type)
{
break; case NOPER:
{
fprintf(stderr, ": %s\n", nopername(x->noper.type));
switch (x->noper.type)
{
break; case OINDEX:
{
walknode(x->noper.l, depth + 1);
walknode(x->noper.m, depth + 1);
if (x->noper.r)
walknode(x->noper.r, depth + 1);
}
break; case OASSERT: case OLEN: case OINV:
case ONOT: case OPOSTDECR: case OPOSTINCR:
case OPREDECR: case OPREINCR:
{
walknode(x->noper.m, depth + 1);
}
break; case OCOND:
{
walknode(x->noper.l, depth + 1);
walknode(x->noper.m, depth + 1);
walknode(x->noper.r, depth + 1);
}
break; default:
{
walknode(x->noper.l, depth + 1);
walknode(x->noper.r, depth + 1);
}
}
}
break; case NNODE:
{
ulong i;
fputc('\n', stderr);
for (i = 0; i < x->nnode.len; ++i)
walknode(x->nnode.params[i], depth + 1);
}
break; case NCOMP:
{
ulong i;
fputc('\n', stderr);
for (i = 0; i < x->ncomp.len; ++i)
walknode(x->ncomp.stmts[i], depth + 1);
}
break; case NMOD:
{
ulong i;
fputs(": ", stderr);
for (i = 0; i < x->nmod.len; ++i)
fprintf(stderr, "%s ", x->nmod.params[i]);
fputc('\n', stderr);
walknode(x->nmod.code, depth + 1);
}
break; case NVAR:
{
fprintf(stderr, ": %s\n", x->nvar);
}
break; case NNUM:
{
fprintf(stderr, ": %lld\n", x->nnum);
}
break; case NFOR:
{
fputc('\n', stderr);
if (x->nfor.before)
walknode(x->nfor.before, depth + 1);
if (x->nfor.cond)
walknode(x->nfor.cond, depth + 1);
if (x->nfor.between)
walknode(x->nfor.between, depth + 1);
walknode(x->nfor.code, depth + 1);
}
break; case NFOREACH:
{
fputc('\n', stderr);
walknode(x->nforeach.obj, depth + 1);
walknode(x->nforeach.code, depth + 1);
}
break; case NCOND:
{
fputc('\n', stderr);
walknode(x->ncond.cond, depth + 1);
walknode(x->ncond.t, depth + 1);
if (x->ncond.f)
walknode(x->ncond.f, depth + 1);
}
break; case NWITH:
{
ulong i;
fputc('\n', stderr);
walknode(x->nwith.mod, depth + 1);
for (i = 0; i < x->nwith.len; ++i)
walknode(x->nwith.args[i], depth + 1);
walknode(x->nwith.code, depth + 1);
}
break; default: putc('\n', stderr);
}
}
void
walkscope(struct scope *scope, ulong depth)
{
#define printdepth() for (j = 0; j < depth; ++j) fputs(" ", stderr)
ulong i, j;
printdepth();
fprintf(stderr, "scope(%lu)=%p:\n", scope->len, scope);
++depth;
if (scope->parent)
walkscope(scope->parent, depth);
for (i = 0; i < scope->len; ++i)
{
printdepth();
fprintf(
stderr,
"%s:%s=%lld\n",
scope->vars[i].name,
valname(scope->vars[i].val.type),
scope->vars[i].val.num
);
}
}
void
walkval(struct val *v, ulong depth)
{
ulong i;
for (i = 0; i < depth; ++i)
fputs(" ", stderr);
fprintf(stderr, "%s", valname(v->type));
switch (v->type)
{
break; case VARR:
fprintf(stderr, ": %lu\n", v->len);
for (i = 0; i < v->len; ++i)
walkval(&v->arr[i], depth + 1);
break; case VNUM: fprintf(stderr, ": %lld\n", v->num);
break; case VVAR: fprintf(stderr, ": %s\n", v->name);
break; default: putc('\n', stderr);
}
}
|