summaryrefslogtreecommitdiffstats
path: root/bm/seed.pl
blob: fdf3884214a059f09a1f27465004b09c0fe4b5bd (plain)
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
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use File::Path qw(make_path);

my ($total_files, $max_depth) = @ARGV;

if (!defined $total_files || !defined $max_depth) {
	die "Usage: perl seed.pl <total_files> <max_depth>\nExample: perl seed.pl 200 20\n";
}

my $target_root = "sandbox";
my $files_created = 0;

sub generate_content {
	my $size_kb = 5 + int(rand(21));
	my $target_bytes = $size_kb * 1024;
	my $content = "";

	my @types    = qw(int char void float double uint32_t bool);
	my @ops      = qw(= += -= == != < >);
	my @vars     = qw(count index status buffer ptr limit offset);

	$content .= "#include <stdio.h>\n#include <stdlib.h>\n\n";

	while (length($content) < $target_bytes) {
		my $roll = rand();

		if ($roll < 0.15) {
			# Function definition (no indent)
			my $type = $types[rand @types];
			my $name = "process_data_" . int(rand(100));
			$content .= "\n$type $name() {\n";
		} 
		elsif ($roll < 0.60) {
			# Standard assignment (1 tab)
			my $type = $types[rand @types];
			my $var  = $vars[rand @vars] . "_" . int(rand(50));
			my $val  = int(rand(1000));
			$content .= "\t$type $var = $val;\n";
		} 
		elsif ($roll < 0.80) {
			# If block with nested logic (1-2 tabs)
			my $var = $vars[rand @vars];
			$content .= "\tif ($var > " . int(rand(100)) . ") {\n";
			$content .= "\t\t$var " . $ops[rand @ops] . " 1;\n";
			$content .= "\t\treturn $var;\n";
			$content .= "\t}\n";
		} 
		elsif ($roll < 0.95) {
			# Closing brace (no indent)
			$content .= "}\n\n";
		}
		else {
			# Comment line (1 tab)
			$content .= "\t/* Update state " . int(rand(1000)) . " */\n";
		}
	}
	
	# Cleanup: Ensure we close the last function block
	$content .= "\n}\n" unless $content =~ /}\n\s*$/;
	return $content;
}

sub seed_tree {
	my ($current_path, $depth, $files_left) = @_;
	return if $files_left <= 0;

	my $files_here = ($depth == $max_depth) 
		? $files_left 
		: int($files_left / ($max_depth - $depth + 1)) + int(rand(3));
	
	$files_here = $files_left if $files_here > $files_left;

	make_path($current_path) unless -d $current_path;

	for (1..$files_here) {
		my $filename = "src_" . sprintf("%04d", ++$files_created) . ".c";
		my $full_path = File::Spec->catfile($current_path, $filename);
		open(my $fh, '>', $full_path) or die "Could not open $full_path: $!";
		print $fh generate_content();
		close($fh);
	}

	if ($depth < $max_depth && ($files_left - $files_here) > 0) {
		my $next_dir = File::Spec->catdir($current_path, "subdir_" . ($depth + 1));
		seed_tree($next_dir, $depth + 1, $files_left - $files_here);
	}
}

print "Seeding $total_files files (C-style) with tab indents...\n";
seed_tree($target_root, 1, $total_files);
print "Success. Created $files_created files in '$target_root'.\n";