154 lines
3.8 KiB
Plaintext
154 lines
3.8 KiB
Plaintext
// seed_bootstrap.cm — minimal bootstrap for regenerating boot files
|
|
// Loads only the compiler pipeline, runs a script directly (no engine/actors)
|
|
// Usage: ./cell --dev --seed regen
|
|
//
|
|
// Hidden env: os, core_path, shop_path, args, json
|
|
|
|
var load_internal = os.load_internal
|
|
var fd = load_internal("js_fd_use")
|
|
|
|
var use_cache = {}
|
|
use_cache['fd'] = fd
|
|
use_cache['os'] = os
|
|
use_cache['json'] = json
|
|
|
|
function use_basic(path) {
|
|
if (use_cache[path])
|
|
return use_cache[path]
|
|
var result = load_internal("js_" + replace(path, '/', '_') + "_use")
|
|
if (result) {
|
|
use_cache[path] = result
|
|
return result
|
|
}
|
|
return null
|
|
}
|
|
|
|
// Load a module from boot .mcode — no caching, just eval
|
|
function boot_load(name) {
|
|
var mcode_path = core_path + '/boot/' + name + ".cm.mcode"
|
|
var mcode_json = null
|
|
if (!fd.is_file(mcode_path)) {
|
|
print("seed: missing boot mcode: " + mcode_path + "\n")
|
|
disrupt
|
|
}
|
|
mcode_json = text(fd.slurp(mcode_path))
|
|
return mach_eval_mcode(name, mcode_json, {use: use_basic})
|
|
}
|
|
|
|
var tokenize_mod = boot_load("tokenize")
|
|
var parse_mod = boot_load("parse")
|
|
var fold_mod = boot_load("fold")
|
|
var mcode_mod = boot_load("mcode")
|
|
var streamline_mod = boot_load("streamline")
|
|
|
|
use_cache['tokenize'] = tokenize_mod
|
|
use_cache['parse'] = parse_mod
|
|
use_cache['fold'] = fold_mod
|
|
use_cache['mcode'] = mcode_mod
|
|
use_cache['streamline'] = streamline_mod
|
|
|
|
function analyze(src, filename) {
|
|
var tok_result = tokenize_mod(src, filename)
|
|
var ast = parse_mod(tok_result.tokens, src, filename, tokenize_mod)
|
|
var _i = 0
|
|
var e = null
|
|
var has_errors = ast.errors != null && length(ast.errors) > 0
|
|
if (has_errors) {
|
|
while (_i < length(ast.errors)) {
|
|
e = ast.errors[_i]
|
|
if (e.line != null) {
|
|
print(`${filename}:${text(e.line)}:${text(e.column)}: error: ${e.message}`)
|
|
} else {
|
|
print(`${filename}: error: ${e.message}`)
|
|
}
|
|
_i = _i + 1
|
|
}
|
|
disrupt
|
|
}
|
|
return fold_mod(ast)
|
|
}
|
|
|
|
function run_ast(name, ast, env) {
|
|
var compiled = mcode_mod(ast)
|
|
var optimized = streamline_mod(compiled)
|
|
var mcode_json = json.encode(optimized)
|
|
return mach_eval_mcode(name, mcode_json, env)
|
|
}
|
|
|
|
function use_fn(path) {
|
|
var result = null
|
|
var file_path = null
|
|
var script = null
|
|
var ast = null
|
|
var mcode_path = null
|
|
var mcode_json = null
|
|
if (use_cache[path])
|
|
return use_cache[path]
|
|
|
|
// Try C embed
|
|
result = load_internal("js_" + replace(path, '/', '_') + "_use")
|
|
if (result) {
|
|
use_cache[path] = result
|
|
return result
|
|
}
|
|
|
|
// Try boot mcode
|
|
mcode_path = core_path + '/boot/' + path + '.cm.mcode'
|
|
if (fd.is_file(mcode_path)) {
|
|
mcode_json = text(fd.slurp(mcode_path))
|
|
result = mach_eval_mcode(path, mcode_json, {use: use_fn})
|
|
use_cache[path] = result
|
|
return result
|
|
}
|
|
|
|
// Try .cm source (CWD then core)
|
|
file_path = path + '.cm'
|
|
if (!fd.is_file(file_path))
|
|
file_path = core_path + '/' + path + '.cm'
|
|
if (fd.is_file(file_path)) {
|
|
script = text(fd.slurp(file_path))
|
|
ast = analyze(script, file_path)
|
|
result = run_ast(path, ast, {use: use_fn})
|
|
use_cache[path] = result
|
|
return result
|
|
}
|
|
|
|
print("seed: module not found: " + path + "\n")
|
|
disrupt
|
|
}
|
|
|
|
// Run the program from args
|
|
var program = args[0]
|
|
var user_args = []
|
|
var _j = 1
|
|
var prog_path = null
|
|
var script = null
|
|
var ast = null
|
|
|
|
if (!program) {
|
|
print("seed: no program specified\n")
|
|
disrupt
|
|
}
|
|
|
|
while (_j < length(args)) {
|
|
push(user_args, args[_j])
|
|
_j = _j + 1
|
|
}
|
|
|
|
prog_path = program + '.ce'
|
|
if (!fd.is_file(prog_path))
|
|
prog_path = core_path + '/' + program + '.ce'
|
|
if (!fd.is_file(prog_path)) {
|
|
prog_path = program + '.cm'
|
|
if (!fd.is_file(prog_path))
|
|
prog_path = core_path + '/' + program + '.cm'
|
|
}
|
|
if (!fd.is_file(prog_path)) {
|
|
print("seed: program not found: " + program + "\n")
|
|
disrupt
|
|
}
|
|
|
|
script = text(fd.slurp(prog_path))
|
|
ast = analyze(script, prog_path)
|
|
run_ast(program, ast, {use: use_fn, args: user_args})
|