Files
cell/seed.ce
2026-02-17 13:37:17 -06:00

117 lines
3.4 KiB
Plaintext

// seed.ce — regenerate boot seed files in boot/
//
// Usage:
// pit seed Regenerate all boot seeds
// pit seed --clean Also clear the build cache after
//
// Seeds are the pre-compiled mcode IR (JSON) files that bootstrap the
// compilation pipeline on cold start. They only need regenerating when:
// - The pipeline source (tokenize, parse, fold, mcode, streamline) changes
// in a way that the boot seeds can't compile the new source
// - You want the seed files to match the current pipeline output
// (e.g. before a release or distribution)
//
// The engine already recompiles pipeline modules from source when their
// content changes (content-addressed cache). Seeds are a fallback for
// cold start when no cache exists.
var fd = use("fd")
var json = use("json")
var os = use("os")
var shop = use("internal/shop")
var tokenize = use("tokenize")
var parse = use("parse")
var fold = use("fold")
var mcode = use("mcode")
var streamline = use("streamline")
var clean = false
var i = 0
for (i = 0; i < length(args); i++) {
if (args[i] == '--clean') {
clean = true
} else if (args[i] == '--help' || args[i] == '-h') {
print("usage: pit seed [--clean]")
print("")
print(" Regenerate boot seed files in boot/")
print(" --clean Also clear the build cache after")
$stop()
}
}
var core_dir = shop.get_package_dir('core')
var boot_dir = core_dir + '/boot'
var pipeline_modules = ['tokenize', 'parse', 'fold', 'mcode', 'streamline']
var generated = 0
var name = null
var src_path = null
var src = null
var tok = null
var ast = null
var folded = null
var compiled = null
var optimized = null
var mcode_json = null
var out_path = null
// Regenerate pipeline module seeds
for (i = 0; i < length(pipeline_modules); i++) {
name = pipeline_modules[i]
src_path = core_dir + '/' + name + '.cm'
if (!fd.is_file(src_path)) {
print('WARNING: source not found: ' + src_path)
continue
}
print('Seeding ' + name + '.cm ...')
src = text(fd.slurp(src_path))
tok = tokenize(src, src_path)
ast = parse(tok.tokens, src, src_path, tokenize)
folded = fold(ast)
compiled = mcode(folded)
optimized = streamline(compiled)
mcode_json = json.encode(optimized)
out_path = boot_dir + '/' + name + '.cm.mcode'
fd.slurpwrite(out_path, stone(blob(mcode_json)))
print(' -> ' + out_path + ' (' + text(length(mcode_json)) + ' bytes)')
generated = generated + 1
}
// Regenerate bootstrap.cm seed
var bootstrap_path = core_dir + '/internal/bootstrap.cm'
if (fd.is_file(bootstrap_path)) {
print('Seeding bootstrap.cm ...')
src = text(fd.slurp(bootstrap_path))
tok = tokenize(src, bootstrap_path)
ast = parse(tok.tokens, src, bootstrap_path, tokenize)
folded = fold(ast)
compiled = mcode(folded)
optimized = streamline(compiled)
mcode_json = json.encode(optimized)
out_path = boot_dir + '/bootstrap.cm.mcode'
fd.slurpwrite(out_path, stone(blob(mcode_json)))
print(' -> ' + out_path + ' (' + text(length(mcode_json)) + ' bytes)')
generated = generated + 1
} else {
print('WARNING: bootstrap source not found: ' + bootstrap_path)
}
print('\nRegenerated ' + text(generated) + ' seed(s)')
if (clean) {
var build_dir = shop.get_build_dir()
if (fd.is_dir(build_dir)) {
print('Clearing build cache: ' + build_dir)
os.system('rm -rf "' + build_dir + '"')
print('Build cache cleared. Next run will recompile from new seeds.')
} else {
print('No build cache to clear.')
}
}
$stop()