diff --git a/internal/bootstrap.cm b/internal/bootstrap.cm index 3a3ee860..d5a53ddf 100644 --- a/internal/bootstrap.cm +++ b/internal/bootstrap.cm @@ -1,4 +1,4 @@ -// Hidden vars (os, args, core_path) come from env +// Hidden vars (os, args, core_path, use_mcode) come from env // args[0] = script name, args[1..] = user args var load_internal = os.load_internal function use_embed(name) { @@ -27,6 +27,14 @@ 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}) +// Optionally load mcode compiler module +var mcode_mod = null +var mcode_path = null +if (use_mcode) { + mcode_path = core_path + "/mcode.cm" + mcode_mod = mach_eval("mcode", text(fd.slurp(mcode_path)), {use: use_basic}) +} + // analyze: tokenize + parse, check for errors function analyze(src, filename) { var tok_result = tokenize_mod(src, filename) @@ -61,6 +69,16 @@ function analyze(src, filename) { return ast } +// Run AST through either mcode or mach pipeline +function run_ast(name, ast, env) { + var compiled = null + if (use_mcode) { + compiled = mcode_mod(ast) + return mcode_run(name, json.encode(compiled), env) + } + return mach_eval_ast(name, json.encode(ast), env) +} + // use() with ƿit pipeline for .cm modules function use(path) { var file_path = path + '.cm' @@ -77,7 +95,7 @@ function use(path) { if (fd.is_file(file_path)) { script = text(fd.slurp(file_path)) ast = analyze(script, file_path) - result = mach_eval_ast(path, json.encode(ast), {use: use}) + result = run_ast(path, ast, {use: use}) use_cache[path] = result return result } @@ -105,4 +123,4 @@ while (_j < length(args)) { 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}) +run_ast(program, ast, {use: use, args: user_args, json: json}) diff --git a/parse.cm b/parse.cm index f3aced0f..df98bdc0 100644 --- a/parse.cm +++ b/parse.cm @@ -357,7 +357,11 @@ var parse = function(tokens, src, filename) { } node.pattern = pattern_str if (length(flags) > 0) node.flags = flags - advance() + // Skip all tokens consumed by the regex re-scan + while (true) { + advance() + if (tok.kind == "eof" || tok.at >= rpos) break + } ast_node_end(node) return node } diff --git a/source/cell.c b/source/cell.c index b4bad4de..49e57222 100644 --- a/source/cell.c +++ b/source/cell.c @@ -295,7 +295,7 @@ static void print_usage(const char *prog) printf("Options:\n"); printf(" --ast Output AST as JSON\n"); printf(" --tokenize Output token array as JSON\n"); - printf(" --mcode Output MCODE IR as JSON\n"); + printf(" --mcode