add -e flag
This commit is contained in:
@@ -435,6 +435,12 @@ if (args != null && (_init == null || !_init.program)) {
|
||||
}
|
||||
}
|
||||
|
||||
// -e flag: eval script string directly
|
||||
var _eval_script = (_init != null && _init.eval_script) ? _init.eval_script : null
|
||||
if (_eval_script && !_init.program) {
|
||||
_init.program = "-e"
|
||||
}
|
||||
|
||||
use_cache['core/internal/os'] = os
|
||||
|
||||
// Extra env properties added as engine initializes (log, runtime fns, etc.)
|
||||
@@ -1426,6 +1432,47 @@ function enet_check()
|
||||
// Finally, run the program
|
||||
actor_mod.setname(_cell.args.program)
|
||||
|
||||
if (_eval_script) {
|
||||
// -e flag: compile and run inline script
|
||||
$_.clock(_ => {
|
||||
var env = {}
|
||||
arrfor(array(runtime_env), function(k) { env[k] = runtime_env[k] })
|
||||
env.use = function(path) {
|
||||
var ck = 'core/' + path
|
||||
var _use_core_result = null
|
||||
var _use_core_ok = false
|
||||
if (use_cache[ck]) return use_cache[ck]
|
||||
var _try_core = function() {
|
||||
_use_core_result = use_core(path)
|
||||
_use_core_ok = true
|
||||
} disruption {}
|
||||
_try_core()
|
||||
if (_use_core_ok && _use_core_result) return _use_core_result
|
||||
var _shop_use = function() {
|
||||
return shop.use(path, null)
|
||||
} disruption {
|
||||
log.error(`use('${path}') failed`)
|
||||
disrupt
|
||||
}
|
||||
return _shop_use()
|
||||
}
|
||||
env.args = _cell.args.arg
|
||||
env.log = log
|
||||
env = stone(env)
|
||||
|
||||
var ast = analyze(_eval_script, "-e")
|
||||
var val = null
|
||||
var _compile = function() {
|
||||
var mach_blob = compile_user_blob("-e", ast, null)
|
||||
val = mach_load(mach_blob, env)
|
||||
} disruption {
|
||||
os.exit(1)
|
||||
}
|
||||
_compile()
|
||||
})
|
||||
return null
|
||||
}
|
||||
|
||||
var prog = _cell.args.program
|
||||
if (ends_with(prog, '.cm')) {
|
||||
os.print(`error: ${prog} is a module (.cm), not a program (.ce)\n`)
|
||||
@@ -1562,18 +1609,23 @@ $_.clock(_ => {
|
||||
var script = null
|
||||
var ast = null
|
||||
var mach_blob = null
|
||||
if (cached_path && fd.is_file(cached_path)) {
|
||||
val = mach_load(fd.slurp(cached_path), env)
|
||||
} else {
|
||||
script = text(source_blob)
|
||||
ast = analyze(script, prog_path)
|
||||
mach_blob = compile_user_blob(prog, ast, pkg)
|
||||
if (cached_path) {
|
||||
ensure_build_dir()
|
||||
fd.slurpwrite(cached_path, mach_blob)
|
||||
var _compile = function() {
|
||||
if (cached_path && fd.is_file(cached_path)) {
|
||||
val = mach_load(fd.slurp(cached_path), env)
|
||||
} else {
|
||||
script = text(source_blob)
|
||||
ast = analyze(script, prog_path)
|
||||
mach_blob = compile_user_blob(prog, ast, pkg)
|
||||
if (cached_path) {
|
||||
ensure_build_dir()
|
||||
fd.slurpwrite(cached_path, mach_blob)
|
||||
}
|
||||
val = mach_load(mach_blob, env)
|
||||
}
|
||||
val = mach_load(mach_blob, env)
|
||||
} disruption {
|
||||
os.exit(1)
|
||||
}
|
||||
_compile()
|
||||
if (val) {
|
||||
log.error('Program must not return anything')
|
||||
disrupt
|
||||
|
||||
Reference in New Issue
Block a user