File: visitor.c

package info (click to toggle)
faucc 20180503-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 1,452 kB
  • sloc: ansic: 38,797; yacc: 1,602; xml: 519; lex: 378; sh: 178; makefile: 125
file content (156 lines) | stat: -rw-r--r-- 3,001 bytes parent folder | download | duplicates (3)
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
/*
 * $Id: visitor.c,v 1.2 2012/02/09 10:43:29 vrsieh Exp $
 *
 * Copyright (C) 2007-2009 FAUcc Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include <assert.h>
#include <stdio.h>

#include "visitor.h"

static int
visitor_expr_do(
	struct stmt *block,
	struct stmt *stmt,
	struct expr *expr,
	int (*ev)(int, struct stmt *, struct stmt *, struct expr *)
)
{
	int retval;
	struct expr *ce;

	retval = 0;

	retval |= (*ev)(0, block, stmt, expr);
	if (expr->expr0) {
		retval |= visitor_expr_do(block, stmt, expr->expr0, ev);
	}
	if (expr->expr1) {
		retval |= visitor_expr_do(block, stmt, expr->expr1, ev);
	}
	if (expr->expr2) {
		retval |= visitor_expr_do(block, stmt, expr->expr2, ev);
	}
	for (ce = expr->first; ce; ce = ce->next) {
		retval |= visitor_expr_do(block, stmt, ce, ev);
	}
	retval |= (*ev)(1, block, stmt, expr);

	return retval;
}

static int
visitor_stmt_do(
	int repeat,
	struct stmt *block,
	struct stmt *stmt,
	int (*sv)(int, struct stmt *, struct stmt *),
	int (*ev)(int, struct stmt *, struct stmt *, struct expr *)
)
{
	int retval;
	struct stmt *cs;

	retval = 0;

	if (sv) {
		retval |= (*sv)(0, block, stmt);
	}
	if (ev
	 && stmt->expr0) {
		retval |= visitor_expr_do(block, stmt, stmt->expr0, ev);
	}
	if (ev
	 && stmt->expr1) {
		retval |= visitor_expr_do(block, stmt, stmt->expr1, ev);
	}
	if (ev
	 && stmt->expr2) {
		retval |= visitor_expr_do(block, stmt, stmt->expr2, ev);
	}
	if (stmt->stmt0) {
		retval |= visitor_stmt_do(repeat, block, stmt->stmt0, sv, ev);
	}
	if (stmt->stmt1) {
		retval |= visitor_stmt_do(repeat, block, stmt->stmt1, sv, ev);
	}
again:	;
	for (cs = stmt->stmt_first; cs; ) {
		struct stmt *next;

		next = cs->next;
		if (visitor_stmt_do(repeat, stmt, cs, sv, ev)) {
			retval |= 1;
			if (repeat) {
				goto again;
			}
		}
		cs = next;
	}
	if (sv) {
		retval |= (*sv)(1, block, stmt);
	}

	return retval;
}

static int
visitor_scope(
	struct scope *scope,
	int repeat,
	int (*sv)(int, struct stmt *, struct stmt *),
	int (*ev)(int, struct stmt *, struct stmt *, struct expr *)
)
{
	struct declaration *dion;
	int retval;
	struct stmt *cs;

	retval = 0;

	for (dion = scope->declaration_first; dion; dion = dion->next) {
		if (! dion->stmt) {
			continue;
		}

	again:	;
		for (cs = dion->stmt->stmt_first; cs; ) {
			struct stmt *next;

			next = cs->next;
			if (visitor_stmt_do(repeat, dion->stmt, cs, sv, ev)) {
				retval |= 1;
				if (repeat) {
					goto again;
				}
			}
			cs = next;
		}
	}

	return retval;
}

int
visitor_stmt(
	struct scope *scope,
	int repeat,
	int (*sv)(int, struct stmt *, struct stmt *)
)
{
	return visitor_scope(scope, repeat, sv, NULL);
}

int
visitor_expr(
	struct scope *scope,
	int repeat,
	int (*ev)(int, struct stmt *, struct stmt *, struct expr *)
)
{
	return visitor_scope(scope, repeat, NULL, ev);
}