// 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("internal/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') {
    log.compile("usage: pit seed [--clean]")
    log.compile("")
    log.compile("  Regenerate boot seed files in boot/")
    log.compile("  --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', 'qbe', 'qbe_emit']
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
var build_dir = 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)) {
    log.error('WARNING: source not found: ' + src_path)
    continue
  }

  log.compile('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)))
  log.compile('  -> ' + 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)) {
  log.compile('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)))
  log.compile('  -> ' + out_path + ' (' + text(length(mcode_json)) + ' bytes)')
  generated = generated + 1
} else {
  log.error('WARNING: bootstrap source not found: ' + bootstrap_path)
}

log.compile('\nRegenerated ' + text(generated) + ' seed(s)')

if (clean) {
  build_dir = shop.get_build_dir()
  if (fd.is_dir(build_dir)) {
    log.compile('Clearing build cache: ' + build_dir)
    os.system('rm -rf "' + build_dir + '"')
    log.compile('Build cache cleared. Next run will recompile from new seeds.')
  } else {
    log.compile('No build cache to clear.')
  }
}

$stop()
