fix regex parser error
This commit is contained in:
@@ -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
|
// args[0] = script name, args[1..] = user args
|
||||||
var load_internal = os.load_internal
|
var load_internal = os.load_internal
|
||||||
function use_embed(name) {
|
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 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})
|
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
|
// analyze: tokenize + parse, check for errors
|
||||||
function analyze(src, filename) {
|
function analyze(src, filename) {
|
||||||
var tok_result = tokenize_mod(src, filename)
|
var tok_result = tokenize_mod(src, filename)
|
||||||
@@ -61,6 +69,16 @@ function analyze(src, filename) {
|
|||||||
return ast
|
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
|
// use() with ƿit pipeline for .cm modules
|
||||||
function use(path) {
|
function use(path) {
|
||||||
var file_path = path + '.cm'
|
var file_path = path + '.cm'
|
||||||
@@ -77,7 +95,7 @@ function use(path) {
|
|||||||
if (fd.is_file(file_path)) {
|
if (fd.is_file(file_path)) {
|
||||||
script = text(fd.slurp(file_path))
|
script = text(fd.slurp(file_path))
|
||||||
ast = analyze(script, 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
|
use_cache[path] = result
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
@@ -105,4 +123,4 @@ while (_j < length(args)) {
|
|||||||
|
|
||||||
var script = text(fd.slurp(script_file))
|
var script = text(fd.slurp(script_file))
|
||||||
var ast = analyze(script, 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})
|
||||||
|
|||||||
6
parse.cm
6
parse.cm
@@ -357,7 +357,11 @@ var parse = function(tokens, src, filename) {
|
|||||||
}
|
}
|
||||||
node.pattern = pattern_str
|
node.pattern = pattern_str
|
||||||
if (length(flags) > 0) node.flags = flags
|
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)
|
ast_node_end(node)
|
||||||
return node
|
return node
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -295,7 +295,7 @@ static void print_usage(const char *prog)
|
|||||||
printf("Options:\n");
|
printf("Options:\n");
|
||||||
printf(" --ast <code|file> Output AST as JSON\n");
|
printf(" --ast <code|file> Output AST as JSON\n");
|
||||||
printf(" --tokenize <code|file> Output token array as JSON\n");
|
printf(" --tokenize <code|file> Output token array as JSON\n");
|
||||||
printf(" --mcode <code|file> Output MCODE IR as JSON\n");
|
printf(" --mcode <script> [args] Run through mcode compilation pipeline\n");
|
||||||
printf(" --run-mcode <code|file> Compile and run through MCODE interpreter\n");
|
printf(" --run-mcode <code|file> Compile and run through MCODE interpreter\n");
|
||||||
printf(" --mach <code|file> Output MACH bytecode\n");
|
printf(" --mach <code|file> Output MACH bytecode\n");
|
||||||
printf(" --mach-run <code|file> Compile and run through MACH VM\n");
|
printf(" --mach-run <code|file> Compile and run through MACH VM\n");
|
||||||
@@ -420,66 +420,6 @@ int cell_init(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check for --mcode flag to output MCODE JSON IR */
|
|
||||||
if (argc >= 3 && strcmp(argv[1], "--mcode") == 0) {
|
|
||||||
const char *script_or_file = argv[2];
|
|
||||||
char *script = NULL;
|
|
||||||
char *allocated_script = NULL;
|
|
||||||
const char *filename = "<eval>";
|
|
||||||
|
|
||||||
struct stat st;
|
|
||||||
if (stat(script_or_file, &st) == 0 && S_ISREG(st.st_mode)) {
|
|
||||||
FILE *f = fopen(script_or_file, "r");
|
|
||||||
if (!f) {
|
|
||||||
printf("Failed to open file: %s\n", script_or_file);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
allocated_script = malloc(st.st_size + 1);
|
|
||||||
if (!allocated_script) {
|
|
||||||
fclose(f);
|
|
||||||
printf("Failed to allocate memory for script\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
size_t read_size = fread(allocated_script, 1, st.st_size, f);
|
|
||||||
fclose(f);
|
|
||||||
allocated_script[read_size] = '\0';
|
|
||||||
script = allocated_script;
|
|
||||||
filename = script_or_file;
|
|
||||||
} else {
|
|
||||||
script = (char *)script_or_file;
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *ast = JS_ASTTree(script, strlen(script), filename);
|
|
||||||
if (!ast) {
|
|
||||||
printf("Failed to parse AST\n");
|
|
||||||
free(allocated_script);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (print_tree_errors(ast)) {
|
|
||||||
cJSON_Delete(ast);
|
|
||||||
free(allocated_script);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *mcode = JS_McodeTree(ast);
|
|
||||||
cJSON_Delete(ast);
|
|
||||||
|
|
||||||
if (!mcode) {
|
|
||||||
printf("Failed to generate MCODE\n");
|
|
||||||
free(allocated_script);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *pretty = cJSON_Print(mcode);
|
|
||||||
cJSON_Delete(mcode);
|
|
||||||
printf("%s\n", pretty);
|
|
||||||
free(pretty);
|
|
||||||
|
|
||||||
free(allocated_script);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Check for --run-mcode flag to execute via MCODE interpreter */
|
/* Check for --run-mcode flag to execute via MCODE interpreter */
|
||||||
if (argc >= 3 && strcmp(argv[1], "--run-mcode") == 0) {
|
if (argc >= 3 && strcmp(argv[1], "--run-mcode") == 0) {
|
||||||
const char *filename = argv[2];
|
const char *filename = argv[2];
|
||||||
@@ -681,6 +621,13 @@ int cell_init(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Default: run script through mach-run bootstrap pipeline */
|
/* Default: run script through mach-run bootstrap pipeline */
|
||||||
|
int use_mcode = 0;
|
||||||
|
int arg_start = 1;
|
||||||
|
if (argc >= 3 && strcmp(argv[1], "--mcode") == 0) {
|
||||||
|
use_mcode = 1;
|
||||||
|
arg_start = 2;
|
||||||
|
}
|
||||||
|
|
||||||
if (!find_cell_shop()) return 1;
|
if (!find_cell_shop()) return 1;
|
||||||
|
|
||||||
size_t boot_size;
|
size_t boot_size;
|
||||||
@@ -720,8 +667,9 @@ int cell_init(int argc, char **argv)
|
|||||||
JSValue hidden_env = JS_NewObject(ctx);
|
JSValue hidden_env = JS_NewObject(ctx);
|
||||||
JS_SetPropertyStr(ctx, hidden_env, "os", js_os_use(ctx));
|
JS_SetPropertyStr(ctx, hidden_env, "os", js_os_use(ctx));
|
||||||
JS_SetPropertyStr(ctx, hidden_env, "core_path", JS_NewString(ctx, core_path));
|
JS_SetPropertyStr(ctx, hidden_env, "core_path", JS_NewString(ctx, core_path));
|
||||||
|
JS_SetPropertyStr(ctx, hidden_env, "use_mcode", JS_NewBool(ctx, use_mcode));
|
||||||
JSValue args_arr = JS_NewArray(ctx);
|
JSValue args_arr = JS_NewArray(ctx);
|
||||||
for (int i = 1; i < argc; i++) {
|
for (int i = arg_start; i < argc; i++) {
|
||||||
JSValue str = JS_NewString(ctx, argv[i]);
|
JSValue str = JS_NewString(ctx, argv[i]);
|
||||||
JS_ArrayPush(ctx, &args_arr, str);
|
JS_ArrayPush(ctx, &args_arr, str);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10467,6 +10467,35 @@ static JSValue js_mach_eval_ast (JSContext *ctx, JSValue this_val, int argc, JSV
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* mcode_run(name, mcode_json, env?) - run pre-compiled mcode JSON */
|
||||||
|
static JSValue js_mcode_run (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
|
||||||
|
if (argc < 2 || !JS_IsText (argv[0]) || !JS_IsText (argv[1]))
|
||||||
|
return JS_ThrowTypeError (ctx, "mcode_run requires (name, mcode_json) text arguments");
|
||||||
|
|
||||||
|
const char *name = JS_ToCString (ctx, argv[0]);
|
||||||
|
if (!name) return JS_EXCEPTION;
|
||||||
|
|
||||||
|
const char *json_str = JS_ToCString (ctx, argv[1]);
|
||||||
|
if (!json_str) {
|
||||||
|
JS_FreeCString (ctx, name);
|
||||||
|
return JS_EXCEPTION;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON *mcode = cJSON_Parse (json_str);
|
||||||
|
JS_FreeCString (ctx, json_str);
|
||||||
|
|
||||||
|
if (!mcode) {
|
||||||
|
JS_FreeCString (ctx, name);
|
||||||
|
return JS_ThrowSyntaxError (ctx, "mcode_run: failed to parse mcode JSON");
|
||||||
|
}
|
||||||
|
|
||||||
|
JSValue env = (argc >= 3 && JS_IsObject (argv[2])) ? argv[2] : JS_NULL;
|
||||||
|
JSValue result = JS_CallMcodeTreeEnv (ctx, mcode, env);
|
||||||
|
cJSON_Delete (mcode);
|
||||||
|
JS_FreeCString (ctx, name);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
/* ============================================================================
|
/* ============================================================================
|
||||||
* stone() function - deep freeze with blob support
|
* stone() function - deep freeze with blob support
|
||||||
* ============================================================================
|
* ============================================================================
|
||||||
@@ -11561,6 +11590,7 @@ static void JS_AddIntrinsicBaseObjects (JSContext *ctx) {
|
|||||||
js_set_global_cfunc(ctx, "eval", js_cell_eval, 2);
|
js_set_global_cfunc(ctx, "eval", js_cell_eval, 2);
|
||||||
js_set_global_cfunc(ctx, "mach_eval", js_mach_eval, 3);
|
js_set_global_cfunc(ctx, "mach_eval", js_mach_eval, 3);
|
||||||
js_set_global_cfunc(ctx, "mach_eval_ast", js_mach_eval_ast, 3);
|
js_set_global_cfunc(ctx, "mach_eval_ast", js_mach_eval_ast, 3);
|
||||||
|
js_set_global_cfunc(ctx, "mcode_run", js_mcode_run, 3);
|
||||||
js_set_global_cfunc(ctx, "stone", js_cell_stone, 1);
|
js_set_global_cfunc(ctx, "stone", js_cell_stone, 1);
|
||||||
js_set_global_cfunc(ctx, "length", js_cell_length, 1);
|
js_set_global_cfunc(ctx, "length", js_cell_length, 1);
|
||||||
js_set_global_cfunc(ctx, "call", js_cell_call, 3);
|
js_set_global_cfunc(ctx, "call", js_cell_call, 3);
|
||||||
|
|||||||
Reference in New Issue
Block a user