-
-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathsplit-aaa-auto.R
More file actions
339 lines (302 loc) · 9.64 KB
/
Copy pathsplit-aaa-auto.R
File metadata and controls
339 lines (302 loc) · 9.64 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#!/usr/bin/env Rscript
# Split a monolithic stimulus-generated aaa-auto.R into per-category
# R/aaa-<cat>.R files, using tools/aaa-categories.yaml for the mapping.
#
# Source file: first argument, or .build/aaa-auto.R by default, or
# R/aaa-auto.R as a transitional fallback.
#
# Each output file:
# - starts with a generated-file header + `# styler: off` pragma
# - groups impls by subcategory, each group prefixed with a banner comment
# - preserves each impl's original source formatting byte-for-byte
suppressPackageStartupMessages({
library(yaml)
})
# ---- locate repo + inputs/outputs ----------------------------------------
script_path <- (function() {
args <- commandArgs(trailingOnly = FALSE)
m <- grep("^--file=", args, value = TRUE)
if (length(m)) sub("^--file=", "", m[1]) else "tools/split-aaa-auto.R"
})()
REPO <- normalizePath(file.path(dirname(script_path), ".."))
CATS <- file.path(REPO, "tools", "aaa-categories.yaml")
# Source file precedence: CLI arg > .build/aaa-auto.R > R/aaa-auto.R
cli_args <- commandArgs(trailingOnly = TRUE)
candidate <- if (length(cli_args) >= 1) {
cli_args[1]
} else {
file.path(REPO, ".build", "aaa-auto.R")
}
if (!file.exists(candidate)) {
fallback <- file.path(REPO, "R", "aaa-auto.R")
if (file.exists(fallback)) {
candidate <- fallback
} else {
stop(
"split-aaa-auto.R: no source file found at ",
candidate,
" or ",
fallback
)
}
}
SRC <- normalizePath(candidate)
OUT_DIR <- file.path(REPO, "R")
message("split-aaa-auto.R: reading ", SRC)
# ---- closure normalization (matches tools/rebuild-cats.R) ----------------
closure_map <- c(
"igraph_bfs_closure" = "igraph_bfs",
"igraph_dfs_closure" = "igraph_dfs",
"igraph_cliques_callback_closure" = "igraph_cliques_callback",
"igraph_maximal_cliques_callback_closure" = "igraph_maximal_cliques_callback",
"igraph_simple_cycles_callback_closure" = "igraph_simple_cycles_callback",
"igraph_get_isomorphisms_vf2_callback_closure" = "igraph_get_isomorphisms_vf2_callback",
"igraph_get_subisomorphisms_vf2_callback_closure" = "igraph_get_subisomorphisms_vf2_callback",
"igraph_motifs_randesu_callback_closure" = "igraph_motifs_randesu_callback",
"igraph_community_leading_eigenvector_callback_closure" = "igraph_community_leading_eigenvector"
)
# ---- load categories: build (cat, sub) lookup keyed by C function --------
cats <- yaml::read_yaml(CATS)
cat_lookup <- new.env(hash = TRUE, parent = emptyenv())
for (cat in names(cats)) {
node <- cats[[cat]]
if (is.character(node)) {
for (fn in node) {
cat_lookup[[fn]] <- list(category = cat, subcategory = NA_character_)
}
} else if (is.list(node)) {
for (sub in names(node)) {
for (fn in node[[sub]]) {
cat_lookup[[fn]] <- list(category = cat, subcategory = sub)
}
}
}
}
# ---- parse SRC, extract each impl's (name, c_function, src_text) --------
src_lines <- readLines(SRC, warn = FALSE)
parsed <- parse(text = src_lines, keep.source = TRUE)
pdata <- utils::getParseData(parsed) # AST + line positions
extract_call_sym <- function(expr) {
# First non-finalizer `.Call(...)` symbol found in the expression tree.
recur <- function(e) {
if (is.call(e)) {
fn <- e[[1]]
if (is.name(fn) && as.character(fn) == ".Call" && length(e) >= 2) {
first <- e[[2]]
if (is.name(first)) {
sym <- as.character(first)
if (sym != "R_igraph_finalizer") return(sym)
}
}
for (i in seq_along(e)) {
r <- recur(e[[i]])
if (!is.null(r)) return(r)
}
}
NULL
}
recur(expr)
}
impls <- list()
for (i in seq_along(parsed)) {
expr <- parsed[[i]]
if (!is.call(expr) || length(expr) < 3) {
next
}
op <- as.character(expr[[1]])
if (!op %in% c("<-", "=", "assign")) {
next
}
lhs <- expr[[2]]
if (!is.name(lhs)) {
next
}
impl_name <- as.character(lhs)
if (!grepl("_impl$", impl_name)) {
next
}
rhs <- expr[[3]]
if (!(is.call(rhs) && identical(rhs[[1]], as.name("function")))) {
next
}
sym <- extract_call_sym(rhs)
if (is.null(sym)) {
stop("impl ", impl_name, " has no non-finalizer .Call() target; aborting.")
}
c_literal <- sub("^R_", "", sym)
c_fn <- if (c_literal %in% names(closure_map)) {
closure_map[[c_literal]]
} else {
c_literal
}
sref <- attr(expr, "srcref")
if (is.null(sref)) {
sref <- getSrcref(parsed)[[i]]
}
line1 <- sref[1L]
line2 <- sref[3L]
src_text <- paste(src_lines[line1:line2], collapse = "\n")
impls[[length(impls) + 1L]] <- list(
impl_name = impl_name,
c_function = c_fn,
src_text = src_text
)
}
message("parsed ", length(impls), " _impl wrappers from source")
# ---- map each impl to (category, subcategory); validate no gaps ---------
# Functions not listed in aaa-categories.yaml are emitted into
# R/aaa-uncategorized.R with a warning, so a new wrapper landing upstream
# never breaks the build. The warning lists exactly what needs a home.
unassigned <- character()
for (i in seq_along(impls)) {
lookup <- cat_lookup[[impls[[i]]$c_function]]
if (is.null(lookup)) {
unassigned <- c(
unassigned,
paste0(impls[[i]]$impl_name, " -> ", impls[[i]]$c_function)
)
impls[[i]]$category <- "uncategorized"
impls[[i]]$subcategory <- NA_character_
} else {
impls[[i]]$category <- lookup$category
impls[[i]]$subcategory <- lookup$subcategory
}
}
if (length(unassigned)) {
warning(
"impls without a category in ",
CATS,
" — written to R/aaa-uncategorized.R:\n ",
paste(unassigned, collapse = "\n "),
"\nAdd placements in tools/rebuild-cats.R and rerun it to clear this.",
call. = FALSE
)
}
# ---- Subcategory ordering (mirrors tools/rebuild-cats.R override) -------
subcategory_order_overrides <- list(
structural = c(
"basic-properties",
"degree-sequences",
"directedness-conversion",
"efficiency-measures",
"graph-components",
"k-cores",
"matchings",
"matrix-representations",
"maximum-cardinality-search-chordal-graphs",
"mixing-patterns",
"mutual-edges",
"neighborhood-of-a-vertex",
"non-simple-graphs-multiple-and-loop-edges",
"percolation",
"pre-calculated-subsets",
"similarity-measures",
"sparsifiers",
"spectral-properties",
"summary-statistics",
"them-statistics",
"transitivity-or-clustering-coefficient",
"us-statistics"
),
centrality = c(
"centrality-measures",
"centralization",
"range-limited-centrality-measures",
"subset-limited-centrality-measures"
),
paths = c(
"distances-and-metrics",
"shortest-paths",
"widest-path-related-functions"
),
trees = c(
"spanning-trees-and-forests",
"unfolding-a-graph-into-a-tree"
)
)
# Natural subcategory order per category from the YAML itself (insertion order)
yaml_sub_order <- lapply(names(cats), function(cat) {
node <- cats[[cat]]
if (is.list(node) && !is.null(names(node))) names(node) else character()
})
names(yaml_sub_order) <- names(cats)
# ---- remove stale R/aaa-*.R before writing ------------------------------
stale <- list.files(OUT_DIR, pattern = "^aaa-.*\\.R$", full.names = TRUE)
# Keep aaa-auto.R if present — the caller deletes it manually after the split.
stale <- stale[basename(stale) != "aaa-auto.R"]
if (length(stale)) {
message("removing ", length(stale), " stale R/aaa-*.R file(s)")
invisible(file.remove(stale))
}
# ---- emit per-category files --------------------------------------------
impl_tbl <- do.call(
rbind,
lapply(impls, function(x) {
data.frame(
impl_name = x$impl_name,
c_function = x$c_function,
category = x$category,
subcategory = x$subcategory,
src_text = x$src_text,
stringsAsFactors = FALSE
)
})
)
src_basename <- basename(SRC)
categories <- sort(unique(impl_tbl$category))
header <- c(
paste0("# Generated by tools/split-aaa-auto.R from ", src_basename,
" — do not edit by hand"),
"# styler: off",
"# jarl-ignore-file unused_function: irrelevant for _impl functions",
""
)
for (cat in categories) {
sub_tbl <- impl_tbl[impl_tbl$category == cat, ]
override <- subcategory_order_overrides[[cat]]
natural <- yaml_sub_order[[cat]]
prev <- if (!is.null(override)) {
override
} else if (!is.null(natural)) {
natural
} else {
character()
}
subs_present <- unique(sub_tbl$subcategory)
sub_order <- c(intersect(prev, subs_present), setdiff(subs_present, prev))
out_lines <- header
# Insert a blank line between each impl's source block.
with_blanks <- function(srcs) as.vector(rbind(srcs, ""))
# Emit NA (flat-list) entries first with no banner
flat_mask <- is.na(sub_tbl$subcategory)
if (any(flat_mask)) {
flat <- sub_tbl[flat_mask, ]
flat <- flat[order(flat$impl_name), ]
out_lines <- c(out_lines, with_blanks(flat$src_text))
}
for (sub in sub_order) {
if (is.na(sub)) {
next
}
rows <- sub_tbl[!is.na(sub_tbl$subcategory) & sub_tbl$subcategory == sub, ]
rows <- rows[order(rows$impl_name), ]
if (nrow(rows) == 0) {
next
}
banner <- paste0("# ==== ", sub, " ====")
out_lines <- c(out_lines, banner, "", with_blanks(rows$src_text))
}
# Strip trailing empty lines, leave exactly one terminating newline
while (length(out_lines) && !nzchar(tail(out_lines, 1))) {
out_lines <- out_lines[-length(out_lines)]
}
out_path <- file.path(OUT_DIR, paste0("aaa-", cat, ".R"))
writeLines(out_lines, out_path)
}
message(
"wrote ",
length(categories),
" R/aaa-<cat>.R files (",
nrow(impl_tbl),
" impls total)"
)