streamlined cell running
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// Hidden vars (os, args) come from env
|
||||
// args[0] = script filename, args[1..] = user args
|
||||
// Hidden vars (os, args, core_path) come from env
|
||||
// args[0] = script name, args[1..] = user args
|
||||
var load_internal = os.load_internal
|
||||
function use_embed(name) {
|
||||
return load_internal("js_" + name + "_use")
|
||||
@@ -13,23 +13,76 @@ use_cache['fd'] = fd
|
||||
use_cache['os'] = os
|
||||
use_cache['json'] = json
|
||||
|
||||
function use(path) {
|
||||
// Bootstrap: load tokenize.cm and parse.cm via C pipeline (mach_eval)
|
||||
function use_basic(path) {
|
||||
if (use_cache[path])
|
||||
return use_cache[path];
|
||||
return use_cache[path]
|
||||
var result = use_embed(replace(path, '/', '_'))
|
||||
use_cache[path] = result
|
||||
return result
|
||||
}
|
||||
|
||||
var tok_path = core_path + "/tokenize.cm"
|
||||
var par_path = core_path + "/parse.cm"
|
||||
var tokenize_mod = mach_eval("tokenize", text(fd.slurp(tok_path)), {use: use_basic})
|
||||
var parse_mod = mach_eval("parse", text(fd.slurp(par_path)), {use: use_basic})
|
||||
|
||||
// analyze: tokenize + parse, check for errors
|
||||
function analyze(src, filename) {
|
||||
var tok_result = tokenize_mod(src, filename)
|
||||
var ast = parse_mod(tok_result.tokens, src, filename)
|
||||
var _i = 0
|
||||
var prev_line = -1
|
||||
var prev_msg = null
|
||||
var e = null
|
||||
var msg = null
|
||||
var line = null
|
||||
var col = null
|
||||
var has_errors = ast.errors != null && length(ast.errors) > 0
|
||||
if (has_errors) {
|
||||
while (_i < length(ast.errors)) {
|
||||
e = ast.errors[_i]
|
||||
msg = e.message
|
||||
line = e.line
|
||||
col = e.column
|
||||
if (msg != prev_msg || line != prev_line) {
|
||||
if (line != null && col != null) {
|
||||
print(`${filename}:${text(line)}:${text(col)}: error: ${msg}`)
|
||||
} else {
|
||||
print(`${filename}: error: ${msg}`)
|
||||
}
|
||||
}
|
||||
prev_line = line
|
||||
prev_msg = msg
|
||||
_i = _i + 1
|
||||
}
|
||||
disrupt
|
||||
}
|
||||
return ast
|
||||
}
|
||||
|
||||
// use() with ƿit pipeline for .cm modules
|
||||
function use(path) {
|
||||
var file_path = path + '.cm'
|
||||
var script = null
|
||||
var ast = null
|
||||
var result = null
|
||||
var exports = null
|
||||
if (use_cache[path])
|
||||
return use_cache[path]
|
||||
|
||||
// Check CWD first, then core_path
|
||||
if (!fd.is_file(file_path))
|
||||
file_path = core_path + '/' + path + '.cm'
|
||||
|
||||
if (fd.is_file(file_path)) {
|
||||
script = text(fd.slurp(file_path))
|
||||
result = mach_eval(path, script, {use: use})
|
||||
ast = analyze(script, file_path)
|
||||
result = mach_eval_ast(path, json.encode(ast), {use: use})
|
||||
use_cache[path] = result
|
||||
return result
|
||||
}
|
||||
|
||||
// Try embedded C module
|
||||
// Fallback to embedded C module
|
||||
result = use_embed(replace(path, '/', '_'))
|
||||
use_cache[path] = result
|
||||
return result
|
||||
@@ -37,14 +90,19 @@ function use(path) {
|
||||
|
||||
// Load and run the user's program
|
||||
var program = args[0]
|
||||
var script_file = program
|
||||
|
||||
// Add .ce extension if not already present
|
||||
if (!ends_with(script_file, '.ce') && !ends_with(script_file, '.cm'))
|
||||
script_file = program + '.ce'
|
||||
|
||||
var user_args = []
|
||||
var _i = 1
|
||||
while (_i < length(args)) {
|
||||
push(user_args, args[_i])
|
||||
_i = _i + 1
|
||||
var _j = 1
|
||||
while (_j < length(args)) {
|
||||
push(user_args, args[_j])
|
||||
_j = _j + 1
|
||||
}
|
||||
var blob = fd.slurp(program)
|
||||
stone(blob)
|
||||
var script = text(blob)
|
||||
mach_eval(program, script, {use: use, args: user_args, json: json})
|
||||
|
||||
var script = text(fd.slurp(script_file))
|
||||
var ast = analyze(script, script_file)
|
||||
mach_eval_ast(program, json.encode(ast), {use: use, args: user_args, json: json})
|
||||
|
||||
Reference in New Issue
Block a user