// Portail Complexity Analysis
// Scans all source files for O(n) annotations and generates a report
import { write, read, exists } from "std/fs"
import { join } from "std/text"
import { now } from "std/date"
let report_file = "target/complexity-report.md"
let src_dir = "src"
// Create target directory
$ mkdir -p target $
// Initialize report
let report = "# Time Complexity Analysis\n\n"
report += "Generated: {now()}\n\n"
// Function to extract complexity annotations from a file
fun extract_complexity(file: Text): [Text] {
let annotations = [Text]
let content = read(file) failed {
return annotations
}
let lines = split(content, "\n")
let line_num = 1
for line in lines {
if line ~ "O(" {
// Extract O(...) pattern
let complexity = match(line, r"O\([^)]+\)")
if len(complexity) > 0 {
annotations += ["| {file} | {line_num} | {complexity[0]} |"]
}
}
line_num += 1
}
return annotations
}
// Function to analyze function signatures
fun analyze_functions(file: Text): [Text] {
let functions = [Text]
let content = read(file) failed {
return functions
}
let lines = split(content, "\n")
let line_num = 1
for line in lines {
if line ~ "fn " && !(line ~ "//") {
// Extract function name
let name_match = match(line, r"fn\s+([a-zA-Z_][a-zA-Z0-9_]*)")
if len(name_match) > 0 {
functions += ["| {file} | {line_num} | {name_match[0]} |"]
}
}
line_num += 1
}
return functions
}
// Scan for complexity annotations
report += "## Complexity Annotations\n\n"
report += "| File | Line | Complexity |\n"
report += "|------|------|------------|\n"
let total_annotations = 0
let files = $ find {src_dir} -name "*.rs" -o -name "*.md" | sort $
for file in split(files, "\n") {
if len(file) > 0 {
let annotations = extract_complexity(file)
for ann in annotations {
report += "{ann}\n"
total_annotations += 1
}
}
}
report += "\n**Total annotations: {total_annotations}**\n\n"
// Scan for functions
report += "## Function Inventory\n\n"
report += "| File | Line | Function |\n"
report += "|------|------|----------|\n"
let total_functions = 0
let rs_files = $ find {src_dir} -name "*.rs" | sort $
for file in split(rs_files, "\n") {
if len(file) > 0 {
let functions = analyze_functions(file)
for func in functions {
report += "{func}\n"
total_functions += 1
}
}
}
report += "\n**Total functions: {total_functions}**\n\n"
// Summary
report += "## Summary\n\n"
let total_files = $ find {src_dir} \( -name "*.rs" -o -name "*.md" \) -type f | wc -l $
report += "- **Total files scanned:** {trim(total_files)}\n"
report += "- **Total functions:** {total_functions}\n"
report += "- **Complexity annotations:** {total_annotations}\n\n"
// Write report
write(report_file, report) failed {
echo("Error: Failed to write report")
exit 1
}
echo("Complexity report generated at {report_file}")